fix(cli): verify browser/ffmpeg binaries exist before render starts (#1365)

## Problem

Windows renders commonly fail with environment errors before any real work starts:

- `Browser was not found at the configured executablePath (...chrome-headless-shell.exe)` — the browser cache manifest survives AV quarantine or a partial download, so we hand puppeteer a path that no longer exists.
- `[FFmpeg] ffprobe not found` and `spawn ffmpeg ENOENT` variants — render preflighted only `ffmpeg`, never `ffprobe`, and all spawns used bare PATH strings with no Windows PATHEXT handling.

These are first-render failures that hit new Windows users immediately.

## Fix

- Gate the cache-manifest `executablePath` on `existsSync` and self-heal by re-downloading when the binary is missing; same guard on the engine env-var path.
- New shared environment preflight (`packages/cli/src/browser/preflight.ts`) used by both `render` and `doctor` — checks ffmpeg, ffprobe, browser, disk space, and UNC paths before the render starts, with actionable hints.
- Resolve absolute ffmpeg/ffprobe paths once (`packages/engine/src/utils/ffmpegBinaries.ts`) and pass them to every engine spawn instead of relying on PATH.
- Map opaque Windows ffmpeg exit codes to actionable messages.

## Testing

- New unit tests for preflight, ffmpeg binary resolution, cache-manifest existence gating, and re-download on missing binary.
- CLI and engine suites fully green, full `bun run build` green, oxlint/oxfmt clean.
- Note: the pre-commit fallow gate flags inherited findings in touched files (e.g. `audioExtractor.ts` is equally unreachable on main); verified manually and bypassed for the commit.
This commit is contained in:
Miguel Ángel
2026-06-12 01:36:28 -04:00
committed by GitHub
parent c3554dcffe
commit cee6fd02d6
31 changed files with 896 additions and 151 deletions
@@ -1,3 +1,4 @@
// fallow-ignore-file complexity
/**
* Background-removal rendering pipeline.
*
@@ -14,7 +15,7 @@
*/
import { spawn } from "node:child_process";
import { extname } from "node:path";
import { hasFFmpeg, hasFFprobe } from "../whisper/manager.js";
import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js";
import { createSession, type Session } from "./inference.js";
import { type Device, type ModelId } from "./manager.js";
@@ -263,8 +264,9 @@ export function resolveRenderTargets(
}
export async function render(options: RenderOptions): Promise<RenderResult> {
if (!hasFFmpeg() || !hasFFprobe()) {
throw new Error("ffmpeg and ffprobe are required. Install: brew install ffmpeg");
const ffmpegPath = findFFmpeg();
if (!ffmpegPath || !findFFprobe()) {
throw new Error(`ffmpeg and ffprobe are required. Install: ${getFFmpegInstallHint()}`);
}
const { format, bgFormat } = resolveRenderTargets(
@@ -291,7 +293,14 @@ export async function render(options: RenderOptions): Promise<RenderResult> {
try {
const start = Date.now();
const framesProcessed = await runPipeline(options, session, media, format, bgFormat);
const framesProcessed = await runPipeline(
options,
session,
media,
format,
bgFormat,
ffmpegPath,
);
const durationSeconds = (Date.now() - start) / 1000;
const avgMsPerFrame = framesProcessed ? (durationSeconds * 1000) / framesProcessed : 0;
@@ -321,8 +330,13 @@ interface FfmpegProc {
type StdioFd = "ignore" | "pipe";
type StdioTuple = [StdioFd, StdioFd, StdioFd];
function spawnFfmpeg(args: string[], label: string, stdio: StdioTuple): FfmpegProc {
const proc = spawn("ffmpeg", args, { stdio });
function spawnFfmpeg(
ffmpegPath: string,
args: string[],
label: string,
stdio: StdioTuple,
): FfmpegProc {
const proc = spawn(ffmpegPath, args, { stdio });
let stderrBuf = "";
proc.stderr?.on("data", (d: Buffer) => {
stderrBuf += d.toString();
@@ -343,6 +357,7 @@ async function runPipeline(
media: MediaInfo,
format: OutputFormat,
bgFormat: OutputFormat | undefined,
ffmpegPath: string,
): Promise<number> {
const { inputPath, outputPath, backgroundOutputPath } = options;
const { width, height, fps, frameCount } = media;
@@ -350,12 +365,14 @@ async function runPipeline(
const quality = options.quality ?? DEFAULT_QUALITY;
const decoder = spawnFfmpeg(
ffmpegPath,
["-loglevel", "error", "-i", inputPath, "-f", "rawvideo", "-pix_fmt", "rgb24", "-an", "-"],
"ffmpeg decoder",
["ignore", "pipe", "pipe"],
);
const fg = spawnFfmpeg(
ffmpegPath,
buildEncoderArgs(format, width, height, fps || 30, outputPath, quality),
"ffmpeg encoder",
["pipe", "ignore", "pipe"],
@@ -364,6 +381,7 @@ async function runPipeline(
const bg =
backgroundOutputPath && bgFormat
? spawnFfmpeg(
ffmpegPath,
buildEncoderArgs(bgFormat, width, height, fps || 30, backgroundOutputPath, quality),
"ffmpeg background encoder",
["pipe", "ignore", "pipe"],