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.

104 lines
3.3 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') or 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. state['eta'] = self.calc_eta(
  70. start, time_now, estimated_size,
  71. state['downloaded_bytes'] + s['downloaded_bytes'])
  72. state['speed'] = s.get('speed')
  73. self._hook_progress(state)
  74. ctx['dl'].add_progress_hook(frag_progress_hook)
  75. return start
  76. def _finish_frag_download(self, ctx):
  77. ctx['dest_stream'].close()
  78. elapsed = time.time() - ctx['started']
  79. self.try_rename(ctx['tmpfilename'], ctx['filename'])
  80. fsize = os.path.getsize(encodeFilename(ctx['filename']))
  81. self._hook_progress({
  82. 'downloaded_bytes': fsize,
  83. 'total_bytes': fsize,
  84. 'filename': ctx['filename'],
  85. 'status': 'finished',
  86. 'elapsed': elapsed,
  87. })