dash.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 'http_dash_segments_generator' in info_dict['protocol'].split('+'):
  14. real_downloader = None # No external FD can support --live-from-start
  15. else:
  16. if info_dict.get('is_live'):
  17. self.report_error('Live DASH videos are not supported')
  18. real_downloader = get_suitable_downloader(
  19. info_dict, self.params, None, protocol='dash_frag_urls', to_stdout=(filename == '-'))
  20. real_start = time.time()
  21. requested_formats = [{**info_dict, **fmt} for fmt in info_dict.get('requested_formats', [])]
  22. args = []
  23. for fmt in requested_formats or [info_dict]:
  24. try:
  25. fragment_count = 1 if self.params.get('test') else len(fmt['fragments'])
  26. except TypeError:
  27. fragment_count = None
  28. ctx = {
  29. 'filename': fmt.get('filepath') or filename,
  30. 'live': 'is_from_start' if fmt.get('is_from_start') else fmt.get('is_live'),
  31. 'total_frags': fragment_count,
  32. }
  33. if real_downloader:
  34. self._prepare_external_frag_download(ctx)
  35. else:
  36. self._prepare_and_start_frag_download(ctx, fmt)
  37. ctx['start'] = real_start
  38. extra_query = None
  39. extra_param_to_segment_url = info_dict.get('extra_param_to_segment_url')
  40. if extra_param_to_segment_url:
  41. extra_query = urllib.parse.parse_qs(extra_param_to_segment_url)
  42. fragments_to_download = self._get_fragments(fmt, ctx, extra_query)
  43. if real_downloader:
  44. self.to_screen(
  45. f'[{self.FD_NAME}] Fragment downloads will be delegated to {real_downloader.get_basename()}')
  46. info_dict['fragments'] = list(fragments_to_download)
  47. fd = real_downloader(self.ydl, self.params)
  48. return fd.real_download(filename, info_dict)
  49. args.append([ctx, fragments_to_download, fmt])
  50. return self.download_and_append_fragments_multiple(*args, is_fatal=lambda idx: idx == 0)
  51. def _resolve_fragments(self, fragments, ctx):
  52. fragments = fragments(ctx) if callable(fragments) else fragments
  53. return [next(iter(fragments))] if self.params.get('test') else fragments
  54. def _get_fragments(self, fmt, ctx, extra_query):
  55. fragment_base_url = fmt.get('fragment_base_url')
  56. fragments = self._resolve_fragments(fmt['fragments'], ctx)
  57. frag_index = 0
  58. for i, fragment in enumerate(fragments):
  59. frag_index += 1
  60. if frag_index <= ctx['fragment_index']:
  61. continue
  62. fragment_url = fragment.get('url')
  63. if not fragment_url:
  64. assert fragment_base_url
  65. fragment_url = urljoin(fragment_base_url, fragment['path'])
  66. if extra_query:
  67. fragment_url = update_url_query(fragment_url, extra_query)
  68. yield {
  69. 'frag_index': frag_index,
  70. 'fragment_count': fragment.get('fragment_count'),
  71. 'index': i,
  72. 'url': fragment_url,
  73. }