You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB

  1. from __future__ import unicode_literals
  2. import os
  3. import re
  4. from .fragment import FragmentFD
  5. from ..utils import (
  6. sanitize_open,
  7. encodeFilename,
  8. )
  9. class DashSegmentsFD(FragmentFD):
  10. """
  11. Download segments in a DASH manifest
  12. """
  13. FD_NAME = 'dashsegments'
  14. def real_download(self, filename, info_dict):
  15. base_url = info_dict['url']
  16. segment_urls = [info_dict['segment_urls'][0]] if self.params.get('test', False) else info_dict['segment_urls']
  17. initialization_url = info_dict.get('initialization_url')
  18. ctx = {
  19. 'filename': filename,
  20. 'total_frags': len(segment_urls) + (1 if initialization_url else 0),
  21. }
  22. self._prepare_and_start_frag_download(ctx)
  23. def combine_url(base_url, target_url):
  24. if re.match(r'^https?://', target_url):
  25. return target_url
  26. return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url)
  27. segments_filenames = []
  28. def append_url_to_file(target_url, target_filename):
  29. success = ctx['dl'].download(target_filename, {'url': combine_url(base_url, target_url)})
  30. if not success:
  31. return False
  32. down, target_sanitized = sanitize_open(target_filename, 'rb')
  33. ctx['dest_stream'].write(down.read())
  34. down.close()
  35. segments_filenames.append(target_sanitized)
  36. if initialization_url:
  37. append_url_to_file(initialization_url, ctx['tmpfilename'] + '-Init')
  38. for i, segment_url in enumerate(segment_urls):
  39. segment_filename = '%s-Seg%d' % (ctx['tmpfilename'], i)
  40. append_url_to_file(segment_url, segment_filename)
  41. self._finish_frag_download(ctx)
  42. for segment_file in segments_filenames:
  43. os.remove(encodeFilename(segment_file))
  44. return True