行业知识
Python调用edge-tts实现在线文字转语音
2026-11-19 03:53:48
来源:互联网转载
edge-tts是一个 Python 模块,允许通过Python代码或命令的方式使用 Microsoft Edge 的在线文本转语音服务。
项目源码
GitHub - rany2/edge-tts: Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API keyUse Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key - rany2/edge-ttshttps://github.com/rany2/edge-tts
安装
pip install edge-tts
用法
命令行方式
- --write-media:输出音频
- --write-subtitles:输出字幕
edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
选项检查可用的声音
edge-tts --list-voices
改变声音
- --voice:指定声音
edge-tts --voice zh-CN-XiaoxiaoNeural --text "君不见黄河之水天上来" --write-media hello.mp3 --write-subtitles hello.vtt
改变速率、音量和音高
edge-tts --rate=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vttedge-tts --volume=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vttedge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
播放音频
edge-playback
edge-playback 用于播放生成的语音。它采用与 edge-tts 相同的参数。
Python代码方式
文字转音频
import asyncioimport edge_ttsTEXT = "Hello World!"VOICE = "en-GB-SoniaNeural"OUTPUT_FILE = "test.mp3"async def amain() -> None: """Main function""" communicate = edge_tts.Communicate(TEXT, VOICE) await communicate.save(OUTPUT_FILE)if __name__ == "__main__": loop = asyncio.get_event_loop_policy().get_event_loop() try: loop.run_until_complete(amain()) finally: loop.close()
使用VoicesManager进行动态语音选择的示例
import asyncioimport randomimport edge_ttsfrom edge_tts import VoicesManagerTEXT = "Hoy es un buen día."OUTPUT_FILE = "spanish.mp3"async def amain() -> None: """Main function""" voices = await VoicesManager.create() voice = voices.find(Gender="Male", Language="es") # Also supports Locales # voice = voices.find(Gender="Female", Locale="es-AR") communicate = edge_tts.Communicate(TEXT, random.choice(voice)["Name"]) await communicate.save(OUTPUT_FILE)if __name__ == "__main__": loop = asyncio.get_event_loop_policy().get_event_loop() try: loop.run_until_complete(amain()) finally: loop.close()
流式传输来自TTS的音频数据
import asyncioimport edge_ttsTEXT = "Hello World!"VOICE = "en-GB-SoniaNeural"OUTPUT_FILE = "test.mp3"async def amain() -> None: """Main function""" communicate = edge_tts.Communicate(TEXT, VOICE) with open(OUTPUT_FILE, "wb") as file: async for chunk in communicate.stream(): if chunk["type"] == "audio": file.write(chunk["data"]) elif chunk["type"] == "WordBoundary": print(f"WordBoundary: {chunk}")if __name__ == "__main__": loop = asyncio.get_event_loop_policy().get_event_loop() try: loop.run_until_complete(amain()) finally: loop.close()
- --voice:指定声音
上一篇:快速查看文件大小的方法技巧
下一篇:吸引目光,突出核心要点!