From 90f7e0df9d4d7c27b8886d3699b3342914c27eea Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Sun, 1 Dec 2024 16:39:42 -0500 Subject: [PATCH] Better error handling and logging --- scripts/process-videos.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/scripts/process-videos.py b/scripts/process-videos.py index 65c7721..fc01517 100755 --- a/scripts/process-videos.py +++ b/scripts/process-videos.py @@ -291,23 +291,33 @@ def run_ffmpeg_with_output(self, cmd: List[str], prefix: str = "") -> None: stderr=subprocess.PIPE, universal_newlines=True, bufsize=1, + env={**os.environ, "PYTHONUNBUFFERED": "1"}, ) - def handle_output(stream, prefix): - for line in stream: - # Skip empty lines - if line.strip(): - # Add prefix to each line for identification - print(f"{prefix}{line}", end="", flush=True) + def handle_output(stream, prefix, stream_name): + try: + while True: + line = stream.readline() + if not line: + print(f"{prefix}[{stream_name}] stream ended") + break + if line.strip(): + print(f"{prefix}{line}", end="", flush=True) + except Exception as e: + print(f"{prefix}Error in {stream_name} handler: {str(e)}") # Create threads to handle stdout and stderr from threading import Thread stdout_thread = Thread( - target=handle_output, args=(process.stdout, f"[{prefix}] ") + target=handle_output, + args=(process.stdout, f"[{prefix}] ", "stdout"), + daemon=True, ) stderr_thread = Thread( - target=handle_output, args=(process.stderr, f"[{prefix}] ") + target=handle_output, + args=(process.stderr, f"[{prefix}] ", "stderr"), + daemon=True, ) # Start threads