stop on busy system.
Might break multi-processing on same machine. Might fix later.
This commit is contained in:
@@ -8,11 +8,32 @@ import argparse
|
|||||||
import tempfile
|
import tempfile
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
import psutil
|
||||||
|
|
||||||
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"]
|
||||||
|
|
||||||
total_files: int = 0
|
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:
|
def acquire_file_lock(filepath: pathlib.Path) -> bool:
|
||||||
lock_file = filepath.with_suffix(filepath.suffix + '.lock')
|
lock_file = filepath.with_suffix(filepath.suffix + '.lock')
|
||||||
try:
|
try:
|
||||||
@@ -105,7 +126,7 @@ def get_video_duration(input_file: pathlib.Path) -> float:
|
|||||||
logging.error(f"Error getting duration for {input_file}")
|
logging.error(f"Error getting duration for {input_file}")
|
||||||
return 1.0
|
return 1.0
|
||||||
|
|
||||||
def run_progress(process, duration, file_index=0):
|
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
|
||||||
@@ -128,6 +149,8 @@ def run_progress(process, duration, file_index=0):
|
|||||||
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 time.time() - last_update > UPDATE_INTERVAL:
|
||||||
|
if is_system_busy():
|
||||||
|
return False
|
||||||
total_progress = (file_index / total_files) * 100
|
total_progress = (file_index / total_files) * 100
|
||||||
sys.stdout.write(
|
sys.stdout.write(
|
||||||
f"\rProcessing ({file_index}/{total_files} [{total_progress:.1f}%]) :"
|
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()
|
last_update = time.time()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
return True
|
||||||
|
|
||||||
def run(filepath: pathlib.Path, cmd: list[str], temp_path: pathlib.Path, file_index: int) -> None:
|
def run(filepath: pathlib.Path, cmd: list[str], temp_path: pathlib.Path, file_index: int) -> None:
|
||||||
duration = get_video_duration(filepath)
|
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,
|
stderr=log_file,
|
||||||
universal_newlines=True
|
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.write("\n")
|
||||||
sys.stdout.flush()
|
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}")
|
logging.error(f"FFmpeg exited with code {process.returncode}")
|
||||||
if temp_path.exists():
|
if temp_path.exists():
|
||||||
temp_path.unlink()
|
temp_path.unlink()
|
||||||
|
release_file_lock(filepath)
|
||||||
raise subprocess.CalledProcessError(process.returncode, cmd)
|
raise subprocess.CalledProcessError(process.returncode, cmd)
|
||||||
|
|
||||||
def process_single_file(
|
def process_single_file(
|
||||||
|
|||||||
Reference in New Issue
Block a user