edge-tts微软文本转语音库,来听听这些语音是不是很熟练?
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上期图文教程,<span style="color: black;">咱们</span>分享了<a style="color: black;">Azure<span style="color: black;">设备</span>学习的文本转语音</a>的账号申请与API申请的<span style="color: black;">仔细</span><span style="color: black;">过程</span>,<span style="color: black;">亦</span>介绍了<a style="color: black;">基于python3实现Azure<span style="color: black;">设备</span>学习文本转语音功能</a>的代码实现过程,虽然<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>Azure账号免费<span style="color: black;">供给</span>一年的试用期,<span style="color: black;">然则</span>毕竟是要付费的,<span style="color: black;">咱们</span>的API<span style="color: black;">亦</span><span style="color: black;">没法</span><span style="color: black;">长时间</span><span style="color: black;">运用</span>,好在微软发布了edge-tts库,此库继承了Azure的文本转语音功能,且是免费<span style="color: black;">运用</span>的,本期<span style="color: black;">咱们</span>就介绍一下edge-tts的<span style="color: black;">运用</span>。<span style="color: black;">亦</span>许你听到如下生成的声音,你会<span style="color: black;">发掘</span>很是<span style="color: black;">熟练</span>。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/fba2c4f4fcec447bb05f0a181d4b1a78~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728065990&x-signature=ChP7vD8Axl2Plb%2FVgIlpf7DRFjk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Edge-TTS是一个Python库,它<span style="color: black;">运用</span>微软的Azure Cognitive Services来实现文本到语音转换(TTS)。该库<span style="color: black;">供给</span>了一个简单的API,可以将文本转换为语音,并且支持多种语言和声音。要<span style="color: black;">运用</span>Edge-TTS库,<span style="color: black;">首要</span>需要安装上Edge-TTS库,安装直接<span style="color: black;">运用</span>pip 进行安装<span style="color: black;">就可</span>。</p><span style="color: black;">pip</span> install edge-tts<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>本期的代码基于python3.9,在自己的cmd窗口中,输入以上安装命令,便会自动安装Edge-TTS</p><span style="color: black;">Collecting</span> <span style="color: black;">edge-tts Downloading edge_tts-6.1.3-py3-none-any.whl (26 kB)</span>
<span style="color: black;">Collecting</span> <span style="color: black;">aiohttp>=3.8.0 Downloading aiohttp-3.8.4-cp39-cp39-win_amd64.whl (323 kB)</span>
<span style="color: black;">|████████████████████████████████|</span> <span style="color: black;">323 kB 17 kB/s</span>
<span style="color: black;">Collecting</span> <span style="color: black;">aiosignal>=1.1.2 Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)</span>
<span style="color: black;">Collecting</span> <span style="color: black;">charset-normalizer<4.0,>=2.0 Downloading charset_normalizer-3.1.0-cp39-cp39-win_amd64.whl (97 kB)</span>
<span style="color: black;">|████████████████████████████████|</span> <span style="color: black;">97 kB 20 kB/s</span>
<span style="color: black;">Collecting</span> <span style="color: black;">yarl<2.0,>=1.0 Downloading yarl-1.8.2-cp39-cp39-win_amd64.whl (56 kB)</span>
<span style="color: black;">|████████████████████████████████|</span> <span style="color: black;">56 kB 20 kB/s</span>
<span style="color: black;">Collecting</span> <span style="color: black;">frozenlist>=1.1.1 Downloading frozenlist-1.3.3-cp39-cp39-win_amd64.whl (34 kB)</span>
<span style="color: black;">Collecting</span> <span style="color: black;">multidict<7.0,>=4.5 Downloading multidict-6.0.4-cp39-cp39-win_amd64.whl (28 kB)</span>
<span style="color: black;">Collecting</span> <span style="color: black;">attrs>=17.3.0 Downloading attrs-22.2.0-py3-none-any.whl (60 kB)</span>
<span style="color: black;">|████████████████████████████████|</span> <span style="color: black;">60 kB 20 kB/s</span>
<span style="color: black;">Collecting</span> <span style="color: black;">async-timeout<5.0,>=4.0.0a3 Using cached async_timeout-4.0.2-py3-none-any.whl (5.8 kB)</span>
<span style="color: black;">Collecting</span> <span style="color: black;">idna>=2.0 Using cached idna-3.4-py3-none-any.whl (61 kB)</span>
<span style="color: black;">Installing</span> <span style="color: black;">collected packages: frozenlist, aiosignal, charset-normalizer, multidict, idna, yarl, attrs, async-timeout, aiohttp, edge-tts</span>
<span style="color: black;">Successfully</span> <span style="color: black;">installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 attrs-22.2.0 charset-normalizer-3.1.0 edge-tts-6.1.3 frozenlist-1.3.3 idna-3.4 multidict-6.0.4 yarl-1.8.2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">安装完成后,<span style="color: black;">咱们</span><span style="color: black;">能够</span>输入edge-tts,若cmd窗口中输出提示信息,说明<span style="color: black;">已然</span>安装完成。</p>usage: edge-tts [-h] [-t TEXT] [-f FILE] [-v VOICE] [-l] [<span style="color: black;">--rate RATE] [--volume VOLUME] [-O OVERLAPPING]</span>
[<span style="color: black;">--write-media WRITE_MEDIA] [--write-subtitles WRITE_SUBTITLES] [--proxy PROXY]</span>
edge-tts: <span style="color: black;">error</span>: one of the arguments -t/<span style="color: black;">--text -f/--file -l/--list-voices is required</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>要直接在cmd命令行中使用Edge-TTS,<span style="color: black;">咱们</span>只需要<span style="color: black;">运用</span>如下命令来运行<span style="color: black;">就可</span></p>edge-tts --text <span style="color: black;">"Hello, world!"</span> --<span style="color: black;">write</span>-media hello.mp3<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上运行完成后,便会在本地生成一个hello.mp3的音频文件,<span style="color: black;">然则</span><span style="color: black;">这儿</span>是默认的语音,<span style="color: black;">咱们</span>需要改变语音,来实现符合自己<span style="color: black;">需求</span>的语音,且默认的语音只支持英文。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>直接在cmd窗口中输入如下命令行来查看edge-tts支持的语音</p>edge-tts --list-voices
<span style="color: black;">Name: af-ZA-AdriNeural Gender: Female</span>
........
........
........
<span style="color: black;">Name: zh-CN-XiaoxiaoNeural Gender: Female</span>
<span style="color: black;">Name: zh-CN-XiaoyiNeural Gender: Female</span>
<span style="color: black;">Name: zh-CN-YunjianNeural Gender: Male</span>
<span style="color: black;">Name: zh-CN-YunxiNeural Gender: Male</span>
<span style="color: black;">Name: zh-CN-YunxiaNeural Gender: Male</span>
<span style="color: black;">Name: zh-CN-YunyangNeural Gender: Male</span>
<span style="color: black;">Name: zh-CN-liaoning-XiaobeiNeural Gender: Female</span>
<span style="color: black;">Name: zh-CN-shaanxi-XiaoniNeural Gender: Female</span>
<span style="color: black;">Name: zh-HK-HiuGaaiNeural Gender: Female</span>
<span style="color: black;">Name: zh-HK-HiuMaanNeural Gender: Female</span>
<span style="color: black;">Name: zh-HK-WanLungNeural Gender: Male</span>
<span style="color: black;">Name: zh-TW-HsiaoChenNeural Gender: Female</span>
<span style="color: black;">Name: zh-TW-HsiaoYuNeural Gender: Female</span>
<span style="color: black;">Name: zh-TW-YunJheNeural Gender: Male</span>
<span style="color: black;">Name: zu-ZA-ThandoNeural Gender: Female</span>
<span style="color: black;">Name: zu-ZA-ThembaNeural Gender: Male</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>会打印出来所有支持的语音,字母zh开头的便是支持中文的语音,<span style="color: black;">咱们</span><span style="color: black;">运用</span>zh-CN-YunxiNeural,来听听zh-CN-YunxiNeural生成的语音,你不是听过。<span style="color: black;">这儿</span><span style="color: black;">咱们</span>只需要添加--voice命令,来指定输出的语音。</p>edge-tts --voice zh-CN-YunxiNeural --text<span style="color: black;">"hello <span style="color: black;">大众</span>好,<span style="color: black;">这儿</span>是人工智能<span style="color: black;">科研</span>所"</span> --<span style="color: black;">write</span>-media hello.mp3<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">命令行执行完成后,<span style="color: black;">咱们</span><span style="color: black;">能够</span>从cmd命令行中看到,不仅edge-tts<span style="color: black;">能够</span>生成语音,还特意生<span style="color: black;">成为了</span>字母文件,<span style="color: black;">咱们</span><span style="color: black;">能够</span>把这些字幕输入到视频剪辑文件中,就<span style="color: black;">能够</span>自动添加字幕了。</p><span style="color: black;">WEBVTT</span>
00<span style="color: black;">:00</span><span style="color: black;">:00.100</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:00.700</span>
<span style="color: black;">hello</span>
00<span style="color: black;">:00</span><span style="color: black;">:00.800</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:01.075</span>
<span style="color: black;">大众</span>
00<span style="color: black;">:00</span><span style="color: black;">:01.075</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:01.387</span>
好
00<span style="color: black;">:00</span><span style="color: black;">:01.712</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:02.000</span><span style="color: black;">这儿</span>
00<span style="color: black;">:00</span><span style="color: black;">:02.000</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:02.237</span>
是
00<span style="color: black;">:00</span><span style="color: black;">:02.263</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:02.850</span>
人工智能
00<span style="color: black;">:00</span><span style="color: black;">:02.862</span> <span style="color: black;">--</span>> 00<span style="color: black;">:00</span><span style="color: black;">:03.350</span>
<span style="color: black;">科研</span>所<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然edge-tts还支持<span style="color: black;">调节</span>语速与音量,<span style="color: black;">咱们</span>只需要添加rate与volume指令</p>edge-tts --voice zh-CN-YunxiNeural --rate=-<span style="color: black;">4</span>% --text <span style="color: black;">"hello <span style="color: black;">大众</span>好,<span style="color: black;">这儿</span>是人工智能<span style="color: black;">科研</span>所"</span> --<span style="color: black;">write</span>-media hello1.mp3
edge-tts --voice zh-CN-YunxiNeural --volume=-<span style="color: black;">4</span>% --text <span style="color: black;">"hello <span style="color: black;">大众</span>好,<span style="color: black;">这儿</span>是人工智能<span style="color: black;">科研</span>所"</span> --<span style="color: black;">write</span>-media hello1.mp3<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rate=-4% 意思是语速降低4%,volume=-4%意思是音量降低4%,当然若是加号,便是<span style="color: black;">增多</span>多少。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然,以上是在CMD命令行中执行的指令,<span style="color: black;">咱们</span><span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">运用</span>python进行批量文本转语音</p><span style="color: black;">import</span>edge_tts<span style="color: black;">import</span> asyncio
TEXT = <span style="color: black;">""</span>
<span style="color: black;">with</span> open (<span style="color: black;">1.txt</span>,<span style="color: black;">rb</span>) <span style="color: black;">as</span> f:
data = f.read()
TEXT = data.decode(<span style="color: black;">utf-8</span>)
print(TEXT)
voice = <span style="color: black;">zh-CN-YunxiNeural</span>output =<span style="color: black;">4.mp3</span>
rate = <span style="color: black;">-4%</span>
volume = <span style="color: black;">+0%</span>
<span style="color: black;">async</span> <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">my_function</span><span style="color: black;">()</span>:</span>
tts = edge_tts.Communicate(text = TEXT,voice = voice,rate = rate,volume=volume)
<span style="color: black;">await</span>tts.save(output)<span style="color: black;">if</span> __name__ == <span style="color: black;">__main__</span>:
asyncio.run(my_function())<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span><span style="color: black;">咱们</span>导入edge_tts与asyncio库,并<span style="color: black;">运用</span>with open函数打开一个txt文件。txt便是<span style="color: black;">咱们</span>要生成语音的文本文件。<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">通常</span>是输入的中文,<span style="color: black;">因此呢</span>,<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>rb读取文件,<span style="color: black;">而后</span><span style="color: black;">经过</span>decode函数转换一下,避免类似如下的问题</p><span style="color: black;">gbk</span> codec can<span style="color: black;">t</span> decode byte <span style="color: black;">0xae</span> <span style="color: black;">in</span> position <span style="color: black;">4</span>: illegal multibyte sequence<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span><span style="color: black;">咱们</span>定义好<span style="color: black;">咱们</span>输入的5个参数</p>text = TEXT 需要转换的文本voice = voice <span style="color: black;">选取</span>的语音rate = rate 语速volume=volume 音量output 输出文件,<span style="color: black;">通常</span>是MP3文件<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>async进行异步请求,若是直接运行,会提示如下错误,<span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span>需要<span style="color: black;">运用</span>异步处理的方式。</p>RuntimeWarning: coroutine <span style="color: black;">Communicate.save</span>was never awaited
tts.save(output)
RuntimeWarning: Enable tracemalloc to<span style="color: black;">get</span> the <span style="color: black;">object</span> allocation traceback<span style="color: black;">https</span>:<span style="color: black;">//github.com/rany2/edge-tts 项目文件<span style="color: black;">位置</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">NLP<span style="color: black;">行业</span><span style="color: black;">近期</span>越来越火,<span style="color: black;">尤其</span>是ChatGPT的流行<span style="color: black;">起始</span>,NLP<span style="color: black;">行业</span>一举打破了CV<span style="color: black;">行业</span>,让<span style="color: black;">更加多</span>的人看到了人工智能的魅力,<span style="color: black;">这儿</span>介绍一下transformer模型,其NLP<span style="color: black;">行业</span>必然绕不开的一个模型。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/719549d373b9468d9b73cd02754cae3c~noop.image?x-expires=1996036353&x-signature=6jJGP7wFrlMcYN3YsMeL6olQPp8%3D" style="width: 50%; margin-bottom: 20px;">
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">专栏</span></div>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;">动画详解transformer与变体模型</div>
<div style="color: black; text-align: left; margin-bottom: 10px;">作者:人工智能<span style="color: black;">科研</span>所</div>
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;">169.9币</div>
<div style="color: black; text-align: left; margin-bottom: 10px;">57人已购</div>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;">查看</div>
</div>
</div>
</div>
</div>
我完全同意你的看法,期待我们能深入探讨这个问题。 感谢楼主分享,祝愿外链论坛越办越好! 在遇到你之前,我对人世间是否有真正的圣人是怀疑的。 我完全同意你的观点,说得太对了。 期待更新、坐等、迫不及待等。
页:
[1]