Python文本转语音(TTS)实战测评:gTTS、edge_tts与pyttsx3免费方案的效果对比与解析
在python中,已经有很多TTS相关的包,可以拿来即用,以下是我整理的一些简单免费的方案,仅供参考:
方案1:gTTS
1.1 介绍
google
的免费文本转语音API服务官方教程:https://gtts.readthedocs.io/en/latest/index.html
1.2 安装
pip install gTTS
1.3 简单使用
from gtts import gTTS
tts = gTTS(text='您好,我是一直可爱的小猪!', lang='zh')
tts.save('test.mp3')
1.4 效果展示
效果参考:https://blog.luler.top/d/7
方案2:edge_tts
2.1 介绍
微软
的免费文本转语音API服务2.2 安装
pip install edge_tts
2.3 简单使用
edge-tts --list-voice
列出所有支持的语音,以下是常用的中文语音:import asyncio
import edge_tts
async def ttsWork(text, file_path, voice="zh-CN-YunxiNeural", rate="+0%", volume="+0%", proxy=None):
communicator = edge_tts.Communicate(
text, # 需要转换的文本
voice=voice, # 配音员口音
rate=rate, # 语速控制
volume=volume, # 音量控制
proxy=proxy # 请求代理设置
)
await communicator.save(file_path)
#国内环境需要设置有效的proxy,国外服务器则不需要配置proxy
asyncio.run(ttsWork("您好,我是一直可爱的小猪!", "test.mp3", proxy="http://127.0.0.1:10808"))
2.4 效果展示
声音类型:zh-CN-XiaoxiaoNeural
效果参考:https://blog.luler.top/d/7
声音类型:zh-CN-YunxiNeural
效果参考:https://blog.luler.top/d/7
声音类型:zh-CN-YunyangNeural
效果参考:https://blog.luler.top/d/7
声音类型:zh-CN-XiaoxuanNeural
效果参考:https://blog.luler.top/d/7
声音类型:zh-CN-YunxiaNeural
效果参考:https://blog.luler.top/d/7
声音类型:zh-HK-WanLungNeural
效果参考:https://blog.luler.top/d/7
方案3:pyttsx3
3.1 介绍
3.2 安装
window安装TTS引擎
无需处理,一般默认支持
linux(debian系列)安装TTS引擎
sudo apt update && sudo apt install espeak-ng libespeak1
macOS安装TTS引擎
略过,没有mac电脑
开始安装python包
pip install pyttsx3
3.3 简单使用
import pyttsx3
# 初始化语音引擎
engine = pyttsx3.init()
# 设置要说的文本
text = "您好,我是一直可爱的小猪!"
# 设置语速,默认值是200
engine.setProperty('rate', 100)
# 设置音量,默认值是1.0
engine.setProperty('volume', 1.0)
# 保存到文件
engine.save_to_file(text, 'test.wav')
engine.runAndWait()
3.4 效果展示
效果参考:https://blog.luler.top/d/7
作者:计算机小手