1 from __future__
import unicode_literals
4 from ..downloader
import get_suitable_downloader
5 from .fragment
import FragmentFD
7 from ..utils
import urljoin
10 class DashSegmentsFD(FragmentFD
):
12 Download segments in a DASH manifest. External downloaders can take over
13 the fragment downloads by supporting the 'dash_frag_urls' protocol
16 FD_NAME
= 'dashsegments'
18 def real_download(self
, filename
, info_dict
):
19 if info_dict
.get('is_live') and set(info_dict
['protocol'].split('+')) != {'http_dash_segments_generator'}
:
20 self
.report_error('Live DASH videos are not supported')
22 real_start
= time
.time()
23 real_downloader
= get_suitable_downloader(
24 info_dict
, self
.params
, None, protocol
='dash_frag_urls', to_stdout
=(filename
== '-'))
26 requested_formats
= [{**info_dict, **fmt}
for fmt
in info_dict
.get('requested_formats', [])]
28 for fmt
in requested_formats
or [info_dict
]:
30 fragment_count
= 1 if self
.params
.get('test') else len(fmt
['fragments'])
34 'filename': fmt
.get('filepath') or filename
,
35 'live': 'is_from_start' if fmt
.get('is_from_start') else fmt
.get('is_live'),
36 'total_frags': fragment_count
,
40 self
._prepare
_external
_frag
_download
(ctx
)
42 self
._prepare
_and
_start
_frag
_download
(ctx
, fmt
)
43 ctx
['start'] = real_start
45 fragments_to_download
= self
._get
_fragments
(fmt
, ctx
)
49 '[%s] Fragment downloads will be delegated to %s' % (self
.FD_NAME
, real_downloader
.get_basename()))
50 info_dict
['fragments'] = list(fragments_to_download
)
51 fd
= real_downloader(self
.ydl
, self
.params
)
52 return fd
.real_download(filename
, info_dict
)
54 args
.append([ctx
, fragments_to_download
, fmt
])
56 return self
.download_and_append_fragments_multiple(*args
)
58 def _resolve_fragments(self
, fragments
, ctx
):
59 fragments
= fragments(ctx
) if callable(fragments
) else fragments
60 return [next(iter(fragments
))] if self
.params
.get('test') else fragments
62 def _get_fragments(self
, fmt
, ctx
):
63 fragment_base_url
= fmt
.get('fragment_base_url')
64 fragments
= self
._resolve
_fragments
(fmt
['fragments'], ctx
)
67 for i
, fragment
in enumerate(fragments
):
69 if frag_index
<= ctx
['fragment_index']:
71 fragment_url
= fragment
.get('url')
73 assert fragment_base_url
74 fragment_url
= urljoin(fragment_base_url
, fragment
['path'])
77 'frag_index': frag_index
,