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.

3.1 KB
Raw Blame
#!/usr/bin/env node
// Thêm tiếng hàng loạt cho các job Lumfin đã render (tests/lumfin/results/*.json có stage "rendered").
//   node tools/add-audio-batch.mjs            liệt kê job THIẾU scene/audio.json + chạy `cli.mjs audio` cho job đã có
//   node tools/add-audio-batch.mjs --dry      chỉ liệt kê, không chạy gì
//   node tools/add-audio-batch.mjs --force    chạy lại cả job đã có tiếng mới hơn audio.json
//   node tools/add-audio-batch.mjs --only 009,039   chỉ các mẫu này
// Job thiếu audio.json: agent khác viết theo engine/AUTHORING.md §9 rồi chạy lại script.
import fs from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';

const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const RES = path.join(ROOT, 'tests', 'lumfin', 'results');
const args = process.argv.slice(2);
const dry = args.includes('--dry'), force = args.includes('--force');
const only = args.includes('--only') ? new Set(String(args[args.indexOf('--only') + 1] || '').split(',')) : null;

const ffHasAudio = (f) => {
  const r = spawnSync('ffprobe', ['-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=codec_name', '-of', 'csv=p=0', f], { encoding: 'utf8' });
  return r.status === 0 && r.stdout.trim().length > 0;
};

const missing = [], todo = [], fresh = [], broken = [];
for (const f of fs.readdirSync(RES).filter((x) => x.endsWith('.json')).sort()) {
  let r; try { r = JSON.parse(fs.readFileSync(path.join(RES, f), 'utf8')); } catch { broken.push(f); continue; }
  if (r.stage !== 'rendered' || !r.jobId) continue;
  if (only && !only.has(String(r.id))) continue;
  const dir = path.join(ROOT, 'jobs', r.jobId), aj = path.join(dir, 'scene', 'audio.json'), mp4 = path.join(dir, 'out.mp4');
  if (!fs.existsSync(mp4)) { broken.push(`${r.id} ${r.jobId}: không có out.mp4`); continue; }
  if (!fs.existsSync(aj)) { missing.push(r); continue; }
  const done = ffHasAudio(mp4) && fs.statSync(mp4).mtimeMs > fs.statSync(aj).mtimeMs;
  (done && !force ? fresh : todo).push(r);
}

console.log(`THIẾU scene/audio.json (${missing.length}) — cần viết theo engine/AUTHORING.md §9:`);
for (const r of missing) console.log(`  ${r.id}  jobs/${r.jobId}/scene/audio.json   (guidance: templates/guidance/${r.id}.md §9)`);
if (fresh.length) console.log(`\nĐã có tiếng, bỏ qua (${fresh.length}): ${fresh.map((r) => r.id).join(', ')}  (--force để làm lại)`);
if (broken.length) console.log(`\nLỗi/bỏ qua: ${broken.join('; ')}`);
console.log(`\nSẽ thêm tiếng (${todo.length}): ${todo.map((r) => r.id).join(', ') || '—'}`);
if (dry || !todo.length) process.exit(0);

const fails = [];
for (const r of todo) {
  console.log(`\n=== ${r.id} ${r.jobId}`);
  const p = spawnSync(process.execPath, [path.join(ROOT, 'cli.mjs'), 'audio', r.jobId], { cwd: ROOT, stdio: 'inherit' });
  if (p.status !== 0) fails.push(r.id);
}
console.log(`\nXong ${todo.length - fails.length}/${todo.length}.${fails.length ? ' Lỗi: ' + fails.join(', ') : ''}`);
process.exit(fails.length ? 1 : 0);