mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-10 01:11:25 +01:00
Compare commits
8 Commits
8fc1d89d9e
...
4786c58400
Author | SHA1 | Date | |
---|---|---|---|
|
4786c58400 | ||
|
bc71b66d98 | ||
|
62ebe762f3 | ||
|
1e8fd3836a | ||
|
89a6f895bf | ||
|
fd8192c08c | ||
|
0b72b88169 | ||
|
dd7b58fbc1 |
|
@ -274,8 +274,8 @@ class FragmentFD(FileDownloader):
|
||||||
progress.thread_reset()
|
progress.thread_reset()
|
||||||
|
|
||||||
state['downloaded_bytes'] = ctx['complete_frags_downloaded_bytes'] = progress.downloaded
|
state['downloaded_bytes'] = ctx['complete_frags_downloaded_bytes'] = progress.downloaded
|
||||||
ctx['speed'] = state['speed'] = progress.smooth_speed
|
state['speed'] = ctx['speed'] = progress.speed.smooth
|
||||||
state['eta'] = progress.eta
|
state['eta'] = progress.eta.smooth
|
||||||
|
|
||||||
self._hook_progress(state, info_dict)
|
self._hook_progress(state, info_dict)
|
||||||
|
|
||||||
|
|
|
@ -6,21 +6,24 @@ import time
|
||||||
|
|
||||||
|
|
||||||
class ProgressCalculator:
|
class ProgressCalculator:
|
||||||
# Time to calculate the speed over (in nanoseconds)
|
# Time to calculate the speed over (seconds)
|
||||||
SAMPLING_WINDOW = 1_000_000_000
|
SAMPLING_WINDOW = 3
|
||||||
# Factor for the exponential moving average (from 0 = prev to 1 = current)
|
# Minimum timeframe before to sample next downloaded bytes (seconds)
|
||||||
SMOOTHING_FACTOR = 0.3
|
SAMPLING_RATE = 0.05
|
||||||
|
# Time before showing eta (seconds)
|
||||||
|
GRACE_PERIOD = 1
|
||||||
|
|
||||||
def __init__(self, initial: int):
|
def __init__(self, initial: int):
|
||||||
self.downloaded = initial or 0
|
self._initial = initial or 0
|
||||||
|
self.downloaded = self._initial
|
||||||
|
|
||||||
self.elapsed: float = 0
|
self.elapsed: float = 0
|
||||||
self.speed: float = 0
|
self.speed = SmoothValue(0, smoothing=0.7)
|
||||||
self.smooth_speed: int = 0
|
self.eta = SmoothValue(None, smoothing=0.9)
|
||||||
self.eta: float | None = None
|
|
||||||
|
|
||||||
self._total = None
|
self._total = 0
|
||||||
self._start_time = time.monotonic_ns()
|
self._start_time = time.monotonic()
|
||||||
|
self._last_update = self._start_time
|
||||||
|
|
||||||
self._lock = threading.Lock()
|
self._lock = threading.Lock()
|
||||||
self._thread_sizes: dict[int, int] = {}
|
self._thread_sizes: dict[int, int] = {}
|
||||||
|
@ -35,9 +38,7 @@ class ProgressCalculator:
|
||||||
@total.setter
|
@total.setter
|
||||||
def total(self, value: int | None):
|
def total(self, value: int | None):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if not value:
|
if value is not None and value < self.downloaded:
|
||||||
value = None
|
|
||||||
elif value < self.downloaded:
|
|
||||||
value = self.downloaded
|
value = self.downloaded
|
||||||
|
|
||||||
self._total = value
|
self._total = value
|
||||||
|
@ -59,29 +60,50 @@ class ProgressCalculator:
|
||||||
self._update(size - last_size)
|
self._update(size - last_size)
|
||||||
|
|
||||||
def _update(self, size: int):
|
def _update(self, size: int):
|
||||||
current_time = time.monotonic_ns()
|
current_time = time.monotonic()
|
||||||
|
|
||||||
self.downloaded += size
|
self.downloaded += size
|
||||||
self.elapsed = (current_time - self._start_time) / 1_000_000_000
|
self.elapsed = current_time - self._start_time
|
||||||
if self.total is not None and self.downloaded > self.total:
|
if self.total is not None and self.downloaded > self.total:
|
||||||
self._total = self.downloaded
|
self._total = self.downloaded
|
||||||
|
|
||||||
|
if self._last_update + self.SAMPLING_RATE > current_time:
|
||||||
|
return
|
||||||
|
self._last_update = current_time
|
||||||
|
|
||||||
self._times.append(current_time)
|
self._times.append(current_time)
|
||||||
self._downloaded.append(self.downloaded)
|
self._downloaded.append(self.downloaded)
|
||||||
|
|
||||||
offset = bisect.bisect_left(self._times, current_time - self.SAMPLING_WINDOW)
|
offset = bisect.bisect_left(self._times, current_time - self.SAMPLING_WINDOW)
|
||||||
del self._times[:offset]
|
del self._times[:offset]
|
||||||
del self._downloaded[:offset]
|
del self._downloaded[:offset]
|
||||||
|
if len(self._times) < 2:
|
||||||
|
self.speed.reset()
|
||||||
|
self.eta.reset()
|
||||||
|
return
|
||||||
|
|
||||||
download_time = current_time - self._times[0]
|
download_time = current_time - self._times[0]
|
||||||
if not download_time:
|
if not download_time:
|
||||||
return
|
return
|
||||||
downloaded_bytes = self.downloaded - self._downloaded[0]
|
|
||||||
|
|
||||||
self.speed = downloaded_bytes * 1_000_000_000 / download_time
|
self.speed.set((self.downloaded - self._downloaded[0]) / download_time)
|
||||||
self.smooth_speed = int(self.SMOOTHING_FACTOR * self.speed + (1 - self.SMOOTHING_FACTOR) * self.smooth_speed)
|
if self.total and self.speed.value and self.elapsed > self.GRACE_PERIOD:
|
||||||
|
self.eta.set((self.total - self.downloaded) / self.speed.value)
|
||||||
|
else:
|
||||||
|
self.eta.reset()
|
||||||
|
|
||||||
if not self.total:
|
|
||||||
self.eta = None
|
class SmoothValue:
|
||||||
elif self.speed:
|
def __init__(self, initial: float | None, smoothing: float):
|
||||||
self.eta = (self.total - self.downloaded) / self.speed
|
self.value = self.smooth = self._initial = initial
|
||||||
|
self._smoothing = smoothing
|
||||||
|
|
||||||
|
def set(self, value: float):
|
||||||
|
self.value = value
|
||||||
|
if self.smooth is None:
|
||||||
|
self.smooth = self.value
|
||||||
|
else:
|
||||||
|
self.smooth = (1 - self._smoothing) * value + self._smoothing * self.smooth
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.value = self.smooth = self._initial
|
||||||
|
|
Loading…
Reference in New Issue
Block a user