1
0

tqdm bar on interactive shell

This commit is contained in:
2025-01-26 19:40:24 +01:00
parent 776c5932c1
commit c06eeda4f2
+12 -13
View File
@@ -9,6 +9,7 @@ import tempfile
import logging
import time
import psutil
from tqdm import tqdm
video_extensions: list[str] = ["mp4", "mkv", "avi", "wmv", "mov", "m4v", "ts", "flv", "mpg"]
@@ -136,36 +137,34 @@ def run_progress(process, duration, file_index=0) -> bool:
UPDATE_INTERVAL = 10 # TODO: Parameter
start_time = time.time()
is_interactive = sys.stdout.isatty()
if is_interactive:
pbar = None
last_time = 0
UPDATE_INTERVAL = 1
pbar = tqdm(total=duration, unit='s', bar_format='{l_bar}{bar}| {n:.2f}{unit}/{total:.2f}{unit} [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}')
last_time = 0
while True:
line = process.stdout.readline() if process.stdout else None
# Check if we've reached the end of output (no more lines to read)
# and if the process has finished (poll() returns exit code instead of None)
if not line and process.poll() is not None:
if pbar:
pbar.close()
break
if line and line.startswith('out_time_ms='):
try:
if time.time() - last_update > UPDATE_INTERVAL:
if is_system_busy():
if pbar:
pbar.close()
return False
if is_interactive:
if is_interactive and pbar:
time_str = line.split('=')[1].strip()
current_time = float(time_str) / 1000000
current_time = min(current_time, duration)
progress = (current_time / duration) * 100
elapsed = time.time() - start_time
speed = current_time / elapsed if elapsed > 0 else 0
sys.stdout.write(
f"\rProcessing:"
f"{progress:5.1f}% "
f"({current_time:.1f} / {duration:.1f} seconds) "
f"[{speed:.2f}x]"
f"{'.' * int(progress / 5)}{' ' * (20 - int(progress / 5))}"
)
sys.stdout.flush()
pbar.update(current_time - last_time)
last_time = current_time
last_update = time.time()
except:
pass