[downloader/dash] Abort if the first segment fails
[youtube-dl] / youtube_dl / downloader / dash.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5
6 from .fragment import FragmentFD
7 from ..compat import compat_urllib_error
8 from ..utils import (
9     sanitize_open,
10     encodeFilename,
11 )
12
13
14 class DashSegmentsFD(FragmentFD):
15     """
16     Download segments in a DASH manifest
17     """
18
19     FD_NAME = 'dashsegments'
20
21     def real_download(self, filename, info_dict):
22         base_url = info_dict['url']
23         segment_urls = [info_dict['segment_urls'][0]] if self.params.get('test', False) else info_dict['segment_urls']
24         initialization_url = info_dict.get('initialization_url')
25
26         ctx = {
27             'filename': filename,
28             'total_frags': len(segment_urls) + (1 if initialization_url else 0),
29         }
30
31         self._prepare_and_start_frag_download(ctx)
32
33         def combine_url(base_url, target_url):
34             if re.match(r'^https?://', target_url):
35                 return target_url
36             return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url)
37
38         segments_filenames = []
39
40         fragment_retries = self.params.get('fragment_retries', 0)
41         skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
42
43         def process_segment(segment, tmp_filename, fatal):
44             target_url, segment_name = segment
45             target_filename = '%s-%s' % (tmp_filename, segment_name)
46             count = 0
47             while count <= fragment_retries:
48                 try:
49                     success = ctx['dl'].download(target_filename, {'url': combine_url(base_url, target_url)})
50                     if not success:
51                         return False
52                     down, target_sanitized = sanitize_open(target_filename, 'rb')
53                     ctx['dest_stream'].write(down.read())
54                     down.close()
55                     segments_filenames.append(target_sanitized)
56                     break
57                 except compat_urllib_error.HTTPError as err:
58                     # YouTube may often return 404 HTTP error for a fragment causing the
59                     # whole download to fail. However if the same fragment is immediately
60                     # retried with the same request data this usually succeeds (1-2 attemps
61                     # is usually enough) thus allowing to download the whole file successfully.
62                     # To be future-proof we will retry all fragments that fail with any
63                     # HTTP error.
64                     count += 1
65                     if count <= fragment_retries:
66                         self.report_retry_fragment(err, segment_name, count, fragment_retries)
67             if count > fragment_retries:
68                 if not fatal:
69                     self.report_skip_fragment(segment_name)
70                     return True
71                 self.report_error('giving up after %s fragment retries' % fragment_retries)
72                 return False
73             return True
74
75         segments_to_download = [(initialization_url, 'Init')] if initialization_url else []
76         segments_to_download.extend([
77             (segment_url, 'Seg%d' % i)
78             for i, segment_url in enumerate(segment_urls)])
79
80         for i, segment in enumerate(segments_to_download):
81             # In DASH, the first segment contains necessary headers to
82             # generate a valid MP4 file, so always abort for the first segment
83             fatal = i == 0 or not skip_unavailable_fragments
84             if not process_segment(segment, ctx['tmpfilename'], fatal):
85                 return False
86
87         self._finish_frag_download(ctx)
88
89         for segment_file in segments_filenames:
90             os.remove(encodeFilename(segment_file))
91
92         return True