赞
踩
Edge-TTS是一个Python库,比较好用,直接pip安装。
pip install edge-tts
输入edge-tts,输出提示信息,安装完成。
- usage: edge-tts [-h] [-t TEXT] [-f FILE] [-v VOICE] [-l] [--rate RATE] [--volume VOLUME] [-O OVERLAPPING]
- [--write-media WRITE_MEDIA] [--write-subtitles WRITE_SUBTITLES] [--proxy PROXY]
- edge-tts: error: one of the arguments -t/--text -f/--file -l/--list-voices is required
使用命令行Edge-TTS来产生语音。
edge-tts --text "Hello, world!" --write-media hello.mp3
edge-tts查看支持的语音。
edge-tts --list-voices
添加--voice命令,指定输出的语音。
edge-tts --voice zh-CN-YunxiNeural --text "hello 大家好" --write-media hello.mp3
添加rate与volume指令调整语速与音量
- edge-tts --voice zh-CN-YunxiNeural --rate=-4% --text "hello 大家好" --write-media hello1.mp3
- edge-tts --voice zh-CN-YunxiNeural --volume=-4% --text "hello 大家好" --write-media hello1.mp3
python进行批量文本转语音
- import edge_tts
- import asyncio
- TEXT = ""
- with open ('1.txt','rb') as f:
- data = f.read()
- TEXT = data.decode('utf-8')
- print(TEXT)
- voice = 'zh-CN-YunxiNeural'
- output = '4.mp3'
- rate = '-4%'
- volume = '+0%'
- async def my_function():
- tts = edge_tts.Communicate(text = TEXT,voice = voice,rate = rate,volume=volume)
- await tts.save(output)
- if __name__ == '__main__':
- asyncio.run(my_function())

首先我们导入edge_tts与asyncio库,并使用with open函数打开一个txt文件。txt便是我们要生成语音的文本文件。这里我们一般是输入的中文,因此,这里我们使用rb读取文件,然后通过decode函数转换一下,避免类似如下的问题
'gbk' codec can't decode byte 0xae in position 4: illegal multibyte sequence
然后我们定义好我们输入的5个参数
然后我们使用async进行异步请求,若是直接运行,会提示如下错误,因此,我们需要使用异步处理的方式。
- RuntimeWarning: coroutine 'Communicate.save' was never awaited
- tts.save(output)
- RuntimeWarning: Enable tracemalloc to get the object allocation traceback
https://github.com/rany2/edge-tts 项目文件地址
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。