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.

69 lines
2.8 KiB

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