|
edge-tts是一个Python模块,允许通过Python代码或命令的方式使用MicrosoftEdge的在线文本转语音服务。项目源码GitHub-rany2/edge-tts:UseMicrosoftEdge'sonlinetext-to-speechservicefromPythonWITHOUTneedingMicrosoftEdgeorWindowsoranAPIkeyUseMicrosoftEdge'sonlinetext-to-speechservicefromPythonWITHOUTneedingMicrosoftEdgeorWindowsoranAPIkey-rany2/edge-ttshttps://github.com/rany2/edge-tts安装pipinstalledge-tts用法命令行方式--write-media:输出音频--write-subtitles:输出字幕edge-tts--text"Hello,world!"--write-mediahello.mp3--write-subtitleshello.vtt选项检查可用的声音edge-tts--list-voices改变声音--voice:指定声音edge-tts--voicezh-CN-XiaoxiaoNeural--text"君不见黄河之水天上来"--write-mediahello.mp3--write-subtitleshello.vtt改变速率、音量和音高edge-tts--rate=-50%--text"Hello,world!"--write-mediahello.mp3--write-subtitleshello.vttedge-tts--volume=-50%--text"Hello,world!"--write-mediahello.mp3--write-subtitleshello.vttedge-tts--pitch=-50Hz--text"Hello,world!"--write-mediahello.mp3--write-subtitleshello.vtt播放音频edge-playbackedge-playback用于播放生成的语音。它采用与edge-tts相同的参数。Python代码方式文字转音频importasyncioimportedge_ttsTEXT="HelloWorld!"VOICE="en-GB-SoniaNeural"OUTPUT_FILE="test.mp3"asyncdefamain()->None:"""Mainfunction"""communicate=edge_tts.Communicate(TEXT,VOICE)awaitcommunicate.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进行动态语音选择的示例importasyncioimportrandomimportedge_ttsfromedge_ttsimportVoicesManagerTEXT="Hoyesunbuendía."OUTPUT_FILE="spanish.mp3"asyncdefamain()->None:"""Mainfunction"""voices=awaitVoicesManager.create()voice=voices.find(Gender="Male",Language="es")#AlsosupportsLocales#voice=voices.find(Gender="Female",Locale="es-AR")communicate=edge_tts.Communicate(TEXT,random.choice(voice)["Name"])awaitcommunicate.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的音频数据importasyncioimportedge_ttsTEXT="HelloWorld!"VOICE="en-GB-SoniaNeural"OUTPUT_FILE="test.mp3"asyncdefamain()->None:"""Mainfunction"""communicate=edge_tts.Communicate(TEXT,VOICE)withopen(OUTPUT_FILE,"wb")asfile:asyncforchunkincommunicate.stream():ifchunk["type"]=="audio":file.write(chunk["data"])elifchunk["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()
|
|