Edge-TTS 是一个基于微软 Azure Cognitive Services 的开源文本转语音(TTS)工具,支持 40+ 种语言、300+ 种声音,可生成自然流畅的语音,并支持调整语速、音量、音高等参数。本教程将带你从基础安装到高级应用,全面掌握 Edge-TTS 的使用方法。
一、安装 Edge-TTS
1. 基础安装(命令行工具)
通过 pip 安装 Edge-TTS 核心库,支持命令行操作:
pip install edge-tts
2. 推荐安装(命令行+播放工具)
若需要直接播放生成的语音,建议使用 pipx 安装(避免依赖冲突),并安装 mpv 播放器(Linux/macOS 用户):
pipx install edge-tts # 推荐,隔离环境
brew install mpv # macOS 安装 mpv
sudo apt install mpv # Linux(Ubuntu/Debian)安装 mpv
Windows 用户可直接使用 edge-playback 命令(无需额外安装播放器)。
二、基础使用:命令行操作
1. 生成语音文件
将文本转换为 MP3 文件,最简命令:
edge-tts --text "Hello, world!" --write-media hello.mp3
--text:要转换的文本内容(支持中文、英文等);--write-media:输出的音频文件路径(默认格式为 MP3)。
2. 指定语音风格
Edge-TTS 提供多种语音模型,可通过 --voice 参数选择:
# 中文普通话(微软晓晓)
edge-tts --voice zh-CN-XiaoyiNeural --text "大家好,欢迎使用 Edge-TTS!" --write-media hello_cn.mp3
# 陕西口音女生(微软小妮)
edge-tts --voice zh-CN-shaanxi-XiaoniNeural --text "大家好,欢迎关注我的公众号!" --write-media hello_sx_cn.mp3
# 台湾腔(微软小宇)
edge-tts --voice zh-TW-HsiaoYuNeural --text "大家好,歡迎關注我的粉絲專頁!" --write-media hello_tw_cn.mp3
查看所有可用语音:
edge-tts --list-voices
输出结果包含语音名称、性别、语言/地区及特点(如“友好、积极”)。
3. 调整语音参数
- 语速:用
--rate调整,格式为±百分比(如-50%表示减速 50%):edge-tts --voice zh-CN-YunyangNeural --rate -4% --text "你好,世界!" --write-media slow_hello.mp3 - 音量:用
--volume调整,格式为±百分比(如-50%表示降低音量 50%):edge-tts --voice zh-CN-YunyangNeural --volume -30% --text "你好,世界!" --write-media quiet_hello.mp3 - 音高:用
--pitch调整,格式为±Hz(如-50Hz表示降低音高 50Hz):edge-tts --voice zh-CN-YunyangNeural --pitch -50Hz --text "你好,世界!" --write-media low_pitch_hello.mp3
4. 生成字幕文件
用 --write-subtitles 生成与音频同步的 SRT/VTT 字幕(适合视频制作):
edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
三、Python 代码集成:灵活控制
Edge-TTS 支持通过 Python 代码调用,适合集成到应用程序(如语音助手、有声读物生成工具)。
1. 基础代码示例
import asyncio
from edge_tts import Communicate
async def generate_speech():
# 初始化 Communicate 对象(文本+语音模型)
communicate = Communicate("你好,世界!", voice="zh-CN-XiaoyiNeural")
# 保存为 MP3 文件
await communicate.save("hello_python.mp3")
# 运行异步函数
asyncio.run(generate_speech())
2. 流式传输(实时处理音频)
若需要实时处理音频(如直播场景),可使用 stream() 方法:
import asyncio
from edge_tts import Communicate
async def stream_audio():
communicate = Communicate("你好,世界!", voice="zh-CN-XiaoyiNeural")
with open("stream_hello.mp3", "wb") as file:
async for chunk in communicate.stream():
if chunk["type"] == "audio": # 只写入音频数据
file.write(chunk["data"])
asyncio.run(stream_audio())
3. 动态选择语音
通过 VoicesManager 获取所有语音列表,并根据条件(如性别、语言)筛选:
import asyncio
from edge_tts import VoicesManager
async def select_voice():
# 获取所有可用语音
voices = await VoicesManager.create()
# 筛选:女性、中文、普通话
target_voice = next((v for v in voices if v["Gender"] == "Female" and v["Locale"] == "zh-CN"), None)
if target_voice:
communicate = Communicate("你好,世界!", voice=target_voice["Name"])
await communicate.save("selected_voice.mp3")
else:
print("未找到符合条件的声音")
asyncio.run(select_voice())
四、常见问题解决
1. 网络依赖
Edge-TTS 依赖微软 Azure 在线服务,需保持网络稳定。若出现连接错误,可检查网络或使用代理:
edge-tts --text "Hello, world!" --proxy http://your_proxy:port --write-media hello.mp3
2. 长文本处理
对于超过 5000 字符的长文本,建议分段处理(每段 1000-2000 字符),避免请求超时:
import asyncio
from edge_tts import Communicate
async def process_long_text():
text = "这是一段很长的文本..." * 10 # 示例长文本
chunks = [text[i:i+1000] for i in range(0, len(text), 1000)] # 分段
for i, chunk in enumerate(chunks):
await Communicate(chunk, voice="zh-CN-XiaoyiNeural").save(f"chunk_{i}.mp3")
asyncio.run(process_long_text())
3. 错误处理
添加 try-except 捕获网络或参数错误:
import asyncio
from edge_tts import Communicate
async def safe_generate():
try:
await Communicate("测试文本", voice="zh-CN-XiaoyiNeural").save("test.mp3")
except Exception as e:
print(f"生成失败:{e}")
asyncio.run(safe_generate())
五、应用场景
- 有声读物/播客:将电子书、文章转换为语音,生成 MP3 文件;
- 教育/语言学习:制作发音练习音频,支持多语言切换;
- 智能客服:集成到 IVR 系统,提供自然语音交互;
- 视频制作:生成语音的同时输出 SRT 字幕,实现音画同步;
- 智能设备:为智能音箱、智能家居提供语音反馈。
通过本教程,你可以快速掌握 Edge-TTS 的基础操作与进阶功能,将其应用到各类语音相关项目中。如需更详细的信息,可参考 Edge-TTS 的 GitHub 仓库(rany2/edge-tts)。
6914

被折叠的 条评论
为什么被折叠?



