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