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.3 KB
Raw Blame
// Chạy edit/setup.mjs từ giao diện (nút "Cài ngay"): một lúc chỉ 1 lần cài, giữ ~200 dòng log cuối để trang web xem tiến độ.
// Dòng cuối của setup là chữ trạng thái: READY | NEED_PYTHON | NEED_FFMPEG | NEED_NETWORK | NEED_NPM.
import path from 'node:path';
import { spawn } from 'node:child_process';
import { EDIT, ROOT } from './env.mjs';

const WORDS = new Set(['READY', 'NEED_PYTHON', 'NEED_FFMPEG', 'NEED_NETWORK', 'NEED_NPM']);
const MAX_LINES = 200;

// Bước người dùng thấy, đoán theo dòng log setup in ra
const STEPS = [
  [/^Thiếu ffmpeg/i, 'Kiểm tra ffmpeg'],
  [/^Tạo môi trường Python/i, 'Tạo môi trường Python'],
  [/^Cài gói Python/i, 'Cài gói Python (vài trăm MB)'],
  [/cuBLAS|cuDNN/i, 'Cài phần chạy bằng GPU (~2 GB)'],
  [/fetching the .*model/i, 'Tải model nhận mặt, người'],
  [/fonts ready/i, 'Chuẩn bị font phụ đề'],
  [/^Model chép lời/i, 'Tải model chép lời (~3 GB, lâu nhất)'],
  [/^Dung lượng/i, 'Kiểm tra lần cuối'],
];

let run = null; // {proc, lines[], step, startedAt, endedAt, code, word}

function push(line) {
  const s = line.replace(/\x1b\[[0-9;]*m/g, '').trimEnd();
  if (!s.trim()) return;
  for (const [re, step] of STEPS) if (re.test(s.trim())) run.step = step;
  const w = s.trim();
  if (WORDS.has(w)) run.word = w;
  run.lines.push(s);
  if (run.lines.length > MAX_LINES) run.lines.splice(0, run.lines.length - MAX_LINES);
}

function feed(stream) {
  let buf = '';
  stream.setEncoding('utf8');
  stream.on('data', (d) => {
    buf += d;
    const parts = buf.split(/\r\n|\n|\r/); // pip vẽ thanh tiến độ bằng \r
    buf = parts.pop();
    for (const p of parts) push(p);
  });
  stream.on('end', () => { if (buf) push(buf); buf = ''; });
}

export function installStatus() {
  if (!run) return { running: false, started: false };
  const running = run.code === null;
  // Lỗi cần người dùng tự làm (cài Python/ffmpeg): dòng ngay trước chữ trạng thái là lời hướng dẫn
  let hint = '';
  if (!running && run.word && run.word !== 'READY') {
    const i = run.lines.lastIndexOf(run.word);
    hint = run.lines.slice(Math.max(0, i - 3), i).join('\n');
  }
  return {
    running, started: true, step: run.step,
    ok: !running && run.code === 0 && run.word === 'READY',
    word: run.word, code: run.code, hint,
    elapsedS: Math.round(((run.endedAt || Date.now()) - run.startedAt) / 1000),
    log: run.lines.slice(-40),
  };
}

/** Bắt đầu cài. Trả {started:true} hoặc {started:false, reason}. */
export function startInstall() {
  if (run && run.code === null) return { started: false, reason: 'đang cài rồi' };
  run = { proc: null, lines: [], step: 'Bắt đầu', startedAt: Date.now(), endedAt: 0, code: null, word: '' };
  const proc = spawn(process.execPath, [path.join(EDIT, 'setup.mjs')], {
    cwd: ROOT, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'],
    env: { ...process.env, PYTHONUNBUFFERED: '1', PIP_PROGRESS_BAR: 'off' },
  });
  run.proc = proc;
  feed(proc.stdout); feed(proc.stderr);
  const cur = run;
  proc.on('error', (e) => { push('Không chạy được setup: ' + e.message); cur.code = -1; cur.endedAt = Date.now(); });
  proc.on('close', (code) => { if (cur.code === null) { cur.code = code ?? -1; cur.endedAt = Date.now(); } });
  return { started: true };
}