前言:
现时咱们对“python 语音聊天”大概比较讲究,看官们都想要了解一些“python 语音聊天”的相关知识。那么小编在网上汇集了一些对于“python 语音聊天””的相关资讯,希望咱们能喜欢,各位老铁们快快来了解一下吧!1.1 简介
edge-tts是一个Python模块,允许在Python代码中使用Microsoft Edge的在线文本转语音服务,或使用提供的edge-tts或edge-playback命令实现文字转语音。
网上有很多文本转语音服务,但多为收费的,而微软此模块包是免费的使用的。本模块是用Azure Cognitive Services来实现文本到语音转换的,使用中只要调用相应的API即可实现,且支持多种语言的声音,如中文、英文等。
为了使用此文本语音转换模块库,需要先行安装该模块。
1.2 安装
以笔者为例,我是在windows10上安装的,Python为3.9.10版。打开命令行窗口输入如下命令:
pip install edge-tts
等待下载并自动安装,完成完整后命令行窗口类似如下所示:
C:\Users\Administrator>pip install edge-ttsCollecting edge-ttsDownloading edge_tts-6.1.3-py3-none-any.whl (26 kB)Collecting aiohttp>=3.8.0Downloading aiohttp-3.8.4-cp39-cp39-win_amd64.whl (323 kB)---------------------------------------- 323.6/323.6 kB 35.1 kB/s eta 0:00:00Collecting yarl<2.0,>=1.0Downloading yarl-1.8.2-cp39-cp39-win_amd64.whl (56 kB)---------------------------------------- 56.8/56.8 kB 33.5 kB/s eta 0:00:00Requirement already satisfied: charset-normalizer<4.0,>=2.0 in d:\program files\python39\lib\site-packages (from aiohttp>=3.8.0->edge-tts) (2.1.1)Collecting async-timeout<5.0,>=4.0.0a3Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)Collecting multidict<7.0,>=4.5Downloading multidict-6.0.4-cp39-cp39-win_amd64.whl (28 kB)Collecting aiosignal>=1.1.2Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)Collecting attrs>=17.3.0Downloading attrs-22.2.0-py3-none-any.whl (60 kB)---------------------------------------- 60.0/60.0 kB 27.9 kB/s eta 0:00:00Collecting frozenlist>=1.1.1Downloading frozenlist-1.3.3-cp39-cp39-win_amd64.whl (34 kB)Requirement already satisfied: idna>=2.0 in d:\program files\python39\lib\site-packages (from yarl<2.0,>=1.0->aiohttp>=3.8.0->edge-tts) (3.4)Installing collected packages: multidict, frozenlist, attrs, async-timeout, yarl, aiosignal, aiohttp, edge-ttsSuccessfully installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 attrs-22.2.0 edge-tts-6.1.3 frozenlist-1.3.3 multidict-6.0.4 yarl-1.8.2[notice] A new release of pip available: 22.3.1 -> 23.0.1[notice] To update, run: python.exe -m pip install --upgrade pip
提示:如果你只想使用edge-tts和edge-playback命令,最好使用pipx安装:
pipx install edge-tts
edge-playback只是对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
1.3 示例
使用edge-tts命令实现文本转语音:
edge-tts --text "Every people of china should read and fight for China Great!" --write-media china.mp3
解释参考下图:
1.4 语言
该tts库支持较多语言的选择,可以输入如下命令查看所支持的语言语音:
edge-tts --list-voices
命令行下输入上述命令回车,输出的内如如下:
C:\Users\Administrator>edge-tts --list-voicesName: af-ZA-AdriNeuralGender: FemaleName: af-ZA-WillemNeuralGender: Male……Name: zh-CN-XiaoxiaoNeuralGender: FemaleName: zh-CN-XiaoyiNeuralGender: FemaleName: zh-CN-YunjianNeuralGender: MaleName: zh-CN-YunxiNeuralGender: MaleName: zh-CN-YunxiaNeuralGender: Male……Name: zh-HK-HiuGaaiNeuralGender: FemaleName: zh-HK-HiuMaanNeuralGender: FemaleName: zh-HK-WanLungNeuralGender: MaleName: zh-TW-HsiaoChenNeuralGender: Female……
此时若要把中文转成对应的语言按照如下形式即可:
C:\Users\Administrator>edge-tts --voice zh-CN-YunxiNeural --text "每个炎黄子孙,都应为华夏伟大复兴而奋斗!" --write-media "c:\\china-dream.mp3"
另外,edge-tts还支持调整语速与音量,只需要转换命令中添加rate与volume指令即可。示例如下:
C:\Users\Administrator>edge-tts --rate=-50% --text "Hello, world!" --write-media hello_with_rate_halved.mp3C:\Users\Administrator>edge-tts --volume=-50% --text "Hello, world!" --write-media hello_with_volume_halved.mp3
1.5 参考
Edge-tts在Python官方项目上的连接:
edge-tts · PyPI:
相关参考示例可以在github上查看:
edge-tts/examples:
欢迎点赞、转发。希望对你有所帮助。
标签: #python 语音聊天