找回密码
 会员注册
查看: 30|回复: 0

Python调用edge-tts实现在线文字转语音

[复制链接]

4

主题

0

回帖

13

积分

新手上路

积分
13
发表于 2024-9-9 21:54:37 | 显示全部楼层 |阅读模式
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()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-9 05:49 , Processed in 1.387963 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表