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.

4.1 KB
Raw Blame
#!/usr/bin/env node
// Cắt 1 khung keyframe làm ảnh đại diện mẫu trong lưới UI (360×640 webp):
//   mẫu gốc "106"            → templates/thumbs/106.webp (có trong repo)
//   mẫu tự tạo "local.x/slug" → ~/.lumreels/packs/local.x/thumbs/template-slug.webp (ghi luôn thumb:true vào pack.json)
//   mẫu gói cộng đồng: từ chối (ảnh đi theo gói, không sửa gói của người khác)
//   node scripts/make-thumbs.mjs <mã mẫu> <jobId | file.mp4> [--at 5.5] [--force] [-o file.webp]
//   vd: node scripts/make-thumbs.mjs 106 261128071500-106-ab12      (lấy jobs/<jobId>/out.mp4)
// Mặc định cắt ở giây 5,5 (thường là lúc cảnh đã đủ phần tử); video ngắn hơn thì lấy gần cuối.
// Cần ffmpeg/ffprobe trong PATH. Đã có ảnh thì không ghi đè, trừ khi --force.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs, probe, run } from '../engine/lib.mjs';
import { resolveTemplate, parsePackRef, packDir, readPackManifest, TEMPLATES_DIR } from '../packs/registry.mjs';
import { lintWebp } from '../packs/lint.mjs';

const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const OUT_DIR = path.join(TEMPLATES_DIR, 'thumbs');
const { pos, opt } = parseArgs(process.argv.slice(2));
const [idArg, src] = pos;
if (!idArg || !src) {
  console.log('Dùng: node scripts/make-thumbs.mjs <templateId> <jobId | file.mp4> [--at 5.5] [--force]');
  process.exit(2);
}
const tpl = resolveTemplate(String(idArg));
if (!tpl) {
  if (/^\d{1,3}$/.test(String(idArg))) console.warn(`Cảnh báo: mẫu ${String(idArg).padStart(3, '0')} chưa có trong catalog.json (chạy node scripts/sync-catalog.mjs)`);
  else { console.error(`Không thấy mẫu "${idArg}" (mẫu gốc 1–3 chữ số hoặc mã gói tac-gia.ten-goi/slug đã cài)`); process.exit(2); }
}
if (tpl && tpl.source === 'pack') { console.error(`Mẫu ${tpl.ref} thuộc gói cộng đồng: ảnh đi theo gói, không sửa được.`); process.exit(2); }
const id = tpl ? tpl.ref : String(idArg).padStart(3, '0');
const packRef = tpl && tpl.source === 'local' ? parsePackRef(tpl.ref) : null;

let mp4 = src;
if (!/\.mp4$/i.test(src)) {
  if (!/^[\w-]{1,80}$/.test(src)) { console.error('jobId không hợp lệ'); process.exit(2); }
  mp4 = path.join(ROOT, 'jobs', src, 'out.mp4');
}
if (!fs.existsSync(mp4)) { console.error(`Không thấy ${mp4} (render job trước: node cli.mjs render <jobId>)`); process.exit(1); }

const out = typeof opt.o === 'string' ? path.resolve(opt.o)
  : packRef ? path.join(packDir(packRef.packId), 'thumbs', `template-${packRef.slug}.webp`) : path.join(OUT_DIR, `${id}.webp`);
if (fs.existsSync(out) && !opt.force) { console.error(`${out} đã có. Thêm --force để ghi đè.`); process.exit(1); }

const dur = Number((await probe(mp4))?.format?.duration) || 0;
let at = opt.at === undefined || opt.at === true ? 5.5 : Number(String(opt.at).replace(',', '.'));
if (!Number.isFinite(at) || at < 0) { console.error('--at phải là số giây'); process.exit(2); }
if (dur && at > dur - 0.1) at = Math.max(0, dur - 0.5);

fs.mkdirSync(path.dirname(out), { recursive: true });
const r = await run('ffmpeg', ['-v', 'error', '-y', '-ss', String(at), '-i', mp4, '-frames:v', '1',
  '-vf', 'scale=360:640:force_original_aspect_ratio=increase:flags=lanczos,crop=360:640', '-c:v', 'libwebp', '-quality', '80', out]);
if (r.code !== 0 || !fs.existsSync(out)) { console.error('ffmpeg lỗi:\n' + r.out); process.exit(1); }
if (packRef && typeof opt.o !== 'string') {
  const bad = lintWebp(fs.readFileSync(out)).errors;
  if (bad.length) { fs.rmSync(out, { force: true }); console.error('ảnh không qua kiểm: ' + bad[0]); process.exit(1); }
  const man = readPackManifest(packRef.packId);
  const items = man.items.map((x) => (x.kind === 'template' && x.slug === packRef.slug ? { ...x, thumb: true } : x));
  fs.writeFileSync(path.join(packDir(packRef.packId), 'pack.json'), JSON.stringify({ ...man, items }, null, 2));
}
console.log(`${path.relative(ROOT, out)} (${Math.round(fs.statSync(out).size / 1024)} KB, khung ${at}s của ${path.relative(ROOT, mp4)})`);