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.

111 lines
3.7 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['started'] = start
  55. def frag_progress_hook(s):
  56. if s['status'] not in ('downloading', 'finished'):
  57. return
  58. frag_total_bytes = s.get('total_bytes', 0)
  59. if s['status'] == 'finished':
  60. state['downloaded_bytes'] += frag_total_bytes
  61. state['frag_index'] += 1
  62. estimated_size = (
  63. (state['downloaded_bytes'] + frag_total_bytes) /
  64. (state['frag_index'] + 1) * total_frags)
  65. time_now = time.time()
  66. state['total_bytes_estimate'] = estimated_size
  67. state['elapsed'] = time_now - start
  68. if s['status'] == 'finished':
  69. progress = self.calc_percent(state['frag_index'], total_frags)
  70. else:
  71. frag_downloaded_bytes = s['downloaded_bytes']
  72. frag_progress = self.calc_percent(frag_downloaded_bytes,
  73. frag_total_bytes)
  74. progress = self.calc_percent(state['frag_index'], total_frags)
  75. progress += frag_progress / float(total_frags)
  76. state['eta'] = self.calc_eta(
  77. start, time_now, estimated_size, state['downloaded_bytes'] + frag_downloaded_bytes)
  78. state['speed'] = s.get('speed')
  79. self._hook_progress(state)
  80. ctx['dl'].add_progress_hook(frag_progress_hook)
  81. return start
  82. def _finish_frag_download(self, ctx):
  83. ctx['dest_stream'].close()
  84. elapsed = time.time() - ctx['started']
  85. self.try_rename(ctx['tmpfilename'], ctx['filename'])
  86. fsize = os.path.getsize(encodeFilename(ctx['filename']))
  87. self._hook_progress({
  88. 'downloaded_bytes': fsize,
  89. 'total_bytes': fsize,
  90. 'filename': ctx['filename'],
  91. 'status': 'finished',
  92. 'elapsed': elapsed,
  93. })