]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/dash.py
7491fdad857af2f36433b8991b6208fbd9cef99e
[youtubedl] / youtube_dl / downloader / dash.py
1 from __future__ import unicode_literals
2
3 from .fragment import FragmentFD
4 from ..compat import compat_urllib_error
5
6
7 class DashSegmentsFD(FragmentFD):
8 """
9 Download segments in a DASH manifest
10 """
11
12 FD_NAME = 'dashsegments'
13
14 def real_download(self, filename, info_dict):
15 segments = info_dict['fragments'][:1] if self.params.get(
16 'test', False) else info_dict['fragments']
17
18 ctx = {
19 'filename': filename,
20 'total_frags': len(segments),
21 }
22
23 self._prepare_and_start_frag_download(ctx)
24
25 fragment_retries = self.params.get('fragment_retries', 0)
26 skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
27
28 frag_index = 0
29 for i, segment in enumerate(segments):
30 frag_index += 1
31 if frag_index <= ctx['fragment_index']:
32 continue
33 # In DASH, the first segment contains necessary headers to
34 # generate a valid MP4 file, so always abort for the first segment
35 fatal = i == 0 or not skip_unavailable_fragments
36 count = 0
37 while count <= fragment_retries:
38 try:
39 success, frag_content = self._download_fragment(ctx, segment['url'], info_dict)
40 if not success:
41 return False
42 self._append_fragment(ctx, frag_content)
43 break
44 except compat_urllib_error.HTTPError as err:
45 # YouTube may often return 404 HTTP error for a fragment causing the
46 # whole download to fail. However if the same fragment is immediately
47 # retried with the same request data this usually succeeds (1-2 attemps
48 # is usually enough) thus allowing to download the whole file successfully.
49 # To be future-proof we will retry all fragments that fail with any
50 # HTTP error.
51 count += 1
52 if count <= fragment_retries:
53 self.report_retry_fragment(err, frag_index, count, fragment_retries)
54 if count > fragment_retries:
55 if not fatal:
56 self.report_skip_fragment(frag_index)
57 continue
58 self.report_error('giving up after %s fragment retries' % fragment_retries)
59 return False
60
61 self._finish_frag_download(ctx)
62
63 return True