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.
#!/usr/bin/env node
// Dựng clip thử tổng hợp cho LumReels Edit khi chưa có clip người thật:
// nối hình (out.silent.mp4) + giọng sạch (voice-*.wav đặt đúng mốc t) của vài job có giọng,
// chèn 1 khoảng lặng dài và 1 câu nói hỏng (nói dở rồi nói lại từ đầu).
// node edit/test/make-synth.mjs <out.mp4> [--wide] (--wide: khung 16:9 nền mờ, để thử reframe)
// Cần thư mục jobs/ có các job dưới (dữ liệu máy dựng, không nằm trong repo).
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { run } from '../../engine/lib.mjs';
import { ROOT } from '../env.mjs';
const JOBS = path.join(ROOT, 'jobs');
const PARTS = [
{ job: '261127153822-100-08b4' }, // "Tháng nào cũng hụt tiền? …"
{ pause: 1.6 }, // lặng dài (hình đứng)
{ job: '261127140923-092-bcd1', cut: 1.35, then: 0.5 }, // nói dở "Để 5 triệu đồng trong…" rồi im
{ job: '261127140923-092-bcd1' }, // nói lại trọn câu
{ job: '261127134703-010-4701' },
{ job: '261127135408-059-bc9d' },
];
const outFile = path.resolve(process.argv[2] || path.join(ROOT, 'edits', '_synth', 'synth.mp4'));
const wide = process.argv.includes('--wide');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'lumedit-synth-'));
const ff = async (args) => { const r = await run('ffmpeg', ['-hide_banner', '-y', ...args]); if (r.code !== 0) throw new Error(r.out.slice(-800)); };
function voiceOf(job) {
const lines = JSON.parse(fs.readFileSync(path.join(JOBS, job, 'scene', 'audio.json'), 'utf8')).voice.lines;
const meta = Object.entries(JSON.parse(fs.readFileSync(path.join(JOBS, job, 'audio', 'voice-meta.json'), 'utf8'))).slice(-lines.length);
return lines.map((l, k) => ({ t: l.t, file: path.join(JOBS, job, 'audio', meta[k][0]), dur: meta[k][1].dur, text: l.text }));
}
const pieces = []; const truth = []; let at = 0; let lastJob = null;
for (const [n, p] of PARTS.entries()) {
const f = path.join(tmp, `p${n}.mp4`);
const vfFor = (pre) => (wide
? `[0:v]${pre}split[s0][s1];[s0]scale=1920:1080:force_original_aspect_ratio=increase,crop=1920:1080,boxblur=20[bg];[s1]scale=-2:1080[fg];[bg][fg]overlay=(W-w)/2:0,fps=30,format=yuv420p[v]`
: `[0:v]${pre}fps=30,format=yuv420p[v]`);
const vf = vfFor('');
if (p.pause) {
const src = path.join(JOBS, lastJob, 'out.silent.mp4');
await ff(['-sseof', '-0.1', '-i', src, '-f', 'lavfi', '-i', 'anullsrc=r=48000:cl=stereo', '-filter_complex', vfFor('tpad=stop_mode=clone:stop_duration=5,'),
'-map', '[v]', '-map', '1:a', '-t', String(p.pause), '-c:v', 'libx264', '-crf', '18', '-c:a', 'pcm_s16le', f.replace('.mp4', '.mov')]);
pieces.push(f.replace('.mp4', '.mov')); at += p.pause; continue;
}
const v = voiceOf(p.job); lastJob = p.job;
const len = p.cut ? p.cut + p.then : 8;
const ins = ['-i', path.join(JOBS, p.job, 'out.silent.mp4')]; const ch = []; const lab = [];
v.forEach((l, k) => {
if (l.t >= len) return;
ins.push('-i', l.file);
const keepDur = p.cut ? Math.max(0, p.cut - l.t) : l.dur;
ch.push(`[${k + 1}:a]aresample=48000,aformat=channel_layouts=stereo,atrim=0:${keepDur.toFixed(3)},afade=t=out:st=${Math.max(0, keepDur - 0.03).toFixed(3)}:d=0.03,adelay=${Math.round(l.t * 1000)}:all=1[a${k}]`);
lab.push(`[a${k}]`);
truth.push({ start: +(at + l.t).toFixed(3), end: +(at + l.t + keepDur).toFixed(3), text: l.text, partial: !!p.cut });
});
ch.push(`${lab.join('')}amix=inputs=${lab.length}:normalize=0,apad[a]`);
await ff([...ins, '-filter_complex', vf + ';' + ch.join(';'), '-map', '[v]', '-map', '[a]', '-t', String(len), '-c:v', 'libx264', '-crf', '18', '-c:a', 'pcm_s16le', f.replace('.mp4', '.mov')]);
pieces.push(f.replace('.mp4', '.mov')); at += len;
}
const list = path.join(tmp, 'list.txt');
fs.writeFileSync(list, pieces.map((p) => `file '${p.replace(/\\/g, '/')}'`).join('\n'));
fs.mkdirSync(path.dirname(outFile), { recursive: true });
await ff(['-f', 'concat', '-safe', '0', '-i', list, '-c:v', 'libx264', '-crf', '18', '-r', '30', '-c:a', 'aac', '-b:a', '192k', '-ar', '48000', outFile]);
fs.writeFileSync(outFile.replace(/\.mp4$/, '.truth.json'), JSON.stringify(truth, null, 1));
fs.rmSync(tmp, { recursive: true, force: true });
console.log(`${outFile} (${at.toFixed(2)} s, ${truth.length} câu; câu nói dở ở ${truth.find((t) => t.partial)?.start} s)`);