赞
踩
edge-tts是一个 Python 模块,允许通过Python代码或命令的方式使用 Microsoft Edge 的在线文本转语音服务。
pip install edge-tts
edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
选项检查可用的声音
edge-tts --list-voices
改变声音
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.vtt
- edge-tts --volume=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
- edge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
播放音频
edge-playback
edge-playback 用于播放生成的语音。它采用与 edge-tts 相同的参数。
文字转音频
- import asyncio
- import edge_tts
-
- TEXT = "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 asyncio
- import random
-
- import edge_tts
- from edge_tts import VoicesManager
-
- TEXT = "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 asyncio
- import edge_tts
-
- TEXT = "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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。