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.
"""Đọc danh sách câu bằng VieNeu-TTS (chạy trên máy), nạp mô hình 1 lần.
python engine/audio/tts_vieneu.py <jobs.json>
jobs.json: [{"text": "...", "out": "đường/dẫn.wav", "voice": "Hải Đăng"}, ...]
Giọng nhân bản: thay "voice" bằng "clone": {"wav": "ref.wav", "npz": "ref.npz", "text": "lời đoạn mẫu"}.
- có ref.npz (speaker embedding + mã âm đã mã hoá sẵn) → dùng thẳng, không mã hoá lại;
- chưa có → encode_reference(ref.wav) rồi ghi ref.npz cho lần sau (VieNeu v3 Turbo trả (speaker_emb, codes));
- bản VieNeu không mã hoá kiểu đó → infer(ref_audio=wav, ref_text=text) mỗi câu.
In 1 dòng JSON mỗi câu xong: {"out": ..., "ok": true} hoặc {"out": ..., "ok": false, "error": ...}.
"""
import json
import os
import sys
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
def load_clone(tts, c, cache):
"""Giọng nhân bản → kwargs cho tts.infer (cache trong tiến trình theo đường dẫn ref.wav)."""
key = c["wav"]
if key in cache:
return cache[key]
import numpy as np
kw = None
npz = c.get("npz")
if npz and os.path.exists(npz):
try:
d = np.load(npz, allow_pickle=False)
kw = {"voice": {"speaker_emb": d["speaker_emb"], "codes": d["codes"] if int(d["has_codes"]) else None}}
except Exception: # noqa: BLE001 file hỏng → mã hoá lại
kw = None
if kw is None:
try:
enc = tts.encode_reference(c["wav"])
if isinstance(enc, tuple) and len(enc) == 2 and enc[0] is not None:
emb, codes = enc
emb = np.asarray(emb, dtype=np.float32)
codes_arr = np.zeros(0, dtype=np.int64) if codes is None else np.asarray(codes, dtype=np.int64)
if npz:
tmp = npz + ".tmp.npz"
np.savez(tmp, speaker_emb=emb, codes=codes_arr, has_codes=np.int8(codes is not None))
os.replace(tmp, npz)
kw = {"voice": {"speaker_emb": emb, "codes": None if codes is None else codes_arr}}
except Exception as e: # noqa: BLE001
print(json.dumps({"warn": f"encode_reference lỗi, dùng ref_audio: {e}"}, ensure_ascii=False), flush=True)
if kw is None:
kw = {"ref_audio": c["wav"], "ref_text": c.get("text") or None}
cache[key] = kw
return kw
def main():
with open(sys.argv[1], encoding="utf-8") as f:
jobs = json.load(f)
from vieneu import Vieneu
tts = Vieneu()
clones = {}
for j in jobs:
err = None
for _ in range(3): # VieNeu thỉnh thoảng lỗi, thử lại
try:
if j.get("clone"):
kw = load_clone(tts, j["clone"], clones)
else:
kw = {"voice": j.get("voice") or None}
tts.save(tts.infer(j["text"], **kw), j["out"])
err = None
break
except Exception as e: # noqa: BLE001
err = str(e)
print(json.dumps({"out": j["out"], "ok": err is None, "error": err}, ensure_ascii=False), flush=True)
if __name__ == "__main__":
main()