tqdm bar on interactive shell
This commit is contained in:
@@ -9,6 +9,7 @@ import tempfile
|
|||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
import psutil
|
import psutil
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
video_extensions: list[str] = ["mp4", "mkv", "avi", "wmv", "mov", "m4v", "ts", "flv", "mpg"]
|
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
|
UPDATE_INTERVAL = 10 # TODO: Parameter
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
is_interactive = sys.stdout.isatty()
|
is_interactive = sys.stdout.isatty()
|
||||||
if is_interactive:
|
pbar = None
|
||||||
|
last_time = 0
|
||||||
UPDATE_INTERVAL = 1
|
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:
|
while True:
|
||||||
line = process.stdout.readline() if process.stdout else None
|
line = process.stdout.readline() if process.stdout else None
|
||||||
|
|
||||||
# Check if we've reached the end of output (no more lines to read)
|
# 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)
|
# and if the process has finished (poll() returns exit code instead of None)
|
||||||
if not line and process.poll() is not None:
|
if not line and process.poll() is not None:
|
||||||
|
if pbar:
|
||||||
|
pbar.close()
|
||||||
break
|
break
|
||||||
|
|
||||||
if line and line.startswith('out_time_ms='):
|
if line and line.startswith('out_time_ms='):
|
||||||
try:
|
try:
|
||||||
if time.time() - last_update > UPDATE_INTERVAL:
|
if time.time() - last_update > UPDATE_INTERVAL:
|
||||||
if is_system_busy():
|
if is_system_busy():
|
||||||
|
if pbar:
|
||||||
|
pbar.close()
|
||||||
return False
|
return False
|
||||||
if is_interactive:
|
if is_interactive and pbar:
|
||||||
time_str = line.split('=')[1].strip()
|
time_str = line.split('=')[1].strip()
|
||||||
current_time = float(time_str) / 1000000
|
current_time = float(time_str) / 1000000
|
||||||
current_time = min(current_time, duration)
|
current_time = min(current_time, duration)
|
||||||
progress = (current_time / duration) * 100
|
pbar.update(current_time - last_time)
|
||||||
elapsed = time.time() - start_time
|
last_time = current_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()
|
|
||||||
last_update = time.time()
|
last_update = time.time()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user