mirror of https://github.com/openclaw/openclaw.git
refactor(media): add shared ffmpeg helpers
This commit is contained in:
parent
1187464041
commit
687ef2e00f
|
|
@ -0,0 +1,24 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { parseFfprobeCodecAndSampleRate, parseFfprobeCsvFields } from "./ffmpeg-exec.js";
|
||||
|
||||
describe("parseFfprobeCsvFields", () => {
|
||||
it("splits ffprobe csv output across commas and newlines", () => {
|
||||
expect(parseFfprobeCsvFields("opus,\n48000\n", 2)).toEqual(["opus", "48000"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseFfprobeCodecAndSampleRate", () => {
|
||||
it("parses opus codec and numeric sample rate", () => {
|
||||
expect(parseFfprobeCodecAndSampleRate("Opus,48000\n")).toEqual({
|
||||
codec: "opus",
|
||||
sampleRateHz: 48_000,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null sample rate for invalid numeric fields", () => {
|
||||
expect(parseFfprobeCodecAndSampleRate("opus,not-a-number")).toEqual({
|
||||
codec: "opus",
|
||||
sampleRateHz: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
import { execFile, type ExecFileOptions } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import {
|
||||
MEDIA_FFMPEG_MAX_BUFFER_BYTES,
|
||||
MEDIA_FFMPEG_TIMEOUT_MS,
|
||||
MEDIA_FFPROBE_TIMEOUT_MS,
|
||||
} from "./ffmpeg-limits.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export type MediaExecOptions = {
|
||||
timeoutMs?: number;
|
||||
maxBufferBytes?: number;
|
||||
};
|
||||
|
||||
function resolveExecOptions(
|
||||
defaultTimeoutMs: number,
|
||||
options: MediaExecOptions | undefined,
|
||||
): ExecFileOptions {
|
||||
return {
|
||||
timeout: options?.timeoutMs ?? defaultTimeoutMs,
|
||||
maxBuffer: options?.maxBufferBytes ?? MEDIA_FFMPEG_MAX_BUFFER_BYTES,
|
||||
};
|
||||
}
|
||||
|
||||
export async function runFfprobe(args: string[], options?: MediaExecOptions): Promise<string> {
|
||||
const { stdout } = await execFileAsync(
|
||||
"ffprobe",
|
||||
args,
|
||||
resolveExecOptions(MEDIA_FFPROBE_TIMEOUT_MS, options),
|
||||
);
|
||||
return stdout.toString();
|
||||
}
|
||||
|
||||
export async function runFfmpeg(args: string[], options?: MediaExecOptions): Promise<string> {
|
||||
const { stdout } = await execFileAsync(
|
||||
"ffmpeg",
|
||||
args,
|
||||
resolveExecOptions(MEDIA_FFMPEG_TIMEOUT_MS, options),
|
||||
);
|
||||
return stdout.toString();
|
||||
}
|
||||
|
||||
export function parseFfprobeCsvFields(stdout: string, maxFields: number): string[] {
|
||||
return stdout
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.split(/[,\r\n]+/, maxFields)
|
||||
.map((field) => field.trim());
|
||||
}
|
||||
|
||||
export function parseFfprobeCodecAndSampleRate(stdout: string): {
|
||||
codec: string | null;
|
||||
sampleRateHz: number | null;
|
||||
} {
|
||||
const [codecRaw, sampleRateRaw] = parseFfprobeCsvFields(stdout, 2);
|
||||
const codec = codecRaw ? codecRaw : null;
|
||||
const sampleRate = sampleRateRaw ? Number.parseInt(sampleRateRaw, 10) : Number.NaN;
|
||||
return {
|
||||
codec,
|
||||
sampleRateHz: Number.isFinite(sampleRate) ? sampleRate : null,
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
export const MEDIA_FFMPEG_MAX_BUFFER_BYTES = 10 * 1024 * 1024;
|
||||
export const MEDIA_FFPROBE_TIMEOUT_MS = 10_000;
|
||||
export const MEDIA_FFMPEG_TIMEOUT_MS = 45_000;
|
||||
export const MEDIA_FFMPEG_MAX_AUDIO_DURATION_SECS = 20 * 60;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import fs from "node:fs/promises";
|
||||
|
||||
export async function unlinkIfExists(filePath: string | null | undefined): Promise<void> {
|
||||
if (!filePath) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await fs.unlink(filePath);
|
||||
} catch {
|
||||
// Best-effort cleanup for temp files.
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue