1 from __future__
import unicode_literals
3 from ..downloader
import _get_real_downloader
4 from .fragment
import FragmentFD
6 from ..compat
import compat_urllib_error
13 class DashSegmentsFD(FragmentFD
):
15 Download segments in a DASH manifest
18 FD_NAME
= 'dashsegments'
20 def real_download(self
, filename
, info_dict
):
21 fragment_base_url
= info_dict
.get('fragment_base_url')
22 fragments
= info_dict
['fragments'][:1] if self
.params
.get(
23 'test', False) else info_dict
['fragments']
25 real_downloader
= _get_real_downloader(info_dict
, 'frag_urls', self
.params
, None)
29 'total_frags': len(fragments
),
33 self
._prepare
_external
_frag
_download
(ctx
)
35 self
._prepare
_and
_start
_frag
_download
(ctx
)
37 fragment_retries
= self
.params
.get('fragment_retries', 0)
38 skip_unavailable_fragments
= self
.params
.get('skip_unavailable_fragments', True)
42 for i
, fragment
in enumerate(fragments
):
44 if frag_index
<= ctx
['fragment_index']:
46 fragment_url
= fragment
.get('url')
48 assert fragment_base_url
49 fragment_url
= urljoin(fragment_base_url
, fragment
['path'])
52 fragment_urls
.append(fragment_url
)
55 # In DASH, the first segment contains necessary headers to
56 # generate a valid MP4 file, so always abort for the first segment
57 fatal
= i
== 0 or not skip_unavailable_fragments
59 while count
<= fragment_retries
:
61 success
, frag_content
= self
._download
_fragment
(ctx
, fragment_url
, info_dict
)
64 self
._append
_fragment
(ctx
, frag_content
)
66 except compat_urllib_error
.HTTPError
as err
:
67 # YouTube may often return 404 HTTP error for a fragment causing the
68 # whole download to fail. However if the same fragment is immediately
69 # retried with the same request data this usually succeeds (1-2 attempts
70 # is usually enough) thus allowing to download the whole file successfully.
71 # To be future-proof we will retry all fragments that fail with any
74 if count
<= fragment_retries
:
75 self
.report_retry_fragment(err
, frag_index
, count
, fragment_retries
)
77 # Don't retry fragment if error occurred during HTTP downloading
78 # itself since it has own retry settings
80 self
.report_skip_fragment(frag_index
)
84 if count
> fragment_retries
:
86 self
.report_skip_fragment(frag_index
)
88 self
.report_error('giving up after %s fragment retries' % fragment_retries
)
92 info_copy
= info_dict
.copy()
93 info_copy
['url_list'] = fragment_urls
94 fd
= real_downloader(self
.ydl
, self
.params
)
95 # TODO: Make progress updates work without hooking twice
96 # for ph in self._progress_hooks:
97 # fd.add_progress_hook(ph)
98 success
= fd
.real_download(filename
, info_copy
)
102 self
._finish
_frag
_download
(ctx
)