fix: refuse a folder as the ffprobe path, and report broken paths through --json

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-27 17:56:38 -04:00
parent d130faeb27
commit a93184a80a
7 changed files with 118 additions and 26 deletions
@@ -1,4 +1,4 @@
import { accessSync, constants } from "node:fs";
import { accessSync, constants, statSync } from "node:fs";
// The binaries media-use spawns: HYPERFRAMES_FFMPEG_PATH / HYPERFRAMES_FFPROBE_PATH when set, else PATH.
// A set path that cannot run throws, so a broken override never reads as "no metadata".
@@ -6,6 +6,7 @@ function configuredOr(name, envVar) {
const path = process.env[envVar]?.trim();
if (!path) return name;
try {
if (!statSync(path).isFile()) throw new Error("not a file");
accessSync(path, constants.X_OK);
} catch {
throw new Error(`${envVar} names "${path}", which cannot run: fix it or unset it.`);
+15 -1
View File
@@ -62,7 +62,21 @@ test("probe refuses an HYPERFRAMES_FFPROBE_PATH that cannot run instead of repor
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/);
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;
}
});
test("probe refuses an HYPERFRAMES_FFPROBE_PATH that names a folder", () => {
const configured = process.env.HYPERFRAMES_FFPROBE_PATH;
process.env.HYPERFRAMES_FFPROBE_PATH = tmpdir();
try {
assert.throws(() => probe("clip.wav"), /HYPERFRAMES_FFPROBE_PATH names .*fix it or unset it/);
} finally {
if (configured === undefined) delete process.env.HYPERFRAMES_FFPROBE_PATH;
else process.env.HYPERFRAMES_FFPROBE_PATH = configured;
@@ -14,8 +14,7 @@ import { resolveSpawnCommand } from "../../audio/scripts/lib/tts.mjs";
// Delegated to the hyperframes CLI (same as transcribe / remove-background), not
// re-implemented here. ffprobe reads the duration back for the ledger.
function probeDurationSeconds(file) {
const ffprobe = ffprobeBinary();
function probeDurationSeconds(ffprobe, file) {
try {
const out = execFileSync(
ffprobe,
@@ -41,6 +40,7 @@ export async function localTtsGenerate(
env = process.env,
pathExists = existsSync,
) {
const ffprobe = ffprobeBinary();
const outPath = join(tmpdir(), `media-use-kokoro-${process.pid}-${Date.now()}.wav`);
const argv = ["hyperframes", "tts", intent, "--output", outPath];
if (ctx?.voice) argv.push("--voice", ctx.voice);
@@ -87,7 +87,7 @@ export async function localTtsGenerate(
metadata: {
description: intent,
provider: "kokoro.local",
duration: probeDurationSeconds(outPath),
duration: probeDurationSeconds(ffprobe, outPath),
provenance: { engine: "kokoro-82m", prompt: intent },
},
};
+28 -10
View File
@@ -145,7 +145,12 @@ const entity = args.entity || null;
if (args.adopt) {
const { adoptExistingAssets } = await import("./lib/adopt.mjs");
const adopted = adoptExistingAssets(projectDir);
let adopted;
try {
adopted = adoptExistingAssets(projectDir);
} catch (err) {
exitError(err.message);
}
if (args.json) {
console.log(JSON.stringify({ ok: true, adopted: adopted.length, assets: adopted }));
} else if (adopted.length === 0) {
@@ -219,10 +224,15 @@ if (args.analyze) {
console.error(`error: --for file not found: ${mediaPath}`);
process.exit(2);
}
const analysis = analyzeMediaGrade(mediaPath, {
ffmpegPath: ffmpegBinary(),
ffprobePath: ffprobeBinary(),
});
let analysis;
try {
analysis = analyzeMediaGrade(mediaPath, {
ffmpegPath: ffmpegBinary(),
ffprobePath: ffprobeBinary(),
});
} catch (err) {
exitError(err.message);
}
if (args.json) {
console.log(JSON.stringify({ ok: true, type: "grade-analysis", ...analysis }));
} else {
@@ -1138,15 +1148,23 @@ function ffDoctorCheck(name, binary) {
try {
bin = binary();
} catch (err) {
return { name: `${name} on PATH`, ok: false, detail: err.message, fix: "fix or unset that variable" };
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;
if (probe.status === 0) {
return { name: `${name} on PATH`, ok: true, detail: firstLine(probe.stdout), fix: "" };
}
const configured = bin !== name;
return {
name: `${name} on PATH`,
ok,
detail: ok ? firstLine(probe.stdout) : `${name} not found`,
fix: ok ? "" : "brew install ffmpeg",
ok: false,
detail: configured ? `"${bin}" did not run` : `${name} not found`,
fix: configured ? "fix or unset the variable that names it" : "brew install ffmpeg",
};
}
+56 -5
View File
@@ -514,6 +514,22 @@ test("--adopt registers existing assets/ files", () => {
cleanup();
});
test("--adopt stops, adopting nothing, when HYPERFRAMES_FFPROBE_PATH cannot run", () => {
setup();
mkdirSync(join(tmp, "assets/bgm"), { recursive: true });
writeFileSync(join(tmp, "assets/bgm/track.mp3"), "fake mp3");
const result = spawnResolve(["--adopt", "--project", tmp, "--json"], {
env: { HYPERFRAMES_FFPROBE_PATH: join(tmp, "no-ffprobe") },
});
assert.equal(result.status, 1);
const report = JSON.parse(result.stdout.trim());
assert.equal(report.ok, false);
assert.match(report.error, /HYPERFRAMES_FFPROBE_PATH names .*no-ffprobe.*fix it or unset it/);
assert.equal(readManifest(tmp).length, 0);
cleanup();
});
test("--adopt skips already-registered assets", () => {
setup();
mkdirSync(join(tmp, "assets/bgm"), { recursive: true });
@@ -710,13 +726,19 @@ 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 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"],
[
false,
`HYPERFRAMES_FFPROBE_PATH names "${missing}", which cannot run: fix it or unset it.`,
"fix or unset that variable",
],
);
});
@@ -725,11 +747,40 @@ test("--analyze refuses a HYPERFRAMES_FFPROBE_PATH that cannot run instead of re
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/);
assert.equal(result.status, 1);
const report = JSON.parse(result.stdout.trim());
assert.equal(report.ok, false);
assert.match(
report.error,
/HYPERFRAMES_FFPROBE_PATH names ".*mu-analyze-no-ffprobe.*fix it or unset it/,
);
});
test(
"--doctor names a configured ffprobe that exists but does not run",
{ skip: process.platform === "win32" },
() => {
const dir = mkdtempSync(join(tmpdir(), "mu-doctor-bad-ff-"));
const broken = join(dir, "ffprobe");
writeFileSync(broken, "#!/bin/sh\nexit 3\n");
chmodSync(broken, 0o755);
try {
const result = spawnResolve(["--doctor", "--json"], {
env: { HYPERFRAMES_FFPROBE_PATH: broken },
});
const check = JSON.parse(result.stdout.trim()).checks.find(
(c) => c.name === "ffprobe on PATH",
);
assert.deepEqual(
[check.ok, check.detail, check.fix],
[false, `"${broken}" did not run`, "fix or unset the variable that names it"],
);
} finally {
rmSync(dir, { recursive: true, force: true });
}
},
);
test("one-line output format matches contract", () => {
setup();
const record = makeRecord({ provenance: { prompt: "format test", provider: "test" } });
+4 -2
View File
@@ -207,8 +207,10 @@ export function useFileManager({
showToast(`Skipped (too large): ${data.skipped.join(", ")}`);
}
if (data.invalid?.length) {
const names = data.invalid.map((entry: { name: string }) => entry.name).join(", ");
showToast(`Not added: ${names}, ${data.invalid[0].reason}`);
const why = data.invalid
.map((entry: { name: string; reason: string }) => `${entry.name} (${entry.reason})`)
.join(", ");
showToast(`Not added: ${why}`);
}
if (data.unchecked?.length) {
const names = data.unchecked.map((entry: { name: string }) => entry.name).join(", ");
@@ -75,10 +75,16 @@ it("says which uploads were added without a media check, and why", async () => {
expect(showToast).toHaveBeenCalledWith(`Added clip.mp4, ${reason}`, "info");
});
it("says why an upload was not added", async () => {
const reason = 'HYPERFRAMES_FFPROBE_PATH names "/x/ffprobe", which cannot run: fix it or unset it';
const { added, showToast } = await uploadClip({ invalid: [{ name: "clip.mp4", reason }] });
it("says why each upload was not added", async () => {
const { added, showToast } = await uploadClip({
invalid: [
{ name: "clip.mp4", reason: "no supported video stream found" },
{ name: "song.mp3", reason: "no supported audio stream found" },
],
});
expect(added).toEqual([]);
expect(showToast).toHaveBeenCalledWith(`Not added: clip.mp4, ${reason}`);
expect(showToast).toHaveBeenCalledWith(
"Not added: clip.mp4 (no supported video stream found), song.mp3 (no supported audio stream found)",
);
});