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.

63 lines
2.5 KiB

  1. from __future__ import unicode_literals
  2. from .fragment import FragmentFD
  3. from ..compat import compat_urllib_error
  4. class DashSegmentsFD(FragmentFD):
  5. """
  6. Download segments in a DASH manifest
  7. """
  8. FD_NAME = 'dashsegments'
  9. def real_download(self, filename, info_dict):
  10. segments = info_dict['fragments'][:1] if self.params.get(
  11. 'test', False) else info_dict['fragments']
  12. ctx = {
  13. 'filename': filename,
  14. 'total_frags': len(segments),
  15. }
  16. self._prepare_and_start_frag_download(ctx)
  17. fragment_retries = self.params.get('fragment_retries', 0)
  18. skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
  19. frag_index = 0
  20. for i, segment in enumerate(segments):
  21. frag_index += 1
  22. if frag_index <= ctx['fragment_index']:
  23. continue
  24. # In DASH, the first segment contains necessary headers to
  25. # generate a valid MP4 file, so always abort for the first segment
  26. fatal = i == 0 or not skip_unavailable_fragments
  27. count = 0
  28. while count <= fragment_retries:
  29. try:
  30. success, frag_content = self._download_fragment(ctx, segment['url'], info_dict)
  31. if not success:
  32. return False
  33. self._append_fragment(ctx, frag_content)
  34. break
  35. except compat_urllib_error.HTTPError as err:
  36. # YouTube may often return 404 HTTP error for a fragment causing the
  37. # whole download to fail. However if the same fragment is immediately
  38. # retried with the same request data this usually succeeds (1-2 attemps
  39. # is usually enough) thus allowing to download the whole file successfully.
  40. # To be future-proof we will retry all fragments that fail with any
  41. # HTTP error.
  42. count += 1
  43. if count <= fragment_retries:
  44. self.report_retry_fragment(err, frag_index, count, fragment_retries)
  45. if count > fragment_retries:
  46. if not fatal:
  47. self.report_skip_fragment(frag_index)
  48. continue
  49. self.report_error('giving up after %s fragment retries' % fragment_retries)
  50. return False
  51. self._finish_frag_download(ctx)
  52. return True