Edge TTS 是微软基于 Azure 语音服务推出的免费、开源、高质量文本转语音工具,支持多语言、多音色,可通过命令行、Python 代码或网页使用,适合视频配音、有声读物、无障碍场景。
一、核心特点
- 免费开源:无使用限制、无 API Key、完全免费
- 语音质量高:神经网络合成,接近真人,支持情感与语调
- 多语言多音色:支持 40+ 语言、300+ 音色,含中文普通话、粤语、台湾腔等
- 跨平台:Windows/macOS/Linux 通用,支持命令行、Python、网页
- 参数可调:语速、音量、音调、情感风格均可自定义
- 无需注册:开箱即用,联网即可
二、安装(Python 版)
1
2
3
4
5
| # 安装
pip install edge-tts
# 或用 pipx 隔离环境(推荐)
pipx install edge-tts
|
三、基础用法
1. 命令行(CLI)
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 查看所有可用语音(含中文)
edge-tts --list-voices | findstr "zh-CN" # Windows
edge-tts --list-voices | grep "zh-CN" # macOS/Linux
# 生成 MP3(推荐:云扬)
edge-tts --voice zh-CN-YunyangNeural --text "你好,这是 Edge TTS 生成的语音" --write-media output.mp3
# 调整语速(±50%)
edge-tts --voice zh-CN-YunyangNeural --text "测试语速" --rate +20% --write-media fast.mp3
edge-tts --voice zh-CN-YunyangNeural --text "测试语速" --rate -20% --write-media slow.mp3
# 调整音调(±50Hz)
edge-tts --voice zh-CN-YunyangNeural --text "测试音调" --pitch +10Hz --write-media high.mp3
|
2. Python 代码示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| import edge_tts
import asyncio
async def text_to_speech():
# 配置
VOICE = "zh-CN-YunyangNeural"
TEXT = "欢迎使用 Edge TTS,语音自然流畅,完全免费。"
OUTPUT_FILE = "output.mp3"
# 生成
communicate = edge_tts.Communicate(TEXT, VOICE, rate="+10%", pitch="+5Hz")
await communicate.save(OUTPUT_FILE)
print(f"音频已保存:{OUTPUT_FILE}")
if __name__ == "__main__":
asyncio.run(text_to_speech())
|
四、常用中文语音推荐
| 语音 ID | 名称 | 风格 | 适用场景 |
|---|
zh-CN-YunyangNeural | 云扬 | 专业播音腔 | 教程、旁白(推荐) |
zh-CN-XiaoxiaoNeural | 晓晓 | 温暖自然 | 有声书、客服 |
zh-CN-YunxiNeural | 云希 | 阳光少年 | 科普、短视频 |
zh-CN-YunjianNeural | 云健 | 沉稳男声 | 新闻、纪录片 |
五、高级用法
1. SSML 标记(控制发音、停顿、情感)
1
2
3
4
5
6
7
8
9
| TEXT = """
<speak>
<prosody rate="slow" pitch="+5Hz">你好</prosody>,
<break time="500ms"/>
<prosody rate="fast">欢迎使用 Edge TTS</prosody>。
<break time="1s"/>
<emphasis level="strong">这是强调的内容</emphasis>。
</speak>
"""
|
2. 批量生成(配合 Remotion 视频)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| # scripts/generate_audio_edge.py
import edge_tts
import asyncio
from pathlib import Path
AUDIO_DIR = Path("public/audio")
AUDIO_DIR.mkdir(exist_ok=True)
SCENES = [
{"id": "intro", "text": "欢迎来到本教程", "voice": "zh-CN-YunyangNeural"},
{"id": "main", "text": "今天我们学习...", "voice": "zh-CN-YunyangNeural"},
]
async def generate_audio(scene):
output = AUDIO_DIR / f"{scene['id']}.mp3"
if output.exists():
print(f"跳过:{output}")
return
communicate = edge_tts.Communicate(scene["text"], scene["voice"])
await communicate.save(output)
print(f"生成:{output}")
async def main():
tasks = [generate_audio(scene) for scene in SCENES]
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
|
六、与 Remotion 集成(音画同步)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| // audioConfig.ts
export const SCENES = [
{ id: "intro", audioFile: "intro.mp3", durationInFrames: 450 },
{ id: "main", audioFile: "main.mp3", durationInFrames: 600 },
];
// 视频组件
import { Audio, Sequence, staticFile } from "remotion";
import { SCENES } from "./audioConfig";
export const MyVideo = () => {
return (
<>
{SCENES.map((scene, i) => (
<Sequence key={scene.id} from={i * 450} durationInFrames={scene.durationInFrames}>
<SceneComponent id={scene.id} />
<Audio src={staticFile(`audio/${scene.audioFile}`)} />
</Sequence>
))}
</>
);
};
|
七、常见问题
- 需要联网吗?
是,Edge TTS 基于微软云服务,必须联网。 - 有使用限制吗?
无官方限制,但建议合理使用,避免高频请求。 - 语音不自然?
尝试不同音色(如 Yunyang),或用 SSML 调整语速/停顿。 - 生成慢?
批量生成时建议异步处理,或拆分长文本。