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.

117 lines
3.9 KiB

  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. }
  53. start = time.time()
  54. ctx.update({
  55. 'started': start,
  56. # Total complete fragments downloaded so far in bytes
  57. 'complete_frags_downloaded_bytes': 0,
  58. # Amount of fragment's bytes downloaded by the time of the previous
  59. # frag progress hook invocation
  60. 'prev_frag_downloaded_bytes': 0,
  61. })
  62. def frag_progress_hook(s):
  63. if s['status'] not in ('downloading', 'finished'):
  64. return
  65. frag_total_bytes = s.get('total_bytes') or 0
  66. estimated_size = (
  67. (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) /
  68. (state['frag_index'] + 1) * total_frags)
  69. time_now = time.time()
  70. state['total_bytes_estimate'] = estimated_size
  71. state['elapsed'] = time_now - start
  72. if s['status'] == 'finished':
  73. state['frag_index'] += 1
  74. state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes']
  75. ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes']
  76. ctx['prev_frag_downloaded_bytes'] = 0
  77. else:
  78. frag_downloaded_bytes = s['downloaded_bytes']
  79. state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes']
  80. state['eta'] = self.calc_eta(
  81. start, time_now, estimated_size,
  82. state['downloaded_bytes'])
  83. state['speed'] = s.get('speed')
  84. ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes
  85. self._hook_progress(state)
  86. ctx['dl'].add_progress_hook(frag_progress_hook)
  87. return start
  88. def _finish_frag_download(self, ctx):
  89. ctx['dest_stream'].close()
  90. elapsed = time.time() - ctx['started']
  91. self.try_rename(ctx['tmpfilename'], ctx['filename'])
  92. fsize = os.path.getsize(encodeFilename(ctx['filename']))
  93. self._hook_progress({
  94. 'downloaded_bytes': fsize,
  95. 'total_bytes': fsize,
  96. 'filename': ctx['filename'],
  97. 'status': 'finished',
  98. 'elapsed': elapsed,
  99. })