GitHub 上的 edge-tts 是一个开源的 Python 模块,它允许用户在自己的 Python 代码中使用微软 Edge 浏览器的在线文本到语音服务。
edge-tts 通过提供的 edge-tts 或 edge-playback 命令行工具来使用这项服务。这个项目使得用户无需安装 Microsoft Edge、Windows 或者拥有 API 密钥即可利用微软的文本到语音技术。
安装
$ pip install edge-tts
基本使用
生成语音和字幕文件
用户可以通过命令行使用 edge-tts 命令来将文本转换为语音并保存为媒体文件,同时还可以生成字幕文件。
$ edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
指定文件作为文本输入
$ edge-tts --file ./hello.txt --write-media hello.mp3 --write-subtitles hello.vtt
播放与字幕
edge-playback 命令可以立即播放生成的语音,并显示字幕,前提需要安装 mpv 命令行播放器。
$ edge-playback --text "Hello, world!"
更改语音
用户可以通过 --list-voices 选项查询可用的语音,并选择不同的语音进行文本到语音的转换。
$ edge-tts --list-voices
Name: Microsoft Server Speech Text to Speech Voice (af-ZA, AdriNeural)
ShortName: af-ZA-AdriNeural
Gender: Female
Locale: af-ZA
Name: Microsoft Server Speech Text to Speech Voice (am-ET, MekdesNeural)
ShortName: am-ET-MekdesNeural
Gender: Female
Locale: am-ET
Name: Microsoft Server Speech Text to Speech Voice (ar-EG, SalmaNeural)
ShortName: ar-EG-SalmaNeural
Gender: Female
Locale: ar-EG
Name: Microsoft Server Speech Text to Speech Voice (ar-SA, ZariyahNeural)
ShortName: ar-SA-ZariyahNeural
Gender: Female
Locale: ar-SA
...
选择阿拉伯语
$ edge-tts --voice ar-EG-SalmaNeural --text "مرحبا كيف حالك؟" --write-media hello_in_arabic.mp3 --write-subtitles hello_in_arabic.vtt
调整语速、音量和音调
用户可以对生成的语音进行一些微调,如调整语速、音量和音调。
$ edge-tts --rate=-50% --text "Hello, world!" --write-media hello_with_rate_halved.mp3 --write-subtitles hello_with_rate_halved.vtt
$ edge-tts --volume=-50% --text "Hello, world!" --write-media hello_with_volume_halved.mp3 --write-subtitles hello_with_volume_halved.vtt
$ edge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello_with_pitch_halved.mp3 --write-subtitles hello_with_pitch_halved.vtt
Python 模块
edge-tts 也可以作为 Python 模块直接在 Python 代码中使用,项目中提供了一些示例应用程序。
这个项目为想要在自己的应用程序中集成文本到语音功能但又不想依赖于特定操作系统或浏览器的开发者提供了便利。如果你对 edge-tts 项目感兴趣,可以访问它的 GitHub 页面以获取更多信息和使用示例。