当前位置:   article > 正文

edge-tts微软文本转语音库

edge-tts

Edge-TTS是一个Python库,比较好用,直接pip安装。

pip install edge-tts

输入edge-tts,输出提示信息,安装完成。

  1. usage: edge-tts [-h] [-t TEXT] [-f FILE] [-v VOICE] [-l] [--rate RATE] [--volume VOLUME] [-O OVERLAPPING]
  2. [--write-media WRITE_MEDIA] [--write-subtitles WRITE_SUBTITLES] [--proxy PROXY]
  3. 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指令调整语速与音量

  1. edge-tts --voice zh-CN-YunxiNeural --rate=-4% --text "hello 大家好" --write-media hello1.mp3
  2. edge-tts --voice zh-CN-YunxiNeural --volume=-4% --text "hello 大家好" --write-media hello1.mp3

python进行批量文本转语音

  1. import edge_tts
  2. import asyncio
  3. TEXT = ""
  4. with open ('1.txt','rb') as f:
  5. data = f.read()
  6. TEXT = data.decode('utf-8')
  7. print(TEXT)
  8. voice = 'zh-CN-YunxiNeural'
  9. output = '4.mp3'
  10. rate = '-4%'
  11. volume = '+0%'
  12. async def my_function():
  13. tts = edge_tts.Communicate(text = TEXT,voice = voice,rate = rate,volume=volume)
  14. await tts.save(output)
  15. if __name__ == '__main__':
  16. 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个参数

  • text = TEXT 需要转换的文本
  • voice = voice 选择的语音
  • rate = rate 语速
  • volume=volume 音量
  • output 输出文件,一般是MP3文件

然后我们使用async进行异步请求,若是直接运行,会提示如下错误,因此,我们需要使用异步处理的方式。

  1. RuntimeWarning: coroutine 'Communicate.save' was never awaited
  2. tts.save(output)
  3. RuntimeWarning: Enable tracemalloc to get the object allocation traceback
https://github.com/rany2/edge-tts 项目文件地址
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/364367
推荐阅读
  

闽ICP备14008679号