Mozilla TTS 已经停止更新,所以这里用 Coqui TTS。
Coqui TTS 是一个由AI驱动的文本转语音合成平台,能够将书面文本转换为自然语音,由先进的XTTS V2模型提供支持。
Coqui TTS 源于 Mozilla TTS 项目,经过不断迭代优化,已成为独立且成熟的 AI 语音合成框架。它采用多种最先进的 TTS 模型,如 Tacotron2、Glow-TTS、VITS 等,能生成高度自然的语音,甚至支持特定音色克隆,适用于各种场景,如语音助手、智能客服、播客配音等。

官网:
开源地址:
https://github.com/coqui-ai/TTS
这里在Ubuntu24.04上安装,首先检查当前 Python 版本
python3 --version
最新版本的 TTS(如 0.15+、0.20+、0.22.0 等)要求 Python 版本在 >=3.9, <3.12 范围内。
添加 deadsnakes PPA(提供多个 Python 版本)
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
安装 Python 3.10
sudo apt install -y python3.10 python3.10-venv python3.10-dev python3.10 --version
创建基于 Python 3.10 的虚拟环境
python3.10 -m venv coqui-tts-env
激活虚拟环境
source coqui-tts-env/bin/activate
退出虚拟环境
deactivate
升级 pip
pip install --upgrade pip
安装 TTS
pip install TTS
转英文
tts --text "hello world" --out_path test.wav
关于专中文
首先查看 TTS 0.22.0 实际支持的中文模型
tts --list_models | grep -A3 -B1 "zh-CN"
打印
33: tts_models/uk/mai/vits 34: tts_models/zh-CN/baker/tacotron2-DDC-GST 35: tts_models/nl/mai/tacotron2-DDC 36: tts_models/nl/css10/vits 37: tts_models/de/thorsten/tacotron2-DCA
由于下载受限,我们可以手动下载模型,查看所有模型下载地址
https://github.com/coqui-ai/TTS/blob/dev/TTS/.models.json
例如我们要下载中文的
https://coqui.gateway.scarf.sh/v0.10.1_models/tts_models/zh-CN/baker/tacotron2-DDC-GST.zip
下载后,找到缓存目录:
Windows: C:\Users\<你的用户名>\AppData\Local\tts\ Linux: ~/.local/share/tts/ Mac: ~/Library/Application Support/tts/
执行命令,进行转换
tts --model_name "tts_models/zh-CN/baker/tacotron2-DDC-GST" --text "它具备强大的多语言识别能力,能处理多种不同口音和背景噪音下的语音内容,将其准确地转换为文本。" --out_path test_zh.wav
关于报错
Traceback (most recent call last):
File "/root/coqui-tts-env/bin/tts", line 7, in <module>
sys.exit(main())
File "/root/coqui-tts-env/lib/python3.10/site-packages/TTS/bin/synthesize.py", line 423, in main
synthesizer = Synthesizer(
File "/root/coqui-tts-env/lib/python3.10/site-packages/TTS/utils/synthesizer.py", line 93, in __init__
self._load_tts(tts_checkpoint, tts_config_path, use_cuda)
File "/root/coqui-tts-env/lib/python3.10/site-packages/TTS/utils/synthesizer.py", line 192, in _load_tts
self.tts_model.load_checkpoint(self.tts_config, tts_checkpoint, eval=True)
File "/root/coqui-tts-env/lib/python3.10/site-packages/TTS/tts/models/base_tacotron.py", line 105, in load_checkpoint
state = load_fsspec(checkpoint_path, map_location=torch.device("cpu"), cache=cache)
File "/root/coqui-tts-env/lib/python3.10/site-packages/TTS/utils/io.py", line 58, in load_fsspec
return torch.load(f, map_location=map_location, **kwargs)
File "/root/coqui-tts-env/lib/python3.10/site-packages/torch/serialization.py", line 1529, in load
raise pickle.UnpicklingError(_get_wo_message(str(e))) from None
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.
(1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
(2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
WeightsUnpickler error: Unsupported global: GLOBAL collections.defaultdict was not an allowed global by default. Please use `torch.serialization.add_safe_globals([collections.defaultdict])` or the `torch.serialization.safe_globals([collections.defaultdict])` context manager to allowlist this global if you trust this class/function.
Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.这个错误是因为 PyTorch 2.6 (2026年最新版本) 引入了一项重大的安全变更:torch.load 的默认参数 weights_only 从 False 变为了 True。
由于 tacotron2-DDC-GST 模型在保存时包含了自定义类(如 TTS.utils.radam.RAdam),PyTorch 的安全检查机制拦截了这些“非简单类型”的加载。
你需要根据报错提示,修改 Coqui TTS 的模型加载函数。
打开报错文件:/root/coqui-tts-env/lib/python3.10/site-packages/TTS/utils/io.py
找到 load_fsspec 函数(大约在第 54 行)。
将 torch.load 调用修改为显式指定 weights_only=False:
# 修改前 return torch.load(f, map_location=map_location, **kwargs) # 修改后 return torch.load(f, map_location=map_location, weights_only=False, **kwargs)
然后再次执行,会在目录生成wav文件。
Java小强
未曾清贫难成人,不经打击老天真。
自古英雄出炼狱,从来富贵入凡尘。
发表评论: