diff --git a/bin/vcmp b/bin/vcmp index 6a76918..746c47c 100755 --- a/bin/vcmp +++ b/bin/vcmp @@ -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: - UPDATE_INTERVAL = 1 + 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