dash.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import time
  2. import urllib.parse
  3. from . import get_suitable_downloader
  4. from .fragment import FragmentFD
  5. from ..utils import update_url_query, urljoin
  6. class DashSegmentsFD(FragmentFD):
  7. """
  8. Download segments in a DASH manifest. External downloaders can take over
  9. the fragment downloads by supporting the 'dash_frag_urls' protocol
  10. """
  11. FD_NAME = 'dashsegments'
  12. def real_download(self, filename, info_dict):
  13. if info_dict.get('is_live') and set(info_dict['protocol'].split('+')) != {'http_dash_segments_generator'}:
  14. self.report_error('Live DASH videos are not supported')
  15. real_start = time.time()
  16. real_downloader = get_suitable_downloader(
  17. info_dict, self.params, None, protocol='dash_frag_urls', to_stdout=(filename == '-'))
  18. requested_formats = [{**info_dict, **fmt} for fmt in info_dict.get('requested_formats', [])]
  19. args = []
  20. for fmt in requested_formats or [info_dict]:
  21. try:
  22. fragment_count = 1 if self.params.get('test') else len(fmt['fragments'])
  23. except TypeError:
  24. fragment_count = None
  25. ctx = {
  26. 'filename': fmt.get('filepath') or filename,
  27. 'live': 'is_from_start' if fmt.get('is_from_start') else fmt.get('is_live'),
  28. 'total_frags': fragment_count,
  29. }
  30. if real_downloader:
  31. self._prepare_external_frag_download(ctx)
  32. else:
  33. self._prepare_and_start_frag_download(ctx, fmt)
  34. ctx['start'] = real_start
  35. extra_query = None
  36. extra_param_to_segment_url = info_dict.get('extra_param_to_segment_url')
  37. if extra_param_to_segment_url:
  38. extra_query = urllib.parse.parse_qs(extra_param_to_segment_url)
  39. fragments_to_download = self._get_fragments(fmt, ctx, extra_query)
  40. if real_downloader:
  41. self.to_screen(
  42. f'[{self.FD_NAME}] Fragment downloads will be delegated to {real_downloader.get_basename()}')
  43. info_dict['fragments'] = list(fragments_to_download)
  44. fd = real_downloader(self.ydl, self.params)
  45. return fd.real_download(filename, info_dict)
  46. args.append([ctx, fragments_to_download, fmt])
  47. return self.download_and_append_fragments_multiple(*args, is_fatal=lambda idx: idx == 0)
  48. def _resolve_fragments(self, fragments, ctx):
  49. fragments = fragments(ctx) if callable(fragments) else fragments
  50. return [next(iter(fragments))] if self.params.get('test') else fragments
  51. def _get_fragments(self, fmt, ctx, extra_query):
  52. fragment_base_url = fmt.get('fragment_base_url')
  53. fragments = self._resolve_fragments(fmt['fragments'], ctx)
  54. frag_index = 0
  55. for i, fragment in enumerate(fragments):
  56. frag_index += 1
  57. if frag_index <= ctx['fragment_index']:
  58. continue
  59. fragment_url = fragment.get('url')
  60. if not fragment_url:
  61. assert fragment_base_url
  62. fragment_url = urljoin(fragment_base_url, fragment['path'])
  63. if extra_query:
  64. fragment_url = update_url_query(fragment_url, extra_query)
  65. yield {
  66. 'frag_index': frag_index,
  67. 'fragment_count': fragment.get('fragment_count'),
  68. 'index': i,
  69. 'url': fragment_url,
  70. }