mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-01 20:42:50 +08:00
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:
1 file changed
+8
-4
+8
-4
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user