
edge-tts实战:免费语音合成,零API Key,让你的AI Agent开口说话
edge-tts技能实战教程:微软Neural TTS引擎,74种语言300+声音,零API Key零费用。附ClawHub安装、语速调节、中文声音推荐、踩坑记录。
📋 实验室验证报告
edge-tts实战:免费语音合成,零API Key,让你的AI Agent开口说话
语音合成(TTS)技术已经相当成熟,但大多数高质量的TTS服务都需要注册账户、申请API Key,还有按字符计费的使用成本。edge-tts改变了这个局面——它基于Microsoft Edge浏览器的语音合成引擎,完全免费,不需要任何API Key,音质接近商业级别。
什么是edge-tts?
edge-tts是一个Python库,利用Microsoft Edge浏览器内置的TTS功能。它的主要特点:完全免费(不需要API Key,不需要注册)、高质量音频(微软Neural TTS引擎,音质自然)、多语言支持(超过300种声音,覆盖中英日韩等数十种语言)、多种格式输出(MP3、WAV等)、异步支持(原生支持async/await)。
安装
pip install edge-tts
edge-tts --version
基本使用
命令行快速使用:
edge-tts --text "你好,我是你的AI助理。" --write-media output.mp3
edge-tts --voice zh-CN-XiaoxiaoNeural --text "今天天气真好!" --write-media output.mp3
edge-tts --list-voices | grep zh-CN
edge-tts --voice zh-CN-XiaoxiaoNeural --rate +20% --pitch +5Hz --text "速度快一点" --write-media output.mp3
在Python中使用
基本异步用法:
import asyncio
import edge_tts
async def text_to_speech(text: str, output_file: str, voice: str = "zh-CN-XiaoxiaoNeural"):
communicate = edge_tts.Communicate(text, voice)
await communicate.save(output_file)
asyncio.run(text_to_speech("你好,世界!", "output.mp3"))
流式输出(低延迟场景)——TTS引擎开始合成的同时就可以开始播放,显著降低感知延迟:
async def stream_tts(text: str, voice: str = "zh-CN-XiaoxiaoNeural"):
communicate = edge_tts.Communicate(text, voice)
async for chunk in communicate.stream():
if chunk["type"] == "audio":
yield chunk["data"]
集成到AI Agent工作流
场景一:让Agent的文字回答有声音
class SpeakingAgent:
def __init__(self, voice="zh-CN-XiaoxiaoNeural"):
self.voice = voice
async def speak(self, text: str):
communicate = edge_tts.Communicate(text, self.voice)
await communicate.save("/tmp/agent_response.mp3")
subprocess.run(["afplay", "/tmp/agent_response.mp3"]) # macOS
场景二:多语言自动切换
VOICE_MAP = {
"zh-cn": "zh-CN-XiaoxiaoNeural",
"zh-tw": "zh-TW-HsiaoChenNeural",
"en": "en-US-JennyNeural",
"ja": "ja-JP-NanamiNeural",
}
async def smart_tts(text: str, output_file: str):
lang = langdetect.detect(text)
voice = VOICE_MAP.get(lang, "en-US-JennyNeural")
communicate = edge_tts.Communicate(text, voice)
await communicate.save(output_file)
推荐声音
中文声音:zh-CN-XiaoxiaoNeural(晓晓,活泼自然)、zh-CN-YunxiNeural(云希,沉稳专业)、zh-CN-XiaoyiNeural(晓伊,温柔亲切)、zh-TW-HsiaoChenNeural(台湾中文)、zh-HK-HiuGaaiNeural(粤语)。
英文声音:en-US-JennyNeural(美式英语,自然流畅)、en-US-GuyNeural(美式英语,专业清晰)、en-GB-SoniaNeural(英式英语,优雅正式)。
注意事项
- 需要网络连接:edge-tts需要访问微软的服务,不支持完全离线使用
- 速率限制:高频率请求可能会被限制,生产环境建议添加请求间隔
- 商业使用:大规模商业使用前建议了解微软服务条款
- 字符限制:单次请求建议控制在3000字以内
完整示例:带语音的Claude助理
import asyncio, edge_tts, anthropic, subprocess
client = anthropic.Anthropic()
async def speak(text: str, voice: str = "zh-CN-XiaoxiaoNeural"):
communicate = edge_tts.Communicate(text, voice)
await communicate.save("/tmp/response.mp3")
subprocess.run(["afplay", "/tmp/response.mp3"])
async def chat_with_voice(user_message: str):
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": user_message}]
)
reply = response.content[0].text
print(f"助理:{reply}")
await speak(reply)
return reply
asyncio.run(chat_with_voice("今天的AI行业有什么新动态?"))
结语
edge-tts是一个被严重低估的工具。免费、易用、音质出色,是给AI Agent添加语音能力的最低门槛选择。现在就试试:pip install edge-tts,然后让你的Agent开口说话。
⚙️ 安装与赋能
clawhub install edge-tts-free-speech-synthesis-zero-api-key安装后在你的 Agent 配置中启用此技能,重启 Agent 即可生效。