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.

110 lines
3.6 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. 'test': self.params.get('test', False),
  31. }
  32. )
  33. tmpfilename = self.temp_name(ctx['filename'])
  34. dest_stream, tmpfilename = sanitize_open(tmpfilename, 'wb')
  35. ctx.update({
  36. 'dl': dl,
  37. 'dest_stream': dest_stream,
  38. 'tmpfilename': tmpfilename,
  39. })
  40. def _start_frag_download(self, ctx):
  41. total_frags = ctx['total_frags']
  42. # This dict stores the download progress, it's updated by the progress
  43. # hook
  44. state = {
  45. 'status': 'downloading',
  46. 'downloaded_bytes': 0,
  47. 'frag_index': 0,
  48. 'frag_count': total_frags,
  49. 'filename': ctx['filename'],
  50. 'tmpfilename': ctx['tmpfilename'],
  51. }
  52. start = time.time()
  53. ctx['started'] = start
  54. def frag_progress_hook(s):
  55. if s['status'] not in ('downloading', 'finished'):
  56. return
  57. frag_total_bytes = s.get('total_bytes', 0)
  58. if s['status'] == 'finished':
  59. state['downloaded_bytes'] += frag_total_bytes
  60. state['frag_index'] += 1
  61. estimated_size = (
  62. (state['downloaded_bytes'] + frag_total_bytes) /
  63. (state['frag_index'] + 1) * total_frags)
  64. time_now = time.time()
  65. state['total_bytes_estimate'] = estimated_size
  66. state['elapsed'] = time_now - start
  67. if s['status'] == 'finished':
  68. progress = self.calc_percent(state['frag_index'], total_frags)
  69. else:
  70. frag_downloaded_bytes = s['downloaded_bytes']
  71. frag_progress = self.calc_percent(frag_downloaded_bytes,
  72. frag_total_bytes)
  73. progress = self.calc_percent(state['frag_index'], total_frags)
  74. progress += frag_progress / float(total_frags)
  75. state['eta'] = self.calc_eta(
  76. start, time_now, estimated_size, state['downloaded_bytes'] + frag_downloaded_bytes)
  77. state['speed'] = s.get('speed')
  78. self._hook_progress(state)
  79. ctx['dl'].add_progress_hook(frag_progress_hook)
  80. return start
  81. def _finish_frag_download(self, ctx):
  82. ctx['dest_stream'].close()
  83. elapsed = time.time() - ctx['started']
  84. self.try_rename(ctx['tmpfilename'], ctx['filename'])
  85. fsize = os.path.getsize(encodeFilename(ctx['filename']))
  86. self._hook_progress({
  87. 'downloaded_bytes': fsize,
  88. 'total_bytes': fsize,
  89. 'filename': ctx['filename'],
  90. 'status': 'finished',
  91. 'elapsed': elapsed,
  92. })