ChatTTS 中文文档 下载 App

ChatTTS 命令行模式——python examples/cmd/run.py 批量生成

命令行脚本

源码仓库中 examples/cmd/run.py 提供了命令行调用方式:

python examples/cmd/run.py "你的第一段文本" "你的第二段文本"

运行后会在当前目录生成 output_audio_0.mp3、output_audio_1.mp3 等文件。

注意事项

自己写命令行脚本

如果你想完全控制参数(说话人、温度、韵律标签等),建议自己写一个简单的 CLI:

import sys
import ChatTTS
import torch
import torchaudio

def main():
    chat = ChatTTS.Chat()
    chat.load(compile=False)
    rand_spk = chat.sample_random_speaker()

    texts = sys.argv[1:]
    if not texts:
        print("用法:python my_chattts.py 文本1 文本2 ...")
        return

    params = ChatTTS.Chat.InferCodeParams(
        spk_emb=rand_spk,
        temperature=0.3,
        top_P=0.7,
        top_K=20,
    )

    wavs = chat.infer(texts, params_infer_code=params)

    for i, wav in enumerate(wavs):
        tensor = torch.from_numpy(wav).unsqueeze(0)
        out = f"output_{i}.wav"
        torchaudio.save(out, tensor, 24000)
        print(f"已保存 {out}")

if __name__ == "__main__":
    main()

保存为 my_chattts.py 后:

python my_chattts.py "你好" "这是第二条" "这是第三条"

管道模式

如果你想从文件批量读取文本:

# texts.txt 每行一条文本
with open('texts.txt', 'r', encoding='utf-8') as f:
    texts = [line.strip() for line in f if line.strip()]

wavs = chat.infer(texts, params_infer_code=params)

第三方命令行工具