logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

dash.py (3328B)


  1. from __future__ import unicode_literals
  2. import itertools
  3. from .fragment import FragmentFD
  4. from ..compat import compat_urllib_error
  5. from ..utils import (
  6. DownloadError,
  7. urljoin,
  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. fragment_base_url = info_dict.get('fragment_base_url')
  16. fragments = info_dict['fragments'][:1] if self.params.get(
  17. 'test', False) else info_dict['fragments']
  18. ctx = {
  19. 'filename': filename,
  20. 'total_frags': len(fragments),
  21. }
  22. self._prepare_and_start_frag_download(ctx)
  23. fragment_retries = self.params.get('fragment_retries', 0)
  24. skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
  25. for frag_index, fragment in enumerate(fragments, 1):
  26. if frag_index <= ctx['fragment_index']:
  27. continue
  28. success = False
  29. # In DASH, the first segment contains necessary headers to
  30. # generate a valid MP4 file, so always abort for the first segment
  31. fatal = frag_index == 1 or not skip_unavailable_fragments
  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. headers = info_dict.get('http_headers')
  37. fragment_range = fragment.get('range')
  38. if fragment_range:
  39. headers = headers.copy() if headers else {}
  40. headers['Range'] = 'bytes=%s' % (fragment_range,)
  41. for count in itertools.count():
  42. try:
  43. success, frag_content = self._download_fragment(ctx, fragment_url, info_dict, headers)
  44. if not success:
  45. return False
  46. self._append_fragment(ctx, frag_content)
  47. except compat_urllib_error.HTTPError as err:
  48. # YouTube may often return 404 HTTP error for a fragment causing the
  49. # whole download to fail. However if the same fragment is immediately
  50. # retried with the same request data this usually succeeds (1-2 attempts
  51. # is usually enough) thus allowing to download the whole file successfully.
  52. # To be future-proof we will retry all fragments that fail with any
  53. # HTTP error.
  54. if count < fragment_retries:
  55. self.report_retry_fragment(err, frag_index, count + 1, fragment_retries)
  56. continue
  57. except DownloadError:
  58. # Don't retry fragment if error occurred during HTTP downloading
  59. # itself since it has its own retry settings
  60. if fatal:
  61. raise
  62. break
  63. if not success:
  64. if not fatal:
  65. self.report_skip_fragment(frag_index)
  66. continue
  67. self.report_error('giving up after %s fragment retries' % count)
  68. return False
  69. self._finish_frag_download(ctx)
  70. return True