2021-12-20 01:06:46 -05:00
|
|
|
import time
|
2015-06-03 11:10:18 -04:00
|
|
|
|
2022-04-17 13:18:50 -04:00
|
|
|
from . import get_suitable_downloader
|
2016-02-09 11:25:02 -05:00
|
|
|
from .fragment import FragmentFD
|
2021-06-21 14:59:50 -04:00
|
|
|
from ..utils import urljoin
|
2015-06-04 10:12:05 -04:00
|
|
|
|
2015-06-03 11:10:18 -04:00
|
|
|
|
2016-02-09 11:25:02 -05:00
|
|
|
class DashSegmentsFD(FragmentFD):
|
2015-06-03 11:10:18 -04:00
|
|
|
"""
|
2021-03-10 10:26:24 -05:00
|
|
|
Download segments in a DASH manifest. External downloaders can take over
|
2021-04-10 11:08:33 -04:00
|
|
|
the fragment downloads by supporting the 'dash_frag_urls' protocol
|
2015-06-03 11:10:18 -04:00
|
|
|
"""
|
|
|
|
|
2016-02-09 11:25:02 -05:00
|
|
|
FD_NAME = 'dashsegments'
|
2015-06-10 02:45:54 -04:00
|
|
|
|
2016-02-09 11:25:02 -05:00
|
|
|
def real_download(self, filename, info_dict):
|
2021-12-20 01:06:46 -05:00
|
|
|
if info_dict.get('is_live') and set(info_dict['protocol'].split('+')) != {'http_dash_segments_generator'}:
|
2021-07-31 06:51:01 -04:00
|
|
|
self.report_error('Live DASH videos are not supported')
|
|
|
|
|
2021-12-20 01:06:46 -05:00
|
|
|
real_start = time.time()
|
2021-07-31 06:53:54 -04:00
|
|
|
real_downloader = get_suitable_downloader(
|
2021-08-01 03:22:09 -04:00
|
|
|
info_dict, self.params, None, protocol='dash_frag_urls', to_stdout=(filename == '-'))
|
2021-02-08 11:46:01 -05:00
|
|
|
|
2021-12-20 01:06:46 -05:00
|
|
|
requested_formats = [{**info_dict, **fmt} for fmt in info_dict.get('requested_formats', [])]
|
|
|
|
args = []
|
|
|
|
for fmt in requested_formats or [info_dict]:
|
|
|
|
try:
|
|
|
|
fragment_count = 1 if self.params.get('test') else len(fmt['fragments'])
|
|
|
|
except TypeError:
|
|
|
|
fragment_count = None
|
|
|
|
ctx = {
|
|
|
|
'filename': fmt.get('filepath') or filename,
|
|
|
|
'live': 'is_from_start' if fmt.get('is_from_start') else fmt.get('is_live'),
|
|
|
|
'total_frags': fragment_count,
|
|
|
|
}
|
|
|
|
|
|
|
|
if real_downloader:
|
|
|
|
self._prepare_external_frag_download(ctx)
|
|
|
|
else:
|
|
|
|
self._prepare_and_start_frag_download(ctx, fmt)
|
|
|
|
ctx['start'] = real_start
|
|
|
|
|
|
|
|
fragments_to_download = self._get_fragments(fmt, ctx)
|
|
|
|
|
|
|
|
if real_downloader:
|
|
|
|
self.to_screen(
|
2022-04-11 11:10:28 -04:00
|
|
|
f'[{self.FD_NAME}] Fragment downloads will be delegated to {real_downloader.get_basename()}')
|
2021-12-21 11:08:13 -05:00
|
|
|
info_dict['fragments'] = list(fragments_to_download)
|
2021-12-20 01:06:46 -05:00
|
|
|
fd = real_downloader(self.ydl, self.params)
|
|
|
|
return fd.real_download(filename, info_dict)
|
|
|
|
|
|
|
|
args.append([ctx, fragments_to_download, fmt])
|
2015-06-10 02:45:54 -04:00
|
|
|
|
2021-12-20 01:06:46 -05:00
|
|
|
return self.download_and_append_fragments_multiple(*args)
|
|
|
|
|
|
|
|
def _resolve_fragments(self, fragments, ctx):
|
|
|
|
fragments = fragments(ctx) if callable(fragments) else fragments
|
2021-12-22 17:04:18 -05:00
|
|
|
return [next(iter(fragments))] if self.params.get('test') else fragments
|
2021-12-20 01:06:46 -05:00
|
|
|
|
|
|
|
def _get_fragments(self, fmt, ctx):
|
|
|
|
fragment_base_url = fmt.get('fragment_base_url')
|
|
|
|
fragments = self._resolve_fragments(fmt['fragments'], ctx)
|
2015-06-03 11:10:18 -04:00
|
|
|
|
2016-06-28 13:07:50 -04:00
|
|
|
frag_index = 0
|
2017-08-04 19:57:19 -04:00
|
|
|
for i, fragment in enumerate(fragments):
|
2016-06-28 13:07:50 -04:00
|
|
|
frag_index += 1
|
2017-04-22 11:42:24 -04:00
|
|
|
if frag_index <= ctx['fragment_index']:
|
2016-06-28 13:07:50 -04:00
|
|
|
continue
|
2021-02-08 11:46:01 -05:00
|
|
|
fragment_url = fragment.get('url')
|
|
|
|
if not fragment_url:
|
|
|
|
assert fragment_base_url
|
|
|
|
fragment_url = urljoin(fragment_base_url, fragment['path'])
|
|
|
|
|
2021-12-20 01:06:46 -05:00
|
|
|
yield {
|
2021-03-12 23:46:58 -05:00
|
|
|
'frag_index': frag_index,
|
2022-06-07 08:44:08 -04:00
|
|
|
'fragment_count': fragment.get('fragment_count'),
|
2021-03-12 23:46:58 -05:00
|
|
|
'index': i,
|
|
|
|
'url': fragment_url,
|
2021-12-20 01:06:46 -05:00
|
|
|
}
|