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.
// Tỉ lệ khung dùng chung cho cả 2 luồng (dựng từ mẫu + edit video). Cạnh ngắn luôn 1080 px nên luật cỡ chữ
// "tối thiểu 28px / chữ chính ≥ 40px trên khung 1080" giữ nguyên cho mọi tỉ lệ.
// 9:16 = 1080×1920 (Reels/Shorts/TikTok, mặc định) · 16:9 = 1920×1080 (YouTube) · 4:3 = 1440×1080 · 3:4 = 1080×1440 (feed)
export const RATIOS = {
'9:16': { width: 1080, height: 1920 },
'16:9': { width: 1920, height: 1080 },
'4:3': { width: 1440, height: 1080 },
'3:4': { width: 1080, height: 1440 },
};
export const RATIO_IDS = Object.keys(RATIOS);
export const DEFAULT_RATIO = '9:16';
/** "16:9" | "16x9" | "16/9" | "1920x1080" | null → mã tỉ lệ chuẩn; trống → 9:16; lạ → throw. */
export function normRatio(v) {
if (v === undefined || v === null || v === '') return DEFAULT_RATIO;
const s = String(v).trim().toLowerCase().replace(/\s+/g, '').replace(/[x×/]/, ':');
if (RATIOS[s]) return s;
const m = /^(\d{3,4}):(\d{3,4})$/.exec(s);
if (m) { const r = ratioOfDims(Number(m[1]), Number(m[2])); if (r) return r; }
throw new Error(`Tỉ lệ khung không hỗ trợ: "${v}" (chọn ${RATIO_IDS.join(', ')})`);
}
export const isRatio = (v) => Object.prototype.hasOwnProperty.call(RATIOS, v);
/** {width, height} của tỉ lệ. */
export function dimsOf(r) { return { ...RATIOS[isRatio(r) ? r : DEFAULT_RATIO] }; }
/** Kích thước đúng 1 tỉ lệ → mã; không khớp → null. */
export function ratioOfDims(w, h) {
for (const [id, d] of Object.entries(RATIOS)) if (d.width === Number(w) && d.height === Number(h)) return id;
return null;
}
/** Tỉ lệ gần nhất với khung w×h bất kỳ (clip nguồn). */
export function nearestRatio(w, h) {
const a = w / h;
let best = DEFAULT_RATIO; let bd = Infinity;
for (const [id, d] of Object.entries(RATIOS)) { const x = Math.abs(Math.log(a / (d.width / d.height))); if (x < bd) { bd = x; best = id; } }
return best;
}
export const isLandscape = (r) => { const d = dimsOf(r); return d.width > d.height; };
/**
* Vùng an toàn (phần của khung) để đặt chữ/đồ hoạ. 9:16 giữ như cũ (né nút của Reels/Shorts/TikTok: 120..960 px
* ngang, 10%..80% dọc). Khung khác không có lớp nút đè lên → lề đều ~6% ngang, 7% trên, dưới chừa chỗ phụ đề.
*/
export function safeArea(r) {
switch (isRatio(r) ? r : DEFAULT_RATIO) {
case '16:9': return { x0: 0.06, x1: 0.94, y0: 0.07, y1: 0.9 };
case '4:3': return { x0: 0.06, x1: 0.94, y0: 0.07, y1: 0.9 };
case '3:4': return { x0: 0.07, x1: 0.93, y0: 0.07, y1: 0.86 };
default: return { x0: 120 / 1080, x1: 960 / 1080, y0: 0.10, y1: 0.80 };
}
}