Skip to content

Commit 748cda9

Browse files
fix(parsers): find FFmpeg and FFprobe installed by the project's npm installer packages (#5434)
* fix(parsers): find FFmpeg and FFprobe installed by the project's npm installer packages * fix(parsers): the installer FFmpeg lookup skips non-executable files and works with pnpm * fix(parsers): a system FFmpeg in a common install dir wins over the npm installer package
1 parent 3aba29c commit 748cda9

2 files changed

Lines changed: 140 additions & 9 deletions

File tree

‎packages/parsers/src/ffBinaries.test.ts‎

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
1+
import {
2+
type PathLike,
3+
chmodSync,
4+
mkdirSync,
5+
mkdtempSync,
6+
realpathSync,
7+
rmSync,
8+
symlinkSync,
9+
writeFileSync,
10+
} from "node:fs";
211
import { tmpdir } from "node:os";
312
import { join, resolve } from "node:path";
413
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -173,6 +182,103 @@ describe("findFfBinary", () => {
173182
expect(findFfBinary("ffmpeg")).toBe(projectBinary);
174183
});
175184

185+
function installerProject(
186+
name: "ffmpeg" | "ffprobe",
187+
{ viaInstaller = false, mode = 0o755 } = {},
188+
) {
189+
const project = realpathSync(mkdtempSync(join(tmpdir(), "hf-ff-installer-")));
190+
const platformPackage = `@${name}-installer/${process.platform}-${process.arch}`;
191+
const packageDir = viaInstaller
192+
? join(project, "store", "node_modules", platformPackage)
193+
: join(project, "node_modules", platformPackage);
194+
mkdirSync(packageDir, { recursive: true });
195+
writeFileSync(join(packageDir, "package.json"), "{}");
196+
if (viaInstaller) {
197+
const installerDir = join(project, "store", "node_modules", `@${name}-installer`, name);
198+
mkdirSync(installerDir, { recursive: true });
199+
writeFileSync(join(installerDir, "package.json"), "{}");
200+
mkdirSync(join(project, "node_modules", `@${name}-installer`), { recursive: true });
201+
symlinkSync(
202+
installerDir,
203+
join(project, "node_modules", `@${name}-installer`, name),
204+
"junction",
205+
);
206+
}
207+
const binary = join(packageDir, process.platform === "win32" ? `${name}.exe` : name);
208+
writeFileSync(binary, "");
209+
chmodSync(binary, mode);
210+
return { project, binary };
211+
}
212+
213+
async function findInProjectDir(
214+
project: string,
215+
name: "ffmpeg" | "ffprobe",
216+
systemBinaries: string[] = [],
217+
) {
218+
vi.stubEnv("HYPERFRAMES_FFMPEG_PATH", "");
219+
vi.stubEnv("HYPERFRAMES_FFPROBE_PATH", "");
220+
process.env.PATH = "";
221+
vi.spyOn(process, "cwd").mockReturnValue(project);
222+
vi.resetModules();
223+
vi.doMock("node:fs", async (importOriginal) => {
224+
const actual = await importOriginal<typeof import("node:fs")>();
225+
const existsSync = (candidate: PathLike) =>
226+
systemBinaries.includes(String(candidate)) ||
227+
(String(candidate).startsWith(project) && actual.existsSync(candidate));
228+
return { ...actual, existsSync, default: { ...actual, existsSync } };
229+
});
230+
vi.doMock("node:child_process", () => {
231+
const mocked = {
232+
execFileSync: () => {
233+
throw new Error("not found");
234+
},
235+
};
236+
return { ...mocked, default: mocked };
237+
});
238+
try {
239+
const { findFfBinary } = await importFresh();
240+
return findFfBinary(name);
241+
} finally {
242+
vi.unstubAllEnvs();
243+
rmSync(project, { recursive: true, force: true });
244+
}
245+
}
246+
247+
it.each(["ffmpeg", "ffprobe"] as const)(
248+
"falls back to the project's @%s-installer package",
249+
async (name) => {
250+
const { project, binary } = installerProject(name);
251+
252+
expect(await findInProjectDir(project, name)).toBe(binary);
253+
},
254+
);
255+
256+
it("finds the platform package next to the installer package (pnpm layout)", async () => {
257+
const { project, binary } = installerProject("ffprobe", { viaInstaller: true });
258+
259+
expect(await findInProjectDir(project, "ffprobe")).toBe(binary);
260+
});
261+
262+
it.skipIf(process.platform === "win32")(
263+
"prefers a system install in a common dir over the installer package",
264+
async () => {
265+
const { project } = installerProject("ffprobe");
266+
267+
expect(await findInProjectDir(project, "ffprobe", ["/usr/bin/ffprobe"])).toBe(
268+
"/usr/bin/ffprobe",
269+
);
270+
},
271+
);
272+
273+
it.skipIf(process.platform === "win32")(
274+
"skips an installer binary that is not executable",
275+
async () => {
276+
const { project } = installerProject("ffprobe", { mode: 0o644 });
277+
278+
expect(await findInProjectDir(project, "ffprobe")).toBeUndefined();
279+
},
280+
);
281+
176282
it("returns undefined when the binary is nowhere, and caches the miss until cleared", async () => {
177283
delete process.env.HYPERFRAMES_FFMPEG_PATH;
178284
Object.defineProperty(process, "platform", { value: "linux", configurable: true });

‎packages/parsers/src/ffBinaries.ts‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { execFileSync } from "node:child_process";
22
import { accessSync, constants, existsSync } from "node:fs";
3-
import { delimiter, join, resolve } from "node:path";
3+
import { createRequire } from "node:module";
4+
import { delimiter, dirname, join, resolve } from "node:path";
45

56
/**
67
* Shared FFmpeg/FFprobe binary resolution for every package that shells out
@@ -87,20 +88,44 @@ const COMMON_BIN_DIRS =
8788
? []
8889
: ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin", "/bin", "/snap/bin"];
8990

90-
function findInCommonDirs(name: FfBinaryName): string | undefined {
91+
// The npm installer package comes last: its binaries are old builds, so any system install wins.
92+
function findInCommonDirsOrInstallerPackage(name: FfBinaryName): string | undefined {
9193
for (const dir of COMMON_BIN_DIRS) {
9294
const candidate = `${dir}/${name}`;
9395
if (existsSync(candidate)) return candidate;
9496
}
95-
return undefined;
97+
return findInInstallerPackage(name);
98+
}
99+
100+
function binaryFileName(name: FfBinaryName): string {
101+
return process.platform === "win32" ? `${name}.exe` : name;
96102
}
97103

98104
function findInProjectLocalBin(name: FfBinaryName): string | undefined {
99-
const extension = process.platform === "win32" ? ".exe" : "";
100-
const candidate = resolve(".hyperframes", "bin", `${name}${extension}`);
105+
const candidate = resolve(".hyperframes", "bin", binaryFileName(name));
101106
return existsSync(candidate) ? candidate : undefined;
102107
}
103108

109+
function tryResolve(fromFile: string, request: string): string | undefined {
110+
try {
111+
return createRequire(fromFile).resolve(request);
112+
} catch {
113+
return undefined;
114+
}
115+
}
116+
117+
// An @ffmpeg-installer / @ffprobe-installer package reachable from the current folder, located
118+
// but never loaded. The platform package is resolved via the installer package first (pnpm layout).
119+
function findInInstallerPackage(name: FfBinaryName): string | undefined {
120+
const project = resolve("package.json");
121+
const platformManifest = `@${name}-installer/${process.platform}-${process.arch}/package.json`;
122+
const installer = tryResolve(project, `@${name}-installer/${name}/package.json`);
123+
const manifest =
124+
(installer && tryResolve(installer, platformManifest)) ?? tryResolve(project, platformManifest);
125+
const candidate = manifest && join(dirname(manifest), binaryFileName(name));
126+
return candidate && isExecutablePathCandidate(candidate) ? candidate : undefined;
127+
}
128+
104129
function lookupOnSystem(name: FfBinaryName): string | undefined {
105130
if (pathLookupCache.has(name)) return pathLookupCache.get(name);
106131
let found: string | undefined;
@@ -124,7 +149,7 @@ function lookupOnSystem(name: FfBinaryName): string | undefined {
124149
}
125150
}
126151
found ??= findInProjectLocalBin(name);
127-
found ??= findInCommonDirs(name);
152+
found ??= findInCommonDirsOrInstallerPackage(name);
128153
const resolved = found ? resolve(found) : undefined;
129154
pathLookupCache.set(name, resolved);
130155
return resolved;
@@ -144,8 +169,8 @@ export interface FindFfBinaryOptions {
144169
/**
145170
* Resolve an FFmpeg-family binary: env override first, then a native
146171
* current-directory/PATH scan on Windows or `which` plus PATH scan on Unix,
147-
* then a project-local
148-
* `.hyperframes/bin`, then well-known Unix install dirs. System lookups are
172+
* then a project-local `.hyperframes/bin`, then well-known Unix install dirs, then an
173+
* `@ffmpeg-installer`/`@ffprobe-installer` package reachable from the current folder. System lookups are
149174
* cached per binary for the process lifetime; the env override is re-read on
150175
* every call.
151176
*/

0 commit comments

Comments
 (0)