Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

Give every encode a pixel format and Remotion a browser (2.7.6) (#177)

Nika Siradzecd815e2ef8a2
diff --git a/backend/services/clip_generator.py b/backend/services/clip_generator.py
index a928b0a..b302e2a 100644
--- a/backend/services/clip_generator.py
+++ b/backend/services/clip_generator.py
@@ -225,6 +225,7 @@ def _apply_local_transition_smoothing(
"-i", input_path,
"-vf", vf,
"-c:v", "libx264", "-crf", "18", "-preset", "medium",
+ "-pix_fmt", "yuv420p",
"-c:a", "copy",
"-movflags", "+faststart",
output_path,
@@ -453,6 +454,29 @@ def _build_tight_segments(
_remotion_available = None # True/False/None — environment availability, not per-clip success
+def _report_remotion_failure(
+ stage: str,
+ stdout: str | bytes | None,
+ stderr: str | bytes | None,
+) -> None:
+ """Print why Remotion gave up, on the stream the worker reads.
+
+ The fallback to burned-in ASS is silent by design and was silent about its
+ cause too, so a render that never once used the caption components the
+ studio previews with looked exactly like one that did.
+
+ Takes bytes as well as text. A killed render reaches this through
+ TimeoutExpired, which carries whatever the child had written and does not
+ honour text mode when it does.
+ """
+ for label, stream in (("out", stdout), ("err", stderr)):
+ if isinstance(stream, bytes):
+ stream = stream.decode("utf-8", "replace")
+ lines = [line.strip() for line in (stream or "").strip().splitlines() if line.strip()]
+ for line in lines[-3:]:
+ print(f" Remotion {stage} {label}: {line[:200]}", file=sys.stderr, flush=True)
+
+
def _kept_caption_overlay_path(output_path: str) -> str:
base, _ = os.path.splitext(os.path.abspath(output_path))
return f"{base}_captions.mov"
@@ -518,9 +542,12 @@ def _render_with_remotion(
)
if r.returncode != 0 or not os.path.exists(bundle_index):
_remotion_available = False
+ _report_remotion_failure("bundling", r.stdout, r.stderr)
return False, None
- except Exception:
+ except Exception as exc:
_remotion_available = False
+ print(f" Remotion bundling: {type(exc).__name__}: {exc}",
+ file=sys.stderr, flush=True)
return False, None
# Prepare words JSON (adjust timestamps by offset)
@@ -611,18 +638,20 @@ def _render_with_remotion(
if keep_caption_overlay:
cmd.append("--keep-overlay")
- # Redirect stderr to devnull to suppress Chrome/FFmpeg noise
- # (avoids buffer deadlock and terminal spam)
- with open(os.devnull, "w") as _devnull:
- result = subprocess.run(
- cmd,
- stdout=subprocess.PIPE,
- stderr=_devnull,
- text=True,
- timeout=600,
- cwd=project_root,
- env=remotion_env,
- )
+ # Both pipes are captured. subprocess.run drains them together, so the
+ # deadlock the old code avoided by pointing stderr at /dev/null could
+ # not happen, and what it threw away was the only description of the
+ # failure. Every clip on the Linux worker fell back to burned-in ASS
+ # for months and the reason went to /dev/null with the rest of it.
+ result = subprocess.run(
+ cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ timeout=600,
+ cwd=project_root,
+ env=remotion_env,
+ )
if result.returncode == 0 and os.path.exists(output_path):
_remotion_available = True
@@ -633,19 +662,19 @@ def _render_with_remotion(
overlay_path = None
return True, overlay_path
- stdout = result.stdout or ""
- if stdout:
- lines = [l.strip() for l in stdout.strip().split("\n") if l.strip()]
- if lines:
- print(f" Remotion: {lines[-1][:120]}", file=sys.stderr, flush=True)
-
+ _report_remotion_failure("render", result.stdout, result.stderr)
print(" Remotion: falling back to ASS for this clip", file=sys.stderr, flush=True)
return False, None
- except subprocess.TimeoutExpired:
+ except subprocess.TimeoutExpired as exc:
+ # Whatever the render said before it was killed, which is the half of a
+ # hang worth having: a browser that never came up looks the same from
+ # out here as a composition that took too long to draw.
+ _report_remotion_failure("render", exc.stdout, exc.stderr)
print(" Remotion: timed out, using ASS for this clip", file=sys.stderr, flush=True)
return False, None
- except Exception:
+ except Exception as exc:
+ print(f" Remotion: {type(exc).__name__}: {exc}", file=sys.stderr, flush=True)
print(" Remotion: render error, using ASS for this clip", file=sys.stderr, flush=True)
return False, None
finally:
diff --git a/backend/services/encoder.py b/backend/services/encoder.py
index 1479ef1..b64091e 100644
--- a/backend/services/encoder.py
+++ b/backend/services/encoder.py
@@ -102,12 +102,20 @@ def _get_encoder_flags(encoder: str) -> list[str]:
"""
Get optimal FFmpeg flags for a given encoder.
Kept minimal to avoid conflicts with filter_complex.
+
+ Every H.264 preset states yuv420p. Left unsaid, FFmpeg picks the output
+ format by negotiating with the filtergraph, and some filters answer with
+ 4:4:4 even when every input is 4:2:0. xfade is one of them. The encoders
+ that ask for high profile then abort, because high is a 4:2:0-only profile,
+ and the ones that do not ask silently write High 4:4:4 Predictive, which
+ phones and every social platform refuse to play.
"""
flags = {
"h264_videotoolbox": [
"-c:v", "h264_videotoolbox",
"-b:v", "6M", # 6 Mbps — plenty for 1080x1920 vertical
"-profile:v", "high",
+ "-pix_fmt", "yuv420p",
"-allow_sw", "1", # Allow software fallback
],
"h264_nvenc": [
@@ -115,6 +123,7 @@ def _get_encoder_flags(encoder: str) -> list[str]:
"-preset", "p6", # Slower = higher quality
"-cq", "18", # High quality
"-profile:v", "high",
+ "-pix_fmt", "yuv420p",
],
"h264_amf": [
"-c:v", "h264_amf",
@@ -136,6 +145,7 @@ def _get_encoder_flags(encoder: str) -> list[str]:
"-crf", "18", # Near-lossless quality
"-preset", "slow", # Better compression at same quality
"-profile:v", "high",
+ "-pix_fmt", "yuv420p",
],
}
return flags.get(encoder, flags["libx264"])
@@ -149,7 +159,8 @@ def get_video_encode_flags() -> list[str]:
except Exception:
# Absolute fallback — never let encoder detection break the pipeline
print("Warning: encoder detection failed, using libx264", file=sys.stderr)
- return ["-c:v", "libx264", "-crf", "18", "-preset", "slow", "-profile:v", "high"]
+ return ["-c:v", "libx264", "-crf", "18", "-preset", "slow",
+ "-profile:v", "high", "-pix_fmt", "yuv420p"]
def _encoder_cache_path() -> str:
@@ -189,7 +200,8 @@ def get_encoder_info() -> dict:
info = detect_encoders()
except Exception:
info = {"available": ["libx264"], "best": "libx264",
- "best_flags": ["-c:v", "libx264", "-crf", "18", "-preset", "slow", "-profile:v", "high"],
+ "best_flags": ["-c:v", "libx264", "-crf", "18", "-preset", "slow",
+ "-profile:v", "high", "-pix_fmt", "yuv420p"],
"system": platform.system()}
try:
diff --git a/backend/services/media_probe.py b/backend/services/media_probe.py
index b84de00..76198f2 100644
--- a/backend/services/media_probe.py
+++ b/backend/services/media_probe.py
@@ -40,6 +40,10 @@ CPU_FLAGS = [
"-crf", _qp["crf"],
"-preset", _qp["preset"],
"-profile:v", "high",
+ # High profile is 4:2:0 only, so leaving the format to negotiation makes
+ # this command fail on any filtergraph that hands it 4:4:4. See
+ # services/encoder.py for the whole story.
+ "-pix_fmt", "yuv420p",
]
diff --git a/backend/services/video_cut.py b/backend/services/video_cut.py
index 5c41ddc..cbe72d9 100644
--- a/backend/services/video_cut.py
+++ b/backend/services/video_cut.py
@@ -38,6 +38,7 @@ def cut_segment(
"-i", input_path,
"-t", str(duration),
"-c:v", "libx264", "-crf", "16", "-preset", "fast", "-profile:v", "high",
+ "-pix_fmt", "yuv420p",
"-c:a", "aac", "-b:a", "192k",
"-avoid_negative_ts", "make_zero",
output_path,
diff --git a/backend/services/video_processor.py b/backend/services/video_processor.py
index 6ed8c2a..da9a5f3 100644
--- a/backend/services/video_processor.py
+++ b/backend/services/video_processor.py
@@ -1563,6 +1563,7 @@ def _track_and_crop_inner(
"-vf", seg_vf,
"-an",
"-c:v", "libx264", "-crf", "18", "-preset", "fast",
+ "-pix_fmt", "yuv420p",
"-avoid_negative_ts", "make_zero",
part_path,
]
@@ -1624,6 +1625,14 @@ def _track_and_crop_inner(
cmd += ["-map", f"{source_index}:a", "-c:a", "aac", "-b:a", "192k"]
cmd += [
"-c:v", "libx264", "-crf", "18", "-preset", "fast",
+ # xfade answers format negotiation with yuv444p even
+ # when both of its inputs are yuv420p, and nothing here
+ # asked for a format, so this wrote High 4:4:4
+ # Predictive. Every later step pins profile high, which
+ # is 4:2:0 only, so the caption burn aborted with
+ # "high profile doesn't support 4:4:4" and the clip was
+ # lost. The whole render exited 0 with nothing in it.
+ "-pix_fmt", "yuv420p",
"-movflags", "+faststart",
# An explicit length, not -shortest. The audio comes
# from a plain input while the video comes out of a
@@ -1660,6 +1669,7 @@ def _track_and_crop_inner(
"-vf", (f"crop={crop_w}:{crop_h}:{_crop_for(r_cx)}:{crop_y},"
f"scale={target_w}:{target_h}"),
"-c:v", "libx264", "-crf", "18", "-preset", "fast",
+ "-pix_fmt", "yuv420p",
"-c:a", "aac", "-b:a", "192k", "-ar", "44100",
"-avoid_negative_ts", "make_zero",
cut_path,
@@ -2176,6 +2186,7 @@ def _crop_split_screen(
"-i", input_path,
"-vf", vf,
"-c:v", "libx264", "-crf", "18", "-preset", "fast",
+ "-pix_fmt", "yuv420p",
"-c:a", "aac", "-b:a", "192k",
"-avoid_negative_ts", "make_zero",
part_path,
diff --git a/cli/VERSION b/cli/VERSION
index a603bb5..49cdd66 100644
--- a/cli/VERSION
+++ b/cli/VERSION
@@ -1 +1 @@
-2.7.5
+2.7.6
diff --git a/package.json b/package.json
index ac014b5..a90929b 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "podcli",
- "version": "2.7.5",
+ "version": "2.7.6",
"private": true,
"description": "AI-powered podcast clip generator for TikTok/YouTube Shorts. Transcribe, find viral moments, export vertical clips with burned captions.",
"type": "module",
diff --git a/remotion/render.mjs b/remotion/render.mjs
index 18578e1..50de9f2 100644
--- a/remotion/render.mjs
+++ b/remotion/render.mjs
@@ -17,7 +17,7 @@
* node remotion/render.mjs --prebundle # Bundle only, no render
*/
-import { renderMedia, selectComposition } from "@remotion/renderer";
+import { ensureBrowser, renderMedia, selectComposition } from "@remotion/renderer";
import { getCachedBundle } from "./bundle-cache.mjs";
import path from "path";
import fs from "fs";
@@ -196,6 +196,22 @@ async function main() {
`Remotion: ${words.length} words, ${styleName}, ${renderW}x${renderH}, ${durationInFrames}f`
);
+ // The browser the captions are drawn in. Left to itself Remotion downloads
+ // one on first use, next to whatever directory the process was started in,
+ // which on a container is a layer and not a volume: recreating it downloads
+ // 120MB again. PODCLI_BROWSER names one the machine already has instead.
+ //
+ // Stated up front rather than left to the render. ensureBrowser fails
+ // immediately and by name when the path is wrong, where the same mistake
+ // inside renderMedia surfaces as a frame that would not draw.
+ const browserExecutable = process.env.PODCLI_BROWSER || null;
+ // headless-shell is Remotion's own build and is driven with --headless=old.
+ // Chrome removed old headless in 132 and Debian trixie ships Chromium 151,
+ // so a browser we were handed rather than downloaded gets the flag that
+ // still exists.
+ const chromeMode = browserExecutable ? "chrome-for-testing" : "headless-shell";
+ await ensureBrowser({ browserExecutable, chromeMode });
+
try {
// Select composition
const composition = await selectComposition({
@@ -203,6 +219,8 @@ async function main() {
id: "CaptionedClip",
inputProps,
timeoutInMilliseconds: DELAY_RENDER_TIMEOUT_MS,
+ browserExecutable,
+ chromeMode,
});
const cpus = os.cpus().length;
@@ -226,6 +244,8 @@ async function main() {
height: renderH,
},
serveUrl: bundleLocation,
+ browserExecutable,
+ chromeMode,
codec: "prores",
proResProfile: "4444",
pixelFormat: "yuva444p10le",
@@ -249,7 +269,7 @@ async function main() {
execSync(
`ffmpeg -y -hide_banner -loglevel warning -i "${path.resolve(opts.video)}" -i "${captionOverlay}" ` +
`-filter_complex "[0:v][1:v]overlay=0:0:shortest=1" ` +
- `-c:v libx264 -crf 18 -preset fast -map 0:a -c:a copy "${opts.output}"`,
+ `-c:v libx264 -crf 18 -preset fast -pix_fmt yuv420p -map 0:a -c:a copy "${opts.output}"`,
{ stdio: ["pipe", "pipe", "pipe"], timeout: 300000 }
);
diff --git a/tests/test_clip_generator.py b/tests/test_clip_generator.py
index 1efbb35..ba03eeb 100644
--- a/tests/test_clip_generator.py
+++ b/tests/test_clip_generator.py
@@ -1,3 +1,4 @@
+import io
import json
import os
import shutil
@@ -182,6 +183,22 @@ class ClipGeneratorTests(unittest.TestCase):
self.assertIsNone(cg._remotion_available)
self.assertGreaterEqual(mock_run.call_count, 4)
+ def test_remotion_failure_report_takes_bytes_as_well_as_text(self):
+ # TimeoutExpired carries what the child had written and does not honour
+ # text mode when it does, so the reporter has to take either.
+ err = io.StringIO()
+ with mock.patch.object(cg.sys, "stderr", err):
+ cg._report_remotion_failure("render", b"bundled in 3.1s\n", "no browser found\n")
+ printed = err.getvalue()
+ self.assertIn("bundled in 3.1s", printed)
+ self.assertIn("no browser found", printed)
+
+ def test_remotion_failure_report_survives_undecodable_bytes(self):
+ err = io.StringIO()
+ with mock.patch.object(cg.sys, "stderr", err):
+ cg._report_remotion_failure("render", b"\xff\xfe broke", None)
+ self.assertIn("broke", err.getvalue())
+
def test_trim_weak_opening_cuts_initial_dead_air(self):
words = [
{"word": "We", "start": 3.2, "end": 3.4},
diff --git a/tests/test_pixel_format.py b/tests/test_pixel_format.py
new file mode 100644
index 0000000..495c2ac
--- /dev/null
+++ b/tests/test_pixel_format.py
@@ -0,0 +1,80 @@
+"""Every H.264 encode in the pipeline states its pixel format.
+
+FFmpeg picks the output format by negotiating with the filtergraph when the
+command does not say, and some filters answer with 4:4:4 even when every input
+is 4:2:0. xfade is one: the mixed-layout crop stitched its runs together and
+wrote High 4:4:4 Predictive from yuv420p parts. Everything downstream asks for
+profile high, which is 4:2:0 only, so the caption burn aborted with "high
+profile doesn't support 4:4:4" and every clip in the render was lost. podcli
+carries on past a failed clip, so the run exited 0 with an empty output folder.
+
+The commands are spread across five modules and the next one added will not
+remember this, so the rule is checked against the source rather than against
+the one command that broke.
+"""
+
+import ast
+import os
+import sys
+import unittest
+
+ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+BACKEND_ROOT = os.path.join(ROOT, "backend")
+if BACKEND_ROOT not in sys.path:
+ sys.path.insert(0, BACKEND_ROOT)
+
+from services import encoder, media_probe
+
+SERVICES = os.path.join(BACKEND_ROOT, "services")
+
+
+def _string_items(node: ast.List) -> list[str]:
+ return [e.value for e in node.elts if isinstance(e, ast.Constant) and isinstance(e.value, str)]
+
+
+def _encode_command_literals():
+ """Every list literal in the backend that spells out a libx264 encode."""
+ for dirpath, _dirs, files in os.walk(BACKEND_ROOT):
+ if "__pycache__" in dirpath:
+ continue
+ for name in sorted(files):
+ if not name.endswith(".py"):
+ continue
+ path = os.path.join(dirpath, name)
+ with open(path, encoding="utf-8") as fh:
+ tree = ast.parse(fh.read(), filename=path)
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.List):
+ continue
+ items = _string_items(node)
+ if "-c:v" in items and "libx264" in items:
+ yield os.path.relpath(path, ROOT), node.lineno, items
+
+
+class PixelFormatTests(unittest.TestCase):
+ def test_every_libx264_command_pins_yuv420p(self):
+ found = 0
+ for rel_path, lineno, items in _encode_command_literals():
+ found += 1
+ with self.subTest(source=f"{rel_path}:{lineno}"):
+ self.assertIn("-pix_fmt", items)
+ self.assertEqual(items[items.index("-pix_fmt") + 1], "yuv420p")
+ # A rename that stops the walk finding anything would pass silently.
+ self.assertGreater(found, 5)
+
+ def test_cpu_flags_pin_yuv420p(self):
+ self.assertIn("-pix_fmt", media_probe.CPU_FLAGS)
+ idx = media_probe.CPU_FLAGS.index("-pix_fmt")
+ self.assertEqual(media_probe.CPU_FLAGS[idx + 1], "yuv420p")
+
+ def test_every_preset_asking_for_high_profile_pins_yuv420p(self):
+ for name in ("libx264", "h264_videotoolbox", "h264_nvenc"):
+ with self.subTest(encoder=name):
+ flags = encoder._get_encoder_flags(name)
+ self.assertIn("-profile:v", flags)
+ self.assertIn("-pix_fmt", flags)
+ self.assertEqual(flags[flags.index("-pix_fmt") + 1], "yuv420p")
+
+
+if __name__ == "__main__":
+ unittest.main()