dash.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import unicode_literals
  2. import os
  3. import re
  4. from .fragment import FragmentFD
  5. from ..compat import compat_urllib_error
  6. from ..utils import (
  7. sanitize_open,
  8. encodeFilename,
  9. )
  10. class DashSegmentsFD(FragmentFD):
  11. """
  12. Download segments in a DASH manifest
  13. """
  14. FD_NAME = 'dashsegments'
  15. def real_download(self, filename, info_dict):
  16. base_url = info_dict['url']
  17. segment_urls = [info_dict['segment_urls'][0]] if self.params.get('test', False) else info_dict['segment_urls']
  18. initialization_url = info_dict.get('initialization_url')
  19. ctx = {
  20. 'filename': filename,
  21. 'total_frags': len(segment_urls) + (1 if initialization_url else 0),
  22. }
  23. self._prepare_and_start_frag_download(ctx)
  24. def combine_url(base_url, target_url):
  25. if re.match(r'^https?://', target_url):
  26. return target_url
  27. return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url)
  28. segments_filenames = []
  29. fragment_retries = self.params.get('fragment_retries', 0)
  30. skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
  31. def append_url_to_file(target_url, tmp_filename, segment_name):
  32. target_filename = '%s-%s' % (tmp_filename, segment_name)
  33. count = 0
  34. while count <= fragment_retries:
  35. try:
  36. success = ctx['dl'].download(target_filename, {'url': combine_url(base_url, target_url)})
  37. if not success:
  38. return False
  39. down, target_sanitized = sanitize_open(target_filename, 'rb')
  40. ctx['dest_stream'].write(down.read())
  41. down.close()
  42. segments_filenames.append(target_sanitized)
  43. break
  44. except compat_urllib_error.HTTPError as err:
  45. # YouTube may often return 404 HTTP error for a fragment causing the
  46. # whole download to fail. However if the same fragment is immediately
  47. # retried with the same request data this usually succeeds (1-2 attemps
  48. # is usually enough) thus allowing to download the whole file successfully.
  49. # To be future-proof we will retry all fragments that fail with any
  50. # HTTP error.
  51. count += 1
  52. if count <= fragment_retries:
  53. self.report_retry_fragment(err, segment_name, count, fragment_retries)
  54. if count > fragment_retries:
  55. if skip_unavailable_fragments:
  56. self.report_skip_fragment(segment_name)
  57. return True
  58. self.report_error('giving up after %s fragment retries' % fragment_retries)
  59. return False
  60. return True
  61. if initialization_url:
  62. if not append_url_to_file(initialization_url, ctx['tmpfilename'], 'Init'):
  63. return False
  64. for i, segment_url in enumerate(segment_urls):
  65. if not append_url_to_file(segment_url, ctx['tmpfilename'], 'Seg%d' % i):
  66. return False
  67. self._finish_frag_download(ctx)
  68. for segment_file in segments_filenames:
  69. os.remove(encodeFilename(segment_file))
  70. return True