Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Improve FFmpeg binary resolution and usage

Richie McIlroy32b036da790b
diff --git a/apps/web/__tests__/unit/audio-extract.test.ts b/apps/web/__tests__/unit/audio-extract.test.ts
index 54bad8c42..6f7aeb4eb 100644
--- a/apps/web/__tests__/unit/audio-extract.test.ts
+++ b/apps/web/__tests__/unit/audio-extract.test.ts
@@ -6,11 +6,17 @@ vi.mock("ffmpeg-static", () => ({
}));
const mockUnlink = vi.fn(() => Promise.resolve(undefined));
-vi.mock("node:fs", () => ({
- promises: {
- unlink: () => mockUnlink(),
- },
-}));
+vi.mock("node:fs", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("node:fs")>();
+ return {
+ ...actual,
+ existsSync: (path: string) => path === "/usr/local/bin/ffmpeg",
+ promises: {
+ ...actual.promises,
+ unlink: () => mockUnlink(),
+ },
+ };
+});
class MockChildProcess extends EventEmitter {
stdout = new EventEmitter();
diff --git a/apps/web/lib/audio-extract.ts b/apps/web/lib/audio-extract.ts
index 9a26200e5..bcd2604fb 100644
--- a/apps/web/lib/audio-extract.ts
+++ b/apps/web/lib/audio-extract.ts
@@ -1,9 +1,40 @@
import { spawn } from "node:child_process";
import { randomUUID } from "node:crypto";
-import { promises as fs } from "node:fs";
+import { existsSync, promises as fs } from "node:fs";
import { tmpdir } from "node:os";
-import { join } from "node:path";
-import ffmpegPath from "ffmpeg-static";
+import { join, resolve } from "node:path";
+import ffmpegStaticPath from "ffmpeg-static";
+
+let cachedFfmpegPath: string | null = null;
+
+function getFfmpegPath(): string {
+ if (cachedFfmpegPath) {
+ return cachedFfmpegPath;
+ }
+
+ const candidatePaths = [
+ ffmpegStaticPath,
+ resolve(process.cwd(), "node_modules/ffmpeg-static/ffmpeg"),
+ resolve(
+ process.cwd(),
+ "node_modules/.pnpm/[email protected]/node_modules/ffmpeg-static/ffmpeg",
+ ),
+ "/var/task/node_modules/ffmpeg-static/ffmpeg",
+ "/var/task/node_modules/.pnpm/[email protected]/node_modules/ffmpeg-static/ffmpeg",
+ ].filter(Boolean) as string[];
+
+ for (const path of candidatePaths) {
+ if (existsSync(path)) {
+ console.log(`[audio-extract] Found FFmpeg at: ${path}`);
+ cachedFfmpegPath = path;
+ return path;
+ }
+ }
+
+ throw new Error(
+ `FFmpeg binary not found. Tried paths: ${candidatePaths.join(", ")}`,
+ );
+}
export interface AudioExtractionResult {
filePath: string;
@@ -14,11 +45,7 @@ export interface AudioExtractionResult {
export async function extractAudioFromUrl(
videoUrl: string,
): Promise<AudioExtractionResult> {
- if (!ffmpegPath) {
- throw new Error("FFmpeg binary not found");
- }
-
- const ffmpeg = ffmpegPath;
+ const ffmpeg = getFfmpegPath();
const outputPath = join(tmpdir(), `audio-${randomUUID()}.m4a`);
const ffmpegArgs = [
@@ -81,11 +108,7 @@ export async function extractAudioFromUrl(
}
export async function extractAudioToBuffer(videoUrl: string): Promise<Buffer> {
- if (!ffmpegPath) {
- throw new Error("FFmpeg binary not found");
- }
-
- const ffmpeg = ffmpegPath;
+ const ffmpeg = getFfmpegPath();
const ffmpegArgs = [
"-i",
videoUrl,
@@ -133,12 +156,13 @@ export async function extractAudioToBuffer(videoUrl: string): Promise<Buffer> {
}
export async function checkHasAudioTrack(videoUrl: string): Promise<boolean> {
- if (!ffmpegPath) {
- console.error("[audio-extract] FFmpeg binary not found");
+ let ffmpeg: string;
+ try {
+ ffmpeg = getFfmpegPath();
+ } catch (err) {
+ console.error("[audio-extract] FFmpeg binary not found:", err);
return false;
}
-
- const ffmpeg = ffmpegPath;
const ffmpegArgs = ["-i", videoUrl, "-hide_banner"];
return new Promise((resolve) => {
diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs
index e9712c707..254130a91 100644
--- a/apps/web/next.config.mjs
+++ b/apps/web/next.config.mjs
@@ -19,6 +19,7 @@ const nextConfig = {
outputFileTracingIncludes: {
"/app/.well-known/workflow/v1/step": [
"./node_modules/ffmpeg-static/ffmpeg",
+ "./node_modules/.pnpm/[email protected]/node_modules/ffmpeg-static/ffmpeg",
],
},
transpilePackages: [