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
// Cài môi trường cho LumReels Edit (chạy được nhiều lần, chỉ làm phần còn thiếu):
// 1. tìm Python 3.10–3.12 (KHÔNG cài gì vào Python hệ thống) → tạo edit/.venv
// 2. pip install edit/askeditor/requirements.txt (+ brotli để đọc font woff2)
// 3. có GPU NVIDIA (Windows/Linux) → thêm cuBLAS + cuDNN (wheel nvidia-*) để whisper chạy GPU; --cpu để bỏ qua
// 4. model MediaPipe (mặt, người) — tải từ kho chính thức, kiểm SHA-256
// 5. font Be Vietnam Pro TTF (ghép từ engine/fonts) cho phụ đề bản nháp
// 6. whisper large-v3 (~2,9 GB, vào ~/.cache/huggingface) — tải lần đầu
// Dòng cuối: READY | NEED_PYTHON | NEED_FFMPEG | NEED_NETWORK | NEED_NPM
// node edit/setup.mjs [--cpu] [--skip-model]
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import crypto from 'node:crypto';
import { spawnSync } from 'node:child_process';
import { EDIT, ROOT, PACK, VENV, venvPython, pyEnv, dirSize, fmtBytes, WHISPER_MODEL, READY_MARK } from './env.mjs';
const args = new Set(process.argv.slice(2));
const say = (s) => console.log(s);
const sh = (cmd, a, opts = {}) => spawnSync(cmd, a, { encoding: 'utf8', windowsHide: true, ...opts });
const has = (cmd) => { const r = sh(cmd, ['-version']); return !r.error && r.status === 0; };
const fail = (word, msg) => { say(msg); say(word); process.exit(word === 'NEED_NETWORK' ? 3 : 2); };
// ---- 0. ffmpeg -------------------------------------------------------------------------------------
if (!has('ffmpeg') || !has('ffprobe')) {
fail('NEED_FFMPEG', 'Thiếu ffmpeg/ffprobe. Windows: winget install Gyan.FFmpeg · macOS: brew install ffmpeg · Ubuntu: sudo apt install ffmpeg');
}
// ---- 1. Python 3.10–3.12 → venv ----------------------------------------------------------------------
const okVersion = (cmd, pre = []) => {
const r = sh(cmd, [...pre, '-c', 'import sys; print("%d.%d" % sys.version_info[:2])']);
if (r.error || r.status !== 0) return null;
const v = r.stdout.trim();
return ['3.10', '3.11', '3.12'].includes(v) ? v : null;
};
function findPython() {
if (process.env.LUMEDIT_PYTHON && okVersion(process.env.LUMEDIT_PYTHON)) return { cmd: process.env.LUMEDIT_PYTHON, pre: [] };
for (const v of ['3.12', '3.11', '3.10']) {
if (process.platform === 'win32' && okVersion('py', [`-${v}`])) return { cmd: 'py', pre: [`-${v}`] };
if (okVersion(`python${v}`)) return { cmd: `python${v}`, pre: [] };
}
for (const c of ['python3', 'python']) if (okVersion(c)) return { cmd: c, pre: [] };
return null;
}
let PY = venvPython();
if (!fs.existsSync(PY) || !okVersion(PY)) {
const base = findPython();
if (!base) {
fail('NEED_PYTHON', 'Cần Python 3.10, 3.11 hoặc 3.12 (thư viện mặt/người/giọng chưa có bản cho 3.13+).\n'
+ ' Windows: winget install Python.Python.3.12 · macOS: brew install [email protected] · Ubuntu: sudo apt install python3.12-venv\n'
+ ' Hoặc đặt LUMEDIT_PYTHON=<đường dẫn python>.');
}
say(`Tạo môi trường Python riêng: ${path.relative(ROOT, VENV)} (bằng ${[base.cmd, ...base.pre].join(' ')})…`);
const r = sh(base.cmd, [...base.pre, '-m', 'venv', '--clear', VENV], { stdio: 'inherit' });
if (r.status !== 0) fail('NEED_PYTHON', 'Không tạo được venv.');
PY = venvPython();
}
const pip = (a, what) => {
say(what);
const r = sh(PY, ['-m', 'pip', 'install', '--disable-pip-version-check', '--quiet', ...a], { stdio: 'inherit' });
if (r.status !== 0) fail('NEED_NETWORK', 'pip install lỗi. Kiểm tra mạng rồi chạy lại: node edit/setup.mjs');
};
// ---- 2. gói Python (chỉ cài lại khi requirements đổi) -------------------------------------------------
const reqFile = path.join(PACK, 'requirements.txt');
const stamp = path.join(VENV, '.lumedit-requirements.sha256');
const want = crypto.createHash('sha256').update(fs.readFileSync(reqFile)).update('brotli').digest('hex');
const importsOk = () => sh(PY, ['-c', 'import mediapipe, faster_whisper, cv2, PIL, numpy, brotli, fontTools']).status === 0;
if ((fs.existsSync(stamp) ? fs.readFileSync(stamp, 'utf8').trim() : '') !== want || !importsOk()) {
pip(['-r', reqFile, 'brotli'], 'Cài gói Python (lần đầu vài trăm MB)…');
fs.writeFileSync(stamp, want + '\n');
}
// ---- 3. CUDA cho whisper -------------------------------------------------------------------------------
const gpu = !args.has('--cpu') && process.platform !== 'darwin' && sh('nvidia-smi', ['-L']).status === 0;
if (gpu) {
const cudaOk = sh(PY, ['-c', 'import importlib.util as u, sys; sys.exit(0 if u.find_spec("nvidia.cublas") and u.find_spec("nvidia.cudnn") else 1)']).status === 0;
if (!cudaOk) pip(['nvidia-cublas-cu12==12.9.*', 'nvidia-cudnn-cu12==9.*'], 'Có GPU NVIDIA: cài cuBLAS + cuDNN vào venv (~2 GB) để chép lời bằng GPU…');
} else {
say(args.has('--cpu') ? 'Bỏ qua GPU (--cpu).' : 'Không thấy GPU NVIDIA: whisper chạy CPU (chậm hơn nhiều).');
}
// ---- 4. model MediaPipe ------------------------------------------------------------------------------
{
const r = sh(PY, ['-X', 'utf8', '-m', 'fx.core.models', 'fetch-vision'], { cwd: PACK, env: pyEnv(), stdio: 'inherit' });
if (r.status !== 0) fail('NEED_NETWORK', 'Không tải được model mặt/người.');
}
// ---- 5. font Be Vietnam Pro TTF -------------------------------------------------------------------------
{
const fontsSrc = path.join(ROOT, 'engine', 'fonts');
if (!fs.existsSync(path.join(fontsSrc, 'be-vietnam-pro-vietnamese-700-normal.woff2'))) {
fail('NEED_NPM', 'Thiếu engine/fonts (chưa chạy npm install ở thư mục gốc LumReels).');
}
const dest = path.join(PACK, 'fonts', 'BeVietnamPro');
const r = sh(PY, ['-X', 'utf8', path.join(EDIT, 'py', 'fonts.py'), fontsSrc, dest], { env: pyEnv(), stdio: 'inherit' });
if (r.status !== 0) fail('NEED_PYTHON', 'Không dựng được font Be Vietnam Pro.');
const lic = path.join(ROOT, 'node_modules', '@fontsource', 'be-vietnam-pro', 'LICENSE');
if (fs.existsSync(lic)) fs.copyFileSync(lic, path.join(dest, 'OFL.txt'));
}
// ---- 6. whisper -------------------------------------------------------------------------------------
let modelDir = '';
if (!args.has('--skip-model')) {
say(`Model chép lời: ${WHISPER_MODEL} (lần đầu tải ~2,9 GB vào bộ nhớ đệm Hugging Face)…`);
const r = sh(PY, ['-X', 'utf8', '-c', `from faster_whisper import download_model; print(download_model(${JSON.stringify(WHISPER_MODEL)}))`],
{ env: pyEnv(), stdio: ['ignore', 'pipe', 'inherit'] });
if (r.status !== 0) fail('NEED_NETWORK', 'Không tải được model whisper.');
modelDir = r.stdout.trim().split(/\r?\n/).pop();
}
// ---- dung lượng ------------------------------------------------------------------------------------------
const venvB = dirSize(VENV);
const modelsB = dirSize(path.join(PACK, 'models'));
const fontsB = dirSize(path.join(PACK, 'fonts', 'BeVietnamPro'));
let whisperB = 0;
if (modelDir) {
// snapshot trên Linux/macOS là symlink tới blobs: đo cả thư mục model trong cache
const repoDir = modelDir.split(/[\\/]snapshots[\\/]/)[0];
whisperB = dirSize(repoDir);
}
say('');
say('Dung lượng:');
say(` ${path.relative(ROOT, VENV)}: ${fmtBytes(venvB)}${gpu ? ' (gồm cuBLAS + cuDNN)' : ''}`);
say(` model mặt/người (${path.relative(ROOT, path.join(PACK, 'models'))}): ${fmtBytes(modelsB)}`);
say(` font Be Vietnam Pro: ${fmtBytes(fontsB)}`);
if (modelDir) say(` whisper ${WHISPER_MODEL}: ${fmtBytes(whisperB)} (${modelDir.replace(os.homedir(), '~')})`);
say(` tổng: ${fmtBytes(venvB + modelsB + fontsB + whisperB)}`);
// ---- kiểm lần cuối + dấu READY (venvReady() đọc dấu này) ---------------------------------------------------------
{
const r = sh(PY, ['-X', 'utf8', '-c', 'import mediapipe, faster_whisper, cv2, PIL, numpy, fontTools, ctranslate2; print("ok")'], { env: pyEnv() });
if (r.status !== 0) fail('NEED_PYTHON', 'Gói Python chưa đủ: ' + (r.stderr || '').trim().split(/\r?\n/).pop());
const files = [
...['blaze_face_short_range.tflite', 'selfie_segmenter.tflite', 'face_landmarker.task'].map((f) => path.join(PACK, 'models', f)),
...[600, 700, 800].map((w) => path.join(PACK, 'fonts', 'BeVietnamPro', `BeVietnamPro-${w}.ttf`)),
...(modelDir ? [path.join(modelDir, 'model.bin')] : []),
];
const lost = files.filter((f) => !fs.existsSync(f));
if (lost.length) fail('NEED_NETWORK', 'Thiếu file sau khi cài: ' + lost.join(', '));
if (!modelDir) say('(--skip-model: chưa ghi dấu READY vì chưa có model whisper)');
else fs.writeFileSync(READY_MARK, JSON.stringify({ at: new Date().toISOString(), whisper: WHISPER_MODEL, gpu, files }, null, 1));
}
say(`PYTHON ${PY}`);
say('READY');