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.

2.6 KB
Raw Blame
"""Chép lời 1 file âm thanh ngắn bằng faster-whisper (dùng cho đoạn mẫu giọng nhân bản + tự nghe lại).

  python engine/audio/whisper_ref.py <file.wav> [model] [lang]
In 1 dòng JSON: {"ok": true, "text": "...", "device": "cuda|cpu", "logprob": -0.2} hoặc {"ok": false, "error": "..."}.
Thử GPU trước, lỗi (thiếu CUDA/cuDNN) thì chạy CPU int8. Có bật VAD để không "nghe" ra chữ từ đoạn nhạc.

  python engine/audio/whisper_ref.py --vad <file>
Chỉ dò chỗ có tiếng người nói (Silero VAD đi kèm faster-whisper, nhanh, phân biệt được tiếng nói với nhạc nền):
{"ok": true, "speech": [[giây_đầu, giây_cuối], ...]}.
"""
import json
import sys

sys.stdout.reconfigure(encoding="utf-8", errors="replace")


def run(path, model, lang, device, compute):
    from faster_whisper import WhisperModel

    m = WhisperModel(model, device=device, compute_type=compute)
    segs, _info = m.transcribe(path, language=lang or None, beam_size=5, vad_filter=True, condition_on_previous_text=False)
    segs = list(segs)
    text = " ".join(s.text.strip() for s in segs).strip()
    lp = sum(s.avg_logprob for s in segs) / len(segs) if segs else None
    return text, lp


def vad(path):
    from faster_whisper import decode_audio
    from faster_whisper.vad import VadOptions, get_speech_timestamps

    sr = 16000
    audio = decode_audio(path, sampling_rate=sr)
    ts = get_speech_timestamps(audio, VadOptions(threshold=0.5, min_silence_duration_ms=250, speech_pad_ms=30), sampling_rate=sr)
    print(json.dumps({"ok": True, "speech": [[round(t["start"] / sr, 3), round(t["end"] / sr, 3)] for t in ts]}), flush=True)


def main():
    if sys.argv[1] == "--vad":
        try:
            vad(sys.argv[2])
        except Exception as e:  # noqa: BLE001
            print(json.dumps({"ok": False, "error": str(e)}, ensure_ascii=False), flush=True)
            sys.exit(1)
        return
    path = sys.argv[1]
    model = sys.argv[2] if len(sys.argv) > 2 else "large-v3"
    lang = sys.argv[3] if len(sys.argv) > 3 else "vi"
    err = None
    for device, compute in (("cuda", "float16"), ("cpu", "int8")):
        try:
            text, lp = run(path, model, lang, device, compute)
            print(json.dumps({"ok": True, "text": text, "device": device, "logprob": lp}, ensure_ascii=False), flush=True)
            return
        except Exception as e:  # noqa: BLE001
            err = f"{device}: {e}"
    print(json.dumps({"ok": False, "error": err}, ensure_ascii=False), flush=True)
    sys.exit(1)


if __name__ == "__main__":
    main()