refactor: cache resolved ffmpeg path at module load time

Avoid repeated filesystem checks on every play() call by resolving
the ffmpeg binary path once at module load. Also verify execute
permission after chmod to handle noexec mounts.

https://claude.ai/code/session_01CqfKgV8GuCmWNpfx86H62X
This commit is contained in:
Claude committed 2026-03-31 16:02:00 +00:00
1 parent 58caff3209
commit 9e52b0bda9
1 file changed
+8 -4
+8 -4
View File
@@ -9,14 +9,15 @@ import type { Logger } from "../logger.js";
const require = createRequire(import.meta.url);
const ffmpegPath: string | null = require("ffmpeg-static");
/** Ensure the bundled ffmpeg binary has execute permission. */
function ensureExecutable(binPath: string): boolean {
/** Ensure the given binary has execute permission. */
function isExecutable(binPath: string): boolean {
try {
accessSync(binPath, constants.X_OK);
return true;
} catch {
try {
chmodSync(binPath, 0o755);
accessSync(binPath, constants.X_OK);
return true;
} catch {
return false;
@@ -24,10 +25,13 @@ function ensureExecutable(binPath: string): boolean {
}
}
/** Resolved once at module load — no repeated fs checks per play(). */
const resolvedFfmpeg: string =
ffmpegPath && isExecutable(ffmpegPath) ? ffmpegPath : "ffmpeg";
/** Resolve ffmpeg binary: prefer bundled ffmpeg-static, fall back to system PATH. */
function getFfmpegCommand(): string {
if (ffmpegPath && ensureExecutable(ffmpegPath)) return ffmpegPath;
return "ffmpeg"; // fallback to system-installed ffmpeg
return resolvedFfmpeg;
}
export interface PlayerEvents {