ChatTTS 命令行模式——python examples/cmd/run.py 批量生成
命令行脚本
源码仓库中 examples/cmd/run.py 提供了命令行调用方式:
python examples/cmd/run.py "你的第一段文本" "你的第二段文本"
运行后会在当前目录生成 output_audio_0.mp3、output_audio_1.mp3 等文件。
注意事项
- 这个脚本只在源码仓库中有——用 PyPI 安装的 ChatTTS 包不包含示例脚本
- 首次运行同样会触发模型加载
- 输出格式是 MP3(脚本内置了 pydub 转换)
自己写命令行脚本
如果你想完全控制参数(说话人、温度、韵律标签等),建议自己写一个简单的 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)
第三方命令行工具
- ChatTTS-ui 项目的命令行工具支持更多参数:音色选择、语速、控制标签可视化
- Awesome-ChatTTS(社区列表)里收录了更多 CLI 工具:见 Amazing-ChatTTS社区