Skip to content

Write HTML. Render video. Built for agents.

fix(parsers): validate Windows FFmpeg discovery candidates (#2871)

Miguel Ángel4ad1cf4551b7
diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts
index 1c1bd54eba..dbb7e36c2c 100644
--- a/packages/parsers/src/ffBinaries.test.ts
+++ b/packages/parsers/src/ffBinaries.test.ts
@@ -17,6 +17,7 @@ describe("findFfBinary", () => {
const originalPlatform = process.platform;
afterEach(() => {
+ vi.restoreAllMocks();
vi.resetModules();
vi.doUnmock("node:child_process");
vi.doUnmock("node:fs");
@@ -44,17 +45,49 @@ describe("findFfBinary", () => {
expect(findFfBinary("ffmpeg")).toBe(resolve(join(tmpdir(), "definitely-missing-ffmpeg")));
});
- it("prefers the real Windows exe when where lists a cmd shim first", async () => {
+ it("prefers the real Windows exe over a cmd shim in PATH", async () => {
delete process.env.HYPERFRAMES_FFMPEG_PATH;
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
+ process.env.PATH = "/tools";
vi.resetModules();
- vi.doMock("node:child_process", () => {
- const mocked = { execFileSync: () => "C:\\tools\\ffmpeg.cmd\r\nC:\\tools\\ffmpeg.exe\r\n" };
+ vi.doMock("node:fs", () => {
+ const mocked = {
+ existsSync: (candidate: unknown) => candidate === "/tools/ffmpeg.exe",
+ accessSync: () => {},
+ constants: { X_OK: 1 },
+ };
+ return { ...mocked, default: mocked };
+ });
+ const { findFfBinary } = await importFresh();
+
+ expect(findFfBinary("ffmpeg")).toBe("/tools/ffmpeg.exe");
+ });
+
+ it("discovers a Windows binary in a Unicode current directory without decoding console output", async () => {
+ delete process.env.HYPERFRAMES_FFMPEG_PATH;
+ Object.defineProperty(process, "platform", { value: "win32", configurable: true });
+ process.env.PATH = "";
+ const unicodeDirectory = "/用户/工具";
+ const ffmpegPath = join(unicodeDirectory, "ffmpeg.exe");
+ vi.spyOn(process, "cwd").mockReturnValue(unicodeDirectory);
+ const execFileSync = vi.fn();
+ vi.resetModules();
+ vi.doMock("node:child_process", () => ({
+ execFileSync,
+ default: { execFileSync },
+ }));
+ vi.doMock("node:fs", () => {
+ const mocked = {
+ existsSync: (candidate: unknown) => candidate === ffmpegPath,
+ accessSync: () => {},
+ constants: { X_OK: 1 },
+ };
return { ...mocked, default: mocked };
});
const { findFfBinary } = await importFresh();
- expect(findFfBinary("ffmpeg")).toBe(resolve("C:\\tools\\ffmpeg.exe"));
+ expect(findFfBinary("ffmpeg")).toBe(ffmpegPath);
+ expect(execFileSync).not.toHaveBeenCalled();
});
it("falls back to scanning PATH when which/where fails", async () => {
diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts
index c0623ef329..b9070af16b 100644
--- a/packages/parsers/src/ffBinaries.ts
+++ b/packages/parsers/src/ffBinaries.ts
@@ -49,7 +49,11 @@ function isExecutablePathCandidate(candidate: string): boolean {
function scanPath(name: FfBinaryName): string | undefined {
const pathValue = process.env.PATH;
- if (!pathValue) return undefined;
+ const searchDirs = [
+ ...(process.platform === "win32" ? [process.cwd()] : []),
+ ...(pathValue ? pathValue.split(delimiter) : []),
+ ].filter(Boolean);
+ if (searchDirs.length === 0) return undefined;
const extensions =
process.platform === "win32"
@@ -65,8 +69,7 @@ function scanPath(name: FfBinaryName): string | undefined {
]
: [""];
const candidates: string[] = [];
- for (const dir of pathValue.split(delimiter)) {
- if (!dir) continue;
+ for (const dir of new Set(searchDirs)) {
for (const ext of extensions) {
const candidate = join(dir, `${name}${ext}`);
if (isExecutablePathCandidate(candidate)) candidates.push(candidate);
@@ -101,16 +104,24 @@ function findInProjectLocalBin(name: FfBinaryName): string | undefined {
function lookupOnSystem(name: FfBinaryName): string | undefined {
if (pathLookupCache.has(name)) return pathLookupCache.get(name);
let found: string | undefined;
- try {
- const command = process.platform === "win32" ? "where" : "which";
- const output = execFileSync(command, [name], {
- encoding: "utf-8",
- stdio: ["pipe", "pipe", "pipe"],
- timeout: 5000,
- });
- found = chooseBestPathCandidate(name, output.split(/\r?\n/));
- } catch {
+ if (process.platform === "win32") {
+ // `where.exe` writes bytes in the active console code page, while Node
+ // decodes its stdout using a caller-selected encoding. Enumerating the
+ // same current-directory + PATH search space from Node keeps Unicode
+ // paths as native JS strings and avoids mojibake entirely.
found = scanPath(name);
+ } else {
+ try {
+ const output = execFileSync("which", [name], {
+ encoding: "utf-8",
+ stdio: ["pipe", "pipe", "pipe"],
+ timeout: 5000,
+ });
+ const candidate = chooseBestPathCandidate(name, output.split(/\r?\n/));
+ found = candidate && isExecutablePathCandidate(candidate) ? candidate : scanPath(name);
+ } catch {
+ found = scanPath(name);
+ }
}
found ??= findInProjectLocalBin(name);
found ??= findInCommonDirs(name);
@@ -131,8 +142,9 @@ export interface FindFfBinaryOptions {
}
/**
- * Resolve an FFmpeg-family binary: env override first, then `which`/`where`,
- * then a manual PATH scan (covers Windows PATHEXT), a project-local
+ * Resolve an FFmpeg-family binary: env override first, then a native
+ * current-directory/PATH scan on Windows or `which` plus PATH scan on Unix,
+ * then a project-local
* `.hyperframes/bin`, then well-known Unix install dirs. System lookups are
* cached per binary for the process lifetime; the env override is re-read on
* every call.