fix: a configured ffmpeg or ffprobe path that cannot run fails loudly

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-27 17:38:05 -04:00
parent 546730d037
commit d130faeb27
12 changed files with 111 additions and 42 deletions
+16 -2
View File
@@ -1,3 +1,17 @@
import { accessSync, constants } from "node:fs";
// The binaries media-use spawns: HYPERFRAMES_FFMPEG_PATH / HYPERFRAMES_FFPROBE_PATH when set, else PATH.
export const ffmpegBinary = () => process.env.HYPERFRAMES_FFMPEG_PATH?.trim() || "ffmpeg";
export const ffprobeBinary = () => process.env.HYPERFRAMES_FFPROBE_PATH?.trim() || "ffprobe";
// A set path that cannot run throws, so a broken override never reads as "no metadata".
function configuredOr(name, envVar) {
const path = process.env[envVar]?.trim();
if (!path) return name;
try {
accessSync(path, constants.X_OK);
} catch {
throw new Error(`${envVar} names "${path}", which cannot run: fix it or unset it.`);
}
return path;
}
export const ffmpegBinary = () => configuredOr("ffmpeg", "HYPERFRAMES_FFMPEG_PATH");
export const ffprobeBinary = () => configuredOr("ffprobe", "HYPERFRAMES_FFPROBE_PATH");
+2 -1
View File
@@ -8,11 +8,12 @@ export function probe(filePath) {
const ext = extname(filePath).toLowerCase();
if (ext === ".svg") return { width: null, height: null, duration: null, codec: "svg" };
const ffprobe = ffprobeBinary();
try {
// execFileSync (no shell) so a hostile filename like `"; rm -rf ~; ".png`
// can't break out of the quoting — filePath is passed as a literal argv entry.
const raw = execFileSync(
ffprobeBinary(),
ffprobe,
["-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", "--", filePath],
{ encoding: "utf8", timeout: 5000 },
);
@@ -57,3 +57,14 @@ test(
}
},
);
test("probe refuses an HYPERFRAMES_FFPROBE_PATH that cannot run instead of reporting no metadata", () => {
const configured = process.env.HYPERFRAMES_FFPROBE_PATH;
process.env.HYPERFRAMES_FFPROBE_PATH = join(tmpdir(), "no-ffprobe-here", "ffprobe");
try {
assert.throws(() => probe("clip.wav"), /HYPERFRAMES_FFPROBE_PATH names ".*no-ffprobe-here.*fix it or unset it/);
} finally {
if (configured === undefined) delete process.env.HYPERFRAMES_FFPROBE_PATH;
else process.env.HYPERFRAMES_FFPROBE_PATH = configured;
}
});
@@ -15,9 +15,10 @@ import { resolveSpawnCommand } from "../../audio/scripts/lib/tts.mjs";
// re-implemented here. ffprobe reads the duration back for the ledger.
function probeDurationSeconds(file) {
const ffprobe = ffprobeBinary();
try {
const out = execFileSync(
ffprobeBinary(),
ffprobe,
["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", "--", file],
{ encoding: "utf8", timeout: 15000 },
);
+18 -15
View File
@@ -1067,21 +1067,7 @@ function runDoctor() {
checks.push(heygenAuthCheck());
}
const ffmpegProbe = runCommand(ffmpegBinary(), ["-version"]);
checks.push({
name: "ffmpeg on PATH",
ok: ffmpegProbe.status === 0,
detail: ffmpegProbe.status === 0 ? firstLine(ffmpegProbe.stdout) : "ffmpeg not found",
fix: ffmpegProbe.status === 0 ? "" : "brew install ffmpeg",
});
const ffprobeProbe = runCommand(ffprobeBinary(), ["-version"]);
checks.push({
name: "ffprobe on PATH",
ok: ffprobeProbe.status === 0,
detail: ffprobeProbe.status === 0 ? firstLine(ffprobeProbe.stdout) : "ffprobe not found",
fix: ffprobeProbe.status === 0 ? "" : "brew install ffmpeg",
});
checks.push(ffDoctorCheck("ffmpeg", ffmpegBinary), ffDoctorCheck("ffprobe", ffprobeBinary));
const nodeOk = !versionLessThan(process.versions.node, MIN_NODE_VERSION);
checks.push({
@@ -1147,6 +1133,23 @@ function printMap(label, values) {
for (const [key, value] of entries) console.log(` ${key}: ${value}`);
}
function ffDoctorCheck(name, binary) {
let bin;
try {
bin = binary();
} catch (err) {
return { name: `${name} on PATH`, ok: false, detail: err.message, fix: "fix or unset that variable" };
}
const probe = runCommand(bin, ["-version"]);
const ok = probe.status === 0;
return {
name: `${name} on PATH`,
ok,
detail: ok ? firstLine(probe.stdout) : `${name} not found`,
fix: ok ? "" : "brew install ffmpeg",
};
}
function runCommand(bin, argv) {
return spawnSync(bin, argv, {
encoding: "utf8",
@@ -708,6 +708,28 @@ test("--doctor checks the ffmpeg and ffprobe that HYPERFRAMES_FFMPEG_PATH and HY
}
});
test("--doctor fails a HYPERFRAMES_FFPROBE_PATH that cannot run and says to fix or unset it", () => {
const missing = join(tmpdir(), "mu-doctor-no-ffprobe", "ffprobe");
const result = spawnResolve(["--doctor", "--json"], { env: { HYPERFRAMES_FFPROBE_PATH: missing } });
const report = JSON.parse(result.stdout.trim());
const check = report.checks.find((c) => c.name === "ffprobe on PATH");
assert.equal(report.ok, false);
assert.deepEqual(
[check.ok, check.detail, check.fix],
[false, `HYPERFRAMES_FFPROBE_PATH names "${missing}", which cannot run: fix it or unset it.`, "fix or unset that variable"],
);
});
test("--analyze refuses a HYPERFRAMES_FFPROBE_PATH that cannot run instead of reporting unknown", () => {
const missing = join(tmpdir(), "mu-analyze-no-ffprobe", "ffprobe");
const result = spawnResolve(["--analyze", "--type", "grade", "--for", RESOLVE_CLI, "--json"], {
env: { HYPERFRAMES_FFPROBE_PATH: missing },
});
assert.notEqual(result.status, 0);
assert.match(result.stderr, /HYPERFRAMES_FFPROBE_PATH names ".*mu-analyze-no-ffprobe.*fix it or unset it/);
assert.doesNotMatch(result.stdout, /"ok":true/);
});
test("one-line output format matches contract", () => {
setup();
const record = makeRecord({ provenance: { prompt: "format test", provider: "test" } });