Skip to content

Commit

Permalink
Better error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Dec 1, 2024
1 parent 59bf7b5 commit 90f7e0d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions scripts/process-videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 90f7e0d

Please sign in to comment.