openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
144 lines (143 loc) • 4.37 kB
JavaScript
import { j as writeExternalFileWithinRoot } from "./fs-safe-BbWvmdVn.mjs";
import { n as resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir-Bf0oU1Cn.mjs";
import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs";
import "./exec-aRIvS9s4.mjs";
import { t as basenameFromAnyPath } from "./file-name-PRG-HeBL.mjs";
import { r as runFfmpeg } from "./ffmpeg-exec-DAuj0BaS.mjs";
import "./image-ops-zeupBxnN.mjs";
import "./media-probe-_SAoK4Iu.mjs";
import path from "node:path";
import { tempWorkspace, withTempWorkspace } from "@openclaw/fs-safe/temp";
//#region src/media/audio-transcode.ts
const DEFAULT_OPUS_SAMPLE_RATE_HZ = 48e3;
const DEFAULT_OPUS_BITRATE = "64k";
const DEFAULT_OPUS_CHANNELS = 1;
const DEFAULT_TEMP_PREFIX = "audio-opus-";
const DEFAULT_OUTPUT_FILE_NAME = "voice.opus";
function normalizeTempPrefix(e) {
const t = e?.trim().replace(/[^a-zA-Z0-9._-]/g, "-");
if (!t || t === "." || t === "..") return DEFAULT_TEMP_PREFIX;
return t.endsWith("-") ? t : `${t}-`;
}
function normalizeOutputFileName(e) {
const t = basenameFromAnyPath(e?.trim() || DEFAULT_OUTPUT_FILE_NAME);
if (/^[a-zA-Z0-9._-]{1,80}$/.test(t) && t !== "." && t !== "..") return t;
return DEFAULT_OUTPUT_FILE_NAME;
}
/** Transcodes arbitrary audio input into mono Opus using a scoped temp workspace. */
async function transcodeAudioBufferToOpus(e) {
const t = e.maxDurationSeconds;
if (t !== void 0 && (!Number.isFinite(t) || t <= 0)) throw new Error("maxDurationSeconds must be a positive finite number");
return await withTempWorkspace({
rootDir: resolvePreferredOpenClawTmpDir(),
prefix: normalizeTempPrefix(e.tempPrefix)
}, async (n) => {
const r = e.inputExtension?.trim();
const i = (r ? r.startsWith(".") ? r : `.${r}` : path.extname(e.inputFileName ?? "")).toLowerCase();
const a = /^\.[a-z0-9]{1,12}$/.test(i) ? i : ".audio";
const o = await n.store.write(`input${a}`, e.audioBuffer, { durable: false });
const s = normalizeOutputFileName(e.outputFileName);
await writeExternalFileWithinRoot({
rootDir: n.dir,
path: s,
write: async (n) => {
await runFfmpeg([
"-hide_banner",
"-loglevel",
"error",
"-y",
"-i",
o,
"-vn",
"-sn",
"-dn",
...t === void 0 ? [] : ["-t", String(t)],
"-c:a",
"libopus",
"-b:a",
e.bitrate ?? DEFAULT_OPUS_BITRATE,
"-ar",
String(e.sampleRateHz ?? DEFAULT_OPUS_SAMPLE_RATE_HZ),
"-ac",
String(e.channels ?? DEFAULT_OPUS_CHANNELS),
"-f",
"opus",
n
], { timeoutMs: e.timeoutMs });
}
});
return await n.read(s);
});
}
/** Transcodes known audio container pairs, currently using macOS afconvert recipes where needed. */
async function transcodeAudioBuffer(e) {
const t = normalizeContainerExt(e.sourceExtension);
const n = normalizeContainerExt(e.targetExtension);
if (!t || !n) return {
ok: false,
reason: "invalid-extension"
};
if (t === n) return {
ok: false,
reason: "noop-same-container"
};
if (n !== "caf") return {
ok: false,
reason: "no-recipe"
};
if (process.platform !== "darwin") return {
ok: false,
reason: "platform-unsupported"
};
const r = await tempWorkspace({
rootDir: resolvePreferredOpenClawTmpDir(),
prefix: "tts-transcode-"
});
try {
const i = await runAfconvert([
"-f",
"caff",
"-d",
"opus@24000",
"-c",
"1",
await r.store.write(`in.${t}`, e.audioBuffer, { durable: false }),
r.path(`out.${n}`)
], e.timeoutMs ?? 5e3);
if (i !== void 0) return {
ok: false,
reason: "transcoder-failed",
detail: i
};
return {
ok: true,
buffer: await r.read(`out.${n}`)
};
} catch (e) {
return {
ok: false,
reason: "transcoder-failed",
detail: e.message
};
} finally {
await r.cleanup();
}
}
function normalizeContainerExt(e) {
const t = e.trim().toLowerCase().replace(/^\./, "");
return /^[a-z0-9]{1,12}$/.test(t) ? t : void 0;
}
async function runAfconvert(e, t) {
try {
const n = await runCommandWithTimeout(["/usr/bin/afconvert", ...e], {
maxOutputBytes: 1024,
timeoutMs: t
});
if (n.termination === "timeout") return `timeout-${t}ms`;
return n.code === 0 ? void 0 : `exit-${n.code ?? "unknown"}`;
} catch (e) {
return e instanceof Error ? e.message : String(e);
}
}
//#endregion
export { transcodeAudioBufferToOpus as n, transcodeAudioBuffer as t };