Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.
Python ai-toolsauto-captionscaptionscliffmpegmcpmcp-serveropusclip-alternativepodcast-clipspodcastsshortform-videovertical-videowhisper
backend: migrate 35 subprocess.run call sites to proc.run
Nika Siradze00f83779c152
diff --git a/backend/cli.py b/backend/cli.pyindex 611289d..bed562e 100644--- a/backend/cli.py+++ b/backend/cli.py@@ -1985,14 +1985,14 @@ def cmd_swap_thumbnail(args):# Trim off old thumbnailtrimmed_path = os.path.join(work_dir, "trimmed.mp4")- import subprocess+ from utils.proc import run as proc_runtrim_cmd = ["ffmpeg", "-y", "-i", clip_path,"-t", str(content_duration),"-c", "copy", "-movflags", "+faststart",trimmed_path,]- result = subprocess.run(trim_cmd, capture_output=True, text=True, timeout=60)+ result = proc_run(trim_cmd, timeout=60, check=False)if result.returncode != 0:print(f" Failed to trim: {result.stderr[-200:]}", file=sys.stderr)sys.exit(1)diff --git a/backend/config/caption_styles.py b/backend/config/caption_styles.pyindex 230ee02..e0ae80b 100644--- a/backend/config/caption_styles.py+++ b/backend/config/caption_styles.py@@ -6,6 +6,7 @@ supported caption types."""import subprocess+from utils.proc import run as proc_runimport functools@@ -27,9 +28,9 @@ def _detect_font() -> str:]try:- result = subprocess.run(+ result = proc_run(["fc-list", "--format=%{family}\n"],- capture_output=True, text=True, timeout=5,+ timeout=5, check=False,)if result.returncode == 0:available = set()diff --git a/backend/services/audio_analyzer.py b/backend/services/audio_analyzer.pyindex 097376f..9edb0d3 100644--- a/backend/services/audio_analyzer.py+++ b/backend/services/audio_analyzer.py@@ -11,6 +11,8 @@ import jsonimport refrom typing import Optional, Callable+from utils.proc import run as proc_run+def extract_audio_energy(video_path: str,@@ -32,7 +34,7 @@ def extract_audio_energy("-f", "null", "-",]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=600)+ result = proc_run(cmd, timeout=600, check=False)# Parse the ametadata output from stdout# Format: "frame:N pts:N pts_time:N.N\nlavfi.astats.Overall.RMS_level=N.N"@@ -72,7 +74,7 @@ def _fallback_energy(video_path: str) -> list[dict]:"ffprobe", "-v", "quiet", "-print_format", "json","-show_format", video_path,]- probe = subprocess.run(probe_cmd, capture_output=True, text=True)+ probe = proc_run(probe_cmd, timeout=30, check=False)try:duration = float(json.loads(probe.stdout)["format"]["duration"])except (json.JSONDecodeError, KeyError):@@ -86,7 +88,7 @@ def _fallback_energy(video_path: str) -> list[dict]:"-af", "ebur128=peak=true","-f", "null", "-",]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=600)+ result = proc_run(cmd, timeout=600, check=False)energy_data = []# Parse ebur128 output from stderr: "t: N.N M: -N.N S: -N.N"diff --git a/backend/services/caption_renderer.py b/backend/services/caption_renderer.pyindex 052f8eb..940ed5a 100644--- a/backend/services/caption_renderer.py+++ b/backend/services/caption_renderer.py@@ -266,8 +266,8 @@ def _calibrate_libass_y(Render a probe frame with FFmpeg+libass to measure the exact Y positionof each text line. Returns a list of Y-center values (one per line)."""- import subprocessimport tempfile+ from utils.proc import run as proc_runcache_key = f"{font_name}:{font_size}:{bold}:{margin_v}:{len(lines)}"if cache_key in _libass_calibration_cache:@@ -312,7 +312,7 @@ def _calibrate_libass_y("-vf", f"ass='{ass_file.name}'","-frames:v", "1", png_file.name,]- subprocess.run(cmd, capture_output=True, text=True, timeout=10)+ proc_run(cmd, timeout=10, check=False)# Analyze the rendered frame to find text line Y centersfrom PIL import Image@@ -380,16 +380,16 @@ def _measure_text_widths(texts: list[str], font_name: str, font_size: int, bold:"""try:from PIL import ImageFont, ImageDraw, Image- import subprocess+ from utils.proc import run as proc_runfont = None# 1) Use fc-match to find the exact font libass resolves (most accurate)try:style = "Bold" if bold else "Regular"- result = subprocess.run(+ result = proc_run(["fc-match", f"{font_name}:{style}", "--format=%{file}"],- capture_output=True, text=True, timeout=3,+ timeout=3, check=False,)if result.returncode == 0 and result.stdout.strip():fc_path = result.stdout.strip()diff --git a/backend/services/claude_suggest.py b/backend/services/claude_suggest.pyindex 683b96e..483302a 100644--- a/backend/services/claude_suggest.py+++ b/backend/services/claude_suggest.py@@ -25,7 +25,8 @@ def _find_cli(name: str, extra_paths: list[str] = None) -> Optional[str]:if os.path.exists(path):return pathtry:- result = subprocess.run(["which", name], capture_output=True, text=True, timeout=5)+ from utils.proc import run as proc_run+ result = proc_run(["which", name], timeout=5, check=False)if result.returncode == 0 and result.stdout.strip():return result.stdout.strip()except Exception:diff --git a/backend/services/clip_generator.py b/backend/services/clip_generator.pyindex b8c7924..dbab0bb 100644--- a/backend/services/clip_generator.py+++ b/backend/services/clip_generator.py@@ -13,6 +13,8 @@ import subprocessimport refrom typing import Optional, Callable+from utils.proc import run as proc_run, ProcError+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))from services.caption_renderer import render_captions@@ -113,7 +115,7 @@ def _get_media_duration(path: str) -> float:"-of", "default=nk=1:nw=1",path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)+ result = proc_run(cmd, timeout=10, check=False)if result.returncode != 0:return 0.0return float((result.stdout or "0").strip() or 0.0)@@ -134,7 +136,7 @@ def _detect_scene_cuts(path: str, threshold: float = 0.22, max_cuts: int = 32) -"-an","-f", "null", "-",]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=90)+ result = proc_run(cmd, timeout=90, check=False)text = (result.stderr or "") + "\n" + (result.stdout or "")raw = [float(m.group(1)) for m in _SCENE_TIME_RE.finditer(text)]if not raw:@@ -221,7 +223,7 @@ def _apply_local_transition_smoothing(output_path,]try:- result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)+ result = proc_run(cmd, timeout=300, check=False)return result.returncode == 0 and os.path.exists(output_path)except Exception:return False@@ -430,9 +432,9 @@ def _render_with_remotion(if not os.path.exists(bundle_index):# Try a quick prebundletry:- r = subprocess.run(+ r = proc_run([node_path, render_script, "--prebundle"],- capture_output=True, text=True, timeout=30, cwd=project_root,+ timeout=30, check=False, cwd=project_root,)if r.returncode != 0 or not os.path.exists(bundle_index):_remotion_available = False@@ -457,7 +459,7 @@ def _render_with_remotion(probe_cmd = ["ffprobe", "-v", "error", "-select_streams", "v:0","-show_entries", "stream=height", "-of", "csv=p=0",os.path.abspath(video_path)]- vid_h = int(subprocess.run(probe_cmd, capture_output=True, text=True, timeout=5).stdout.strip())+ vid_h = int(proc_run(probe_cmd, timeout=5, check=False).stdout.strip())# Get face center Y from the cropped video's face detection# Use a quick sample at the middle of the clipdiff --git a/backend/services/encoder.py b/backend/services/encoder.pyindex 570b158..673c340 100644--- a/backend/services/encoder.py+++ b/backend/services/encoder.py@@ -11,6 +11,7 @@ Returns optimal FFmpeg encoder flags for the current system."""import subprocess+from utils.proc import run as proc_run, ProcErrorimport platformimport tempfileimport os@@ -82,12 +83,10 @@ def _test_encoder(encoder: str) -> bool:"-shortest",tmp_out,]- result = subprocess.run(- cmd, capture_output=True, text=True, timeout=15,- )+ result = proc_run(cmd, timeout=15, check=False)# Check both return code AND that the file was actually createdreturn result.returncode == 0 and os.path.exists(tmp_out) and os.path.getsize(tmp_out) > 100- except (subprocess.TimeoutExpired, FileNotFoundError, OSError):+ except (ProcError, FileNotFoundError, OSError):return Falsefinally:if tmp_out and os.path.exists(tmp_out):diff --git a/backend/services/speaker_detection.py b/backend/services/speaker_detection.pyindex 4e91e34..57848f2 100644--- a/backend/services/speaker_detection.py+++ b/backend/services/speaker_detection.py@@ -13,6 +13,8 @@ import subprocessimport tempfilefrom typing import Optional, Callable+from utils.proc import run as proc_run, ProcError+# Fix torchaudio compatibility — speechbrain calls torchaudio.list_audio_backends()# which was removed in torchaudio >= 2.10. Monkey-patch before any import.try:@@ -39,7 +41,7 @@ def extract_audio_wav(video_path: str, output_path: str) -> str:"-ac", "1", # Monooutput_path,]- result = subprocess.run(cmd, capture_output=True, text=True)+ result = proc_run(cmd, timeout=600, check=False)if result.returncode != 0:raise RuntimeError(f"Audio extraction failed: {result.stderr[-300:]}")return output_pathdiff --git a/backend/services/thumbnail_ai.py b/backend/services/thumbnail_ai.pyindex 862b6be..da53168 100644--- a/backend/services/thumbnail_ai.py+++ b/backend/services/thumbnail_ai.py@@ -12,6 +12,7 @@ The config provides brand constraints (accent color, logo, font), Claude works wimport jsonimport osimport subprocess+from utils.proc import run as proc_runimport sysimport tempfileimport base64@@ -673,7 +674,7 @@ def thumbnail_to_video_frame("-shortest",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True)+ result = proc_run(cmd, timeout=120, check=False)if result.returncode != 0:raise RuntimeError(f"Failed: {result.stderr[-300:]}")return output_pathdiff --git a/backend/services/thumbnail_generator.py b/backend/services/thumbnail_generator.pyindex b54b396..049029d 100644--- a/backend/services/thumbnail_generator.py+++ b/backend/services/thumbnail_generator.py@@ -725,7 +725,7 @@ def thumbnail_to_video_frame(Used to append thumbnail as the last 2 seconds of a short video."""- import subprocess+ from utils.proc import run as proc_runcmd = ["ffmpeg", "-y",@@ -744,7 +744,7 @@ def thumbnail_to_video_frame(output_path,]- result = subprocess.run(cmd, capture_output=True, text=True)+ result = proc_run(cmd, timeout=120, check=False)if result.returncode != 0:raise RuntimeError(f"Thumbnail to video failed: {result.stderr[-300:]}")return output_pathdiff --git a/backend/services/thumbnail_html.py b/backend/services/thumbnail_html.pyindex 4ddf09f..e10a502 100644--- a/backend/services/thumbnail_html.py+++ b/backend/services/thumbnail_html.py@@ -13,6 +13,7 @@ import osimport reimport shutilimport subprocess+from utils.proc import run as proc_run, ProcErrorimport sysimport tempfilefrom typing import Optional@@ -744,7 +745,7 @@ def thumbnail_to_video_frame("-shortest",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True)+ result = proc_run(cmd, timeout=120, check=False)if result.returncode != 0:raise RuntimeError(f"Thumbnail to video failed: {result.stderr[-300:]}")return output_pathdiff --git a/backend/services/video_processor.py b/backend/services/video_processor.pyindex c36aac6..1df2f7d 100644--- a/backend/services/video_processor.py+++ b/backend/services/video_processor.py@@ -12,6 +12,7 @@ import mathfrom typing import Optionalfrom services.encoder import get_video_encode_flags+from utils.proc import run as proc_run, ProcErrorimport sys# Max time for any single FFmpeg call (seconds). Prevents infinite hangs.@@ -39,7 +40,7 @@ def _run_ffmpeg_with_fallback(cmd_parts_before_enc: list, cmd_parts_after_enc: lenc_flags = get_video_encode_flags()cmd = cmd_parts_before_enc + enc_flags + cmd_parts_after_enc + [output_path]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode == 0:return output_path@@ -47,7 +48,7 @@ def _run_ffmpeg_with_fallback(cmd_parts_before_enc: list, cmd_parts_after_enc: lif enc_flags != CPU_FLAGS:print(f"Warning: HW encoder failed for {label}, falling back to libx264", file=sys.stderr)cmd_fallback = cmd_parts_before_enc + CPU_FLAGS + cmd_parts_after_enc + [output_path]- result2 = subprocess.run(cmd_fallback, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result2 = proc_run(cmd_fallback, timeout=_FFMPEG_TIMEOUT, check=False)if result2.returncode == 0:return output_pathraise RuntimeError(f"FFmpeg {label} failed (both HW and CPU): {result2.stderr[-500:]}")@@ -74,7 +75,7 @@ def get_video_info(video_path: str) -> dict:"-show_streams",video_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode != 0:raise RuntimeError(f"ffprobe failed: {result.stderr}")return json.loads(result.stdout)@@ -146,7 +147,7 @@ def cut_segment("-avoid_negative_ts", "make_zero",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode != 0:raise RuntimeError(f"FFmpeg cut failed: {result.stderr[-500:]}")return output_path@@ -194,7 +195,7 @@ def cut_multi_segment("-movflags", "+faststart",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode != 0:raise RuntimeError(f"FFmpeg concat failed: {result.stderr[-500:]}")@@ -1646,7 +1647,7 @@ def _track_and_crop("-avoid_negative_ts", "make_zero",part_path,]- r = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ r = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if r.returncode != 0:continuepart_paths.append(part_path)@@ -1678,7 +1679,7 @@ def _track_and_crop("-movflags", "+faststart",output_path,]- r = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ r = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if r.returncode == 0:return output_pathelse:@@ -1717,7 +1718,7 @@ def _track_and_crop("-movflags", "+faststart",output_path,]- r = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ r = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if r.returncode == 0:return output_path@@ -2203,7 +2204,7 @@ def _crop_split_screen("-avoid_negative_ts", "make_zero",part_path,]- r = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ r = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if r.returncode != 0:print(f"Warning: split-screen segment {i} failed: {r.stderr[-200:]}", file=sys.stderr)continue@@ -2226,7 +2227,7 @@ def _crop_split_screen("-movflags", "+faststart",output_path,]- r = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ r = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if r.returncode != 0:return None@@ -2889,7 +2890,7 @@ def _create_gradient_png(output_path: str, width: int = 1080, height: int = 1920"-frames:v", "1",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode != 0:raise RuntimeError(f"Gradient creation failed: {result.stderr[-300:]}")return output_path@@ -2976,7 +2977,7 @@ def burn_captions(output_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)# Fallback to CPU if HW encoder failedif result.returncode != 0 and enc_flags != CPU_FLAGS:@@ -2991,7 +2992,7 @@ def burn_captions("-movflags", "+faststart",output_path,]- result = subprocess.run(cmd_fallback, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd_fallback, timeout=_FFMPEG_TIMEOUT, check=False)# Clean up gradient fileif gradient_overlay and os.path.exists(gradient_path):@@ -3077,7 +3078,7 @@ def concat_outro("-movflags", "+faststart",output_path,]- result = subprocess.run(xfade_cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(xfade_cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode == 0:if os.path.exists(outro_scaled):os.remove(outro_scaled)@@ -3147,7 +3148,7 @@ def concat_outro("-movflags", "+faststart",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode != 0:raise RuntimeError(f"FFmpeg concat failed: {result.stderr[-500:]}")return output_path@@ -3173,7 +3174,7 @@ def normalize_audio("-af", f"loudnorm=I={target_lufs}:TP=-1.5:LRA=11:print_format=json","-f", "null", "-",]- result = subprocess.run(measure_cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(measure_cmd, timeout=_FFMPEG_TIMEOUT, check=False)# Try to parse loudnorm output from stderrstderr = result.stderr@@ -3221,7 +3222,7 @@ def normalize_audio("-movflags", "+faststart",output_path,]- result = subprocess.run(cmd, capture_output=True, text=True, timeout=_FFMPEG_TIMEOUT)+ result = proc_run(cmd, timeout=_FFMPEG_TIMEOUT, check=False)if result.returncode != 0:raise RuntimeError(f"FFmpeg normalize failed: {result.stderr[-500:]}")return output_path