Skip to content

Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.

Trộn tiếng: giới hạn thời gian ffmpeg, tự thử lại, cắt đúng độ dài

Lumtek97da59deb854
diff --git a/engine/audio/mix.mjs b/engine/audio/mix.mjs
index 6f090d1..313bea3 100644
--- a/engine/audio/mix.mjs
+++ b/engine/audio/mix.mjs
@@ -44,7 +44,12 @@ export function validateAudioSpec(spec) {
}
async function ff(args, what) {
- const r = await run('ffmpeg', ['-y', '-hide_banner', '-nostats', ...args]);
+ // ffmpeg đôi khi treo ở graph sidechain → giới hạn 3 phút, thử lại 1 lần
+ let r;
+ for (let i = 0; i < 2; i++) {
+ r = await run('ffmpeg', ['-y', '-hide_banner', '-nostats', '-nostdin', ...args], { stdio: ['ignore', 'pipe', 'pipe'], timeoutMs: 180000 });
+ if (r.code === 0) break;
+ }
if (r.code !== 0) throw new Error(`${what} lỗi: ${r.out.slice(-800)}`);
return r.out;
}
@@ -121,7 +126,7 @@ export async function buildMix(sceneDir, { duration, workDir, log = console.log,
// tiền trộn (float, chưa chuẩn hoá) dài đúng duration
const pre = path.join(workDir, 'premix.wav');
const graph = [...chains, `${mixLabels.join('')}amix=inputs=${mixLabels.length}:normalize=0:duration=longest,apad,atrim=0:${duration.toFixed(4)}[out]`].join(';');
- await ff([...inputs, '-filter_complex', graph, '-map', '[out]', '-ar', '48000', '-c:a', 'pcm_f32le', pre], 'trộn âm thanh');
+ await ff([...inputs, '-filter_complex', graph, '-map', '[out]', '-t', duration.toFixed(4), '-ar', '48000', '-c:a', 'pcm_f32le', pre], 'trộn âm thanh');
// loudnorm 2 lượt → −14 LUFS
const m = parseLoudnorm(await ff(['-i', pre, '-af', `loudnorm=I=${TARGET_LUFS}:TP=-1.5:LRA=11:print_format=json`, '-f', 'null', '-'], 'đo loudness'));
diff --git a/engine/lib.mjs b/engine/lib.mjs
index 68022bc..2c42b24 100644
--- a/engine/lib.mjs
+++ b/engine/lib.mjs
@@ -175,8 +175,12 @@ export async function runHF(args, { cwd, onLine, timeoutMs = hfTimeoutMs(sceneDu
export function run(cmd, args, opts = {}) {
return new Promise((resolve) => {
- const p = spawn(cmd, args, { windowsHide: true, ...opts });
+ const { timeoutMs, ...rest } = opts;
+ const p = spawn(cmd, args, { windowsHide: true, ...rest });
let out = '';
+ const timer = timeoutMs ? setTimeout(() => { out += `
+[quá ${timeoutMs / 1000}s, dừng]`; p.kill('SIGKILL'); }, timeoutMs) : null;
+ p.on('close', () => timer && clearTimeout(timer));
p.stdout?.on('data', (d) => (out += d));
p.stderr?.on('data', (d) => (out += d));
p.on('error', (e) => resolve({ code: -1, out: String(e) }));