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.

122 lines
4.1 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. if 'live' not in ctx:
  22. ctx['live'] = False
  23. self.to_screen(
  24. '[%s] Total fragments: %s'
  25. % (self.FD_NAME, ctx['total_frags'] if not ctx['live'] else 'unknown (live)'))
  26. self.report_destination(ctx['filename'])
  27. dl = HttpQuietDownloader(
  28. self.ydl,
  29. {
  30. 'continuedl': True,
  31. 'quiet': True,
  32. 'noprogress': True,
  33. 'ratelimit': self.params.get('ratelimit'),
  34. 'retries': self.params.get('retries', 0),
  35. 'test': self.params.get('test', False),
  36. }
  37. )
  38. tmpfilename = self.temp_name(ctx['filename'])
  39. dest_stream, tmpfilename = sanitize_open(tmpfilename, 'wb')
  40. ctx.update({
  41. 'dl': dl,
  42. 'dest_stream': dest_stream,
  43. 'tmpfilename': tmpfilename,
  44. })
  45. def _start_frag_download(self, ctx):
  46. total_frags = ctx['total_frags']
  47. # This dict stores the download progress, it's updated by the progress
  48. # hook
  49. state = {
  50. 'status': 'downloading',
  51. 'downloaded_bytes': 0,
  52. 'frag_index': 0,
  53. 'frag_count': total_frags,
  54. 'filename': ctx['filename'],
  55. 'tmpfilename': ctx['tmpfilename'],
  56. }
  57. start = time.time()
  58. ctx.update({
  59. 'started': start,
  60. # Total complete fragments downloaded so far in bytes
  61. 'complete_frags_downloaded_bytes': 0,
  62. # Amount of fragment's bytes downloaded by the time of the previous
  63. # frag progress hook invocation
  64. 'prev_frag_downloaded_bytes': 0,
  65. })
  66. def frag_progress_hook(s):
  67. if s['status'] not in ('downloading', 'finished'):
  68. return
  69. time_now = time.time()
  70. state['elapsed'] = time_now - start
  71. frag_total_bytes = s.get('total_bytes') or 0
  72. if not ctx['live']:
  73. estimated_size = (
  74. (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) /
  75. (state['frag_index'] + 1) * total_frags)
  76. state['total_bytes_estimate'] = estimated_size
  77. if s['status'] == 'finished':
  78. state['frag_index'] += 1
  79. state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes']
  80. ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes']
  81. ctx['prev_frag_downloaded_bytes'] = 0
  82. else:
  83. frag_downloaded_bytes = s['downloaded_bytes']
  84. state['downloaded_bytes'] += frag_downloaded_bytes - ctx['prev_frag_downloaded_bytes']
  85. if not ctx['live']:
  86. state['eta'] = self.calc_eta(
  87. start, time_now, estimated_size,
  88. state['downloaded_bytes'])
  89. state['speed'] = s.get('speed')
  90. ctx['prev_frag_downloaded_bytes'] = frag_downloaded_bytes
  91. self._hook_progress(state)
  92. ctx['dl'].add_progress_hook(frag_progress_hook)
  93. return start
  94. def _finish_frag_download(self, ctx):
  95. ctx['dest_stream'].close()
  96. elapsed = time.time() - ctx['started']
  97. self.try_rename(ctx['tmpfilename'], ctx['filename'])
  98. fsize = os.path.getsize(encodeFilename(ctx['filename']))
  99. self._hook_progress({
  100. 'downloaded_bytes': fsize,
  101. 'total_bytes': fsize,
  102. 'filename': ctx['filename'],
  103. 'status': 'finished',
  104. 'elapsed': elapsed,
  105. })