Better progress and update checks
This commit is contained in:
@@ -16,6 +16,10 @@ total_files: int = 0
|
|||||||
|
|
||||||
def is_system_busy() -> bool:
|
def is_system_busy() -> bool:
|
||||||
"""Returns true if the cpu is doing something else. maybe."""
|
"""Returns true if the cpu is doing something else. maybe."""
|
||||||
|
# First call all processes, so we can recheck them later.
|
||||||
|
# See: https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent
|
||||||
|
for proc in psutil.process_iter(['cpu_percent', 'pid']):
|
||||||
|
pass
|
||||||
total_cpu = 0
|
total_cpu = 0
|
||||||
our_pids = [os.getpid()]
|
our_pids = [os.getpid()]
|
||||||
try:
|
try:
|
||||||
@@ -27,7 +31,6 @@ def is_system_busy() -> bool:
|
|||||||
for proc in psutil.process_iter(['cpu_percent', 'pid']):
|
for proc in psutil.process_iter(['cpu_percent', 'pid']):
|
||||||
if proc.info['pid'] not in our_pids:
|
if proc.info['pid'] not in our_pids:
|
||||||
total_cpu += proc.info['cpu_percent']
|
total_cpu += proc.info['cpu_percent']
|
||||||
#print(total_cpu)
|
|
||||||
|
|
||||||
cpu_count = psutil.cpu_count()
|
cpu_count = psutil.cpu_count()
|
||||||
if cpu_count is None:
|
if cpu_count is None:
|
||||||
@@ -130,8 +133,11 @@ def run_progress(process, duration, file_index=0) -> bool:
|
|||||||
current_time = None
|
current_time = None
|
||||||
last_update = 0
|
last_update = 0
|
||||||
progress = 0
|
progress = 0
|
||||||
UPDATE_INTERVAL = 1
|
UPDATE_INTERVAL = 10 # TODO: Parameter
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
is_interactive = sys.stdout.isatty()
|
||||||
|
if is_interactive:
|
||||||
|
UPDATE_INTERVAL = 1
|
||||||
while True:
|
while True:
|
||||||
line = process.stdout.readline() if process.stdout else None
|
line = process.stdout.readline() if process.stdout else None
|
||||||
|
|
||||||
@@ -142,18 +148,18 @@ def run_progress(process, duration, file_index=0) -> bool:
|
|||||||
|
|
||||||
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 is_system_busy():
|
||||||
|
return False
|
||||||
|
if is_interactive:
|
||||||
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
|
progress = (current_time / duration) * 100
|
||||||
elapsed = time.time() - start_time
|
elapsed = time.time() - start_time
|
||||||
speed = current_time / elapsed if elapsed > 0 else 0
|
speed = current_time / elapsed if elapsed > 0 else 0
|
||||||
if time.time() - last_update > UPDATE_INTERVAL:
|
|
||||||
if is_system_busy():
|
|
||||||
return False
|
|
||||||
total_progress = (file_index / total_files) * 100
|
|
||||||
sys.stdout.write(
|
sys.stdout.write(
|
||||||
f"\rProcessing ({file_index}/{total_files} [{total_progress:.1f}%]) :"
|
f"\rProcessing:"
|
||||||
f"{progress:5.1f}% "
|
f"{progress:5.1f}% "
|
||||||
f"({current_time:.1f} / {duration:.1f} seconds) "
|
f"({current_time:.1f} / {duration:.1f} seconds) "
|
||||||
f"[{speed:.2f}x]"
|
f"[{speed:.2f}x]"
|
||||||
@@ -211,7 +217,7 @@ def process_single_file(
|
|||||||
|
|
||||||
temp_path = temp_dir / filepath.with_suffix(".mp4").name
|
temp_path = temp_dir / filepath.with_suffix(".mp4").name
|
||||||
clean_directory(temp_dir) # Should be empty if mktemp was used, but clear it regardless.
|
clean_directory(temp_dir) # Should be empty if mktemp was used, but clear it regardless.
|
||||||
logging.info(f"Processing {filepath} to {target_path}")
|
logging.info(f"Processing ({file_index}/{total_files}): {filepath} to {target_path} ")
|
||||||
logging.debug(f"Using temporary file: {temp_path}")
|
logging.debug(f"Using temporary file: {temp_path}")
|
||||||
|
|
||||||
cmd = ffmpeg_command(filepath, temp_path, nice)
|
cmd = ffmpeg_command(filepath, temp_path, nice)
|
||||||
|
|||||||
Reference in New Issue
Block a user