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.

51 lines
1.7 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import FileDownloader
  4. from ..compat import compat_urllib_request
  5. class DashSegmentsFD(FileDownloader):
  6. """
  7. Download segments in a DASH manifest
  8. """
  9. def real_download(self, filename, info_dict):
  10. self.report_destination(filename)
  11. tmpfilename = self.temp_name(filename)
  12. base_url = info_dict['url']
  13. segment_urls = info_dict['segment_urls']
  14. self.byte_counter = 0
  15. def append_url_to_file(outf, target_url, target_name):
  16. self.to_screen('[DashSegments] %s: Downloading %s' % (info_dict['id'], target_name))
  17. req = compat_urllib_request.Request(target_url)
  18. data = self.ydl.urlopen(req).read()
  19. outf.write(data)
  20. self.byte_counter += len(data)
  21. def combine_url(base_url, target_url):
  22. if re.match(r'^https?://', target_url):
  23. return target_url
  24. return '%s/%s' % (base_url, target_url)
  25. with open(tmpfilename, 'wb') as outf:
  26. append_url_to_file(
  27. outf, combine_url(base_url, info_dict['initialization_url']),
  28. 'initialization segment')
  29. for i, segment_url in enumerate(segment_urls):
  30. append_url_to_file(
  31. outf, combine_url(base_url, segment_url),
  32. 'segment %d / %d' % (i + 1, len(segment_urls)))
  33. self.try_rename(tmpfilename, filename)
  34. self._hook_progress({
  35. 'downloaded_bytes': self.byte_counter,
  36. 'total_bytes': self.byte_counter,
  37. 'filename': filename,
  38. 'status': 'finished',
  39. })
  40. return True