diff --git a/bin/vcmp b/bin/vcmp index 4fd822e..16fff16 100755 --- a/bin/vcmp +++ b/bin/vcmp @@ -8,11 +8,32 @@ import argparse import tempfile import logging import time +import psutil video_extensions: list[str] = ["mp4", "mkv", "avi", "wmv", "mov", "m4v", "ts", "flv", "mpg"] total_files: int = 0 +def is_system_busy() -> bool: + """Returns true if the cpu is doing something else. maybe.""" + total_cpu = 0 + our_pids = [os.getpid()] + try: + # add child processes + our_pids.extend(p.pid for p in psutil.Process().children(recursive=True)) + except: + pass + + for proc in psutil.process_iter(['cpu_percent', 'pid']): + if proc.info['pid'] not in our_pids: + total_cpu += proc.info['cpu_percent'] + #print(total_cpu) + + cpu_count = psutil.cpu_count() + if cpu_count is None: + cpu_count = 1 + return total_cpu > 50 * cpu_count # TODO: Parameter + def acquire_file_lock(filepath: pathlib.Path) -> bool: lock_file = filepath.with_suffix(filepath.suffix + '.lock') try: @@ -105,7 +126,7 @@ def get_video_duration(input_file: pathlib.Path) -> float: logging.error(f"Error getting duration for {input_file}") return 1.0 -def run_progress(process, duration, file_index=0): +def run_progress(process, duration, file_index=0) -> bool: current_time = None last_update = 0 progress = 0 @@ -128,6 +149,8 @@ def run_progress(process, duration, file_index=0): elapsed = time.time() - start_time 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( f"\rProcessing ({file_index}/{total_files} [{total_progress:.1f}%]) :" @@ -140,6 +163,7 @@ def run_progress(process, duration, file_index=0): last_update = time.time() except: pass + return True def run(filepath: pathlib.Path, cmd: list[str], temp_path: pathlib.Path, file_index: int) -> None: duration = get_video_duration(filepath) @@ -154,7 +178,12 @@ def run(filepath: pathlib.Path, cmd: list[str], temp_path: pathlib.Path, file_in stderr=log_file, universal_newlines=True ) - run_progress(process, duration, file_index) + if not run_progress(process, duration, file_index): + process.kill() + release_file_lock(filepath) + if temp_path.exists(): + temp_path.unlink() + sys.exit(0) sys.stdout.write("\n") sys.stdout.flush() @@ -165,6 +194,7 @@ def run(filepath: pathlib.Path, cmd: list[str], temp_path: pathlib.Path, file_in logging.error(f"FFmpeg exited with code {process.returncode}") if temp_path.exists(): temp_path.unlink() + release_file_lock(filepath) raise subprocess.CalledProcessError(process.returncode, cmd) def process_single_file(