fragment.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from __future__ import division, unicode_literals
  2. import os
  3. import time
  4. from .common import FileDownloader
  5. from .http import HttpFD
  6. from ..utils import (
  7. encodeFilename,
  8. sanitize_open,
  9. )
  10. class HttpQuietDownloader(HttpFD):
  11. def to_screen(self, *args, **kargs):
  12. pass
  13. class FragmentFD(FileDownloader):
  14. """
  15. A base file downloader class for fragmented media (e.g. f4m/m3u8 manifests).
  16. """
  17. def _prepare_and_start_frag_download(self, ctx):
  18. self._prepare_frag_download(ctx)
  19. self._start_frag_download(ctx)
  20. def _prepare_frag_download(self, ctx):
  21. self.to_screen('[%s] Total fragments: %d' % (self.FD_NAME, ctx['total_frags']))
  22. self.report_destination(ctx['filename'])
  23. dl = HttpQuietDownloader(
  24. self.ydl,
  25. {
  26. 'continuedl': True,
  27. 'quiet': True,
  28. 'noprogress': True,
  29. 'ratelimit': self.params.get('ratelimit', None),
  30. 'retries': self.params.get('retries', 0),
  31. 'test': self.params.get('test', False),
  32. }
  33. )
  34. tmpfilename = self.temp_name(ctx['filename'])
  35. dest_stream, tmpfilename = sanitize_open(tmpfilename, 'wb')
  36. ctx.update({
  37. 'dl': dl,
  38. 'dest_stream': dest_stream,
  39. 'tmpfilename': tmpfilename,
  40. })
  41. def _start_frag_download(self, ctx):
  42. total_frags = ctx['total_frags']
  43. # This dict stores the download progress, it's updated by the progress
  44. # hook
  45. state = {
  46. 'status': 'downloading',
  47. 'downloaded_bytes': 0,
  48. 'frag_index': 0,
  49. 'frag_count': total_frags,
  50. 'filename': ctx['filename'],
  51. 'tmpfilename': ctx['tmpfilename'],
  52. # Total complete fragments downloaded so far in bytes
  53. '_complete_frags_downloaded_bytes': 0,
  54. # Amount of fragment's bytes downloaded by the time of the previous
  55. # frag progress hook invocation
  56. '_prev_frag_downloaded_bytes': 0,
  57. }
  58. start = time.time()
  59. ctx['started'] = start
  60. def frag_progress_hook(s):
  61. if s['status'] not in ('downloading', 'finished'):
  62. return
  63. frag_total_bytes = s.get('total_bytes') or 0
  64. estimated_size = (
  65. (state['_complete_frags_downloaded_bytes'] + frag_total_bytes) /
  66. (state['frag_index'] + 1) * total_frags)
  67. time_now = time.time()
  68. state['total_bytes_estimate'] = estimated_size
  69. state['elapsed'] = time_now - start
  70. if s['status'] == 'finished':
  71. state['frag_index'] += 1
  72. state['downloaded_bytes'] += frag_total_bytes - state['_prev_frag_downloaded_bytes']
  73. state['_complete_frags_downloaded_bytes'] = state['downloaded_bytes']
  74. state['_prev_frag_downloaded_bytes'] = 0
  75. else:
  76. frag_downloaded_bytes = s['downloaded_bytes']
  77. state['downloaded_bytes'] += frag_downloaded_bytes - state['_prev_frag_downloaded_bytes']
  78. state['eta'] = self.calc_eta(
  79. start, time_now, estimated_size,
  80. state['downloaded_bytes'])
  81. state['speed'] = s.get('speed')
  82. state['_prev_frag_downloaded_bytes'] = frag_downloaded_bytes
  83. self._hook_progress(state)
  84. ctx['dl'].add_progress_hook(frag_progress_hook)
  85. return start
  86. def _finish_frag_download(self, ctx):
  87. ctx['dest_stream'].close()
  88. elapsed = time.time() - ctx['started']
  89. self.try_rename(ctx['tmpfilename'], ctx['filename'])
  90. fsize = os.path.getsize(encodeFilename(ctx['filename']))
  91. self._hook_progress({
  92. 'downloaded_bytes': fsize,
  93. 'total_bytes': fsize,
  94. 'filename': ctx['filename'],
  95. 'status': 'finished',
  96. 'elapsed': elapsed,
  97. })