导语:不想自己念东西 网上免费的ai文字转语音又贼麻烦。找了开源的容器化部署方式,以后备用。
通过Dockerfile构建镜像
# 下载代码
git clone https://github.com/LiuChangFreeman/ms-tts-web
# git clone https://github.com/alekssamos/msspeech.git # 这个是后端
cd ms-tts-web
# 不设置GITHUB_HTTP_PORXY参数则不启用加速,适用于海外机器构建
docker build -t ms_tts_web . --rm --network=host
# 或者使用已构建好的镜像
docker pull registry.cn-shanghai.aliyuncs.com/custom_runtime/ms-tts-web
# 创建容器实例 这个只能翻译300行文字
docker run --name ms_tts -d --net=host -v /Users/xujiamin/Downloads/ai/ms-tts-web/tts_storage/:/home/storage -e HOST=0.0.0.0 ms_tts_web
借用上面容器的python环境 转换超300行的文字
# 启动一个python3.7.4的容器
docker run -it --name=test python:3.7.4 bash
# 安装msspeech
pip install --upgrade msspeech
# 更新语言列表 # /usr/local/lib/python3.7/site-packages/msspeech/voices_list.json
msspeech-update-voices
# 安装vim
apt update ;apt install vim lrzsz -y
#
voices_list.json 这个是对应的语言库 可以在其中找到需要的语言的别名 并修改。默认是ru毛语
1.txt是你需要转为语音的文字内容
audio3.mp3 是输出的语音mp3文件的名称
await mss.set_rate是语速 -100 到 100 之间调解
对应修改文件如下
import asyncio
from msspeech import MSSpeech
async def main():
mss = MSSpeech()
print("Geting voices...")
voices = await mss.get_voices_list()
print("searching Russian voice...")
# for voice in voices:
# if voice["Locale"] == "zh-CN":
# #if voice["Locale"] == "ru-RU":
# print("Russian voice found:", voice["FriendlyName"])
# await mss.set_voice(voice["Name"])
await mss.set_voice("zh-CN-XiaoyiNeural")
#await mss.set_voice("zh-CN-XiaoxiaoNeural")
print("*" * 10)
filename = "audio3.mp3"
with open("1.txt", encoding="UTF8") as f: text:str = f.read()
#text = "Или написать текст здесь"
print("waiting...")
await mss.set_rate(-20)
await mss.set_pitch(0)
await mss.set_volume(1)
await mss.synthesize(text.strip(), filename)
print("*"*10)
print("SUCCESS! OK!")
print("*"*10)
if __name__ == "__main__":
asyncio.run(main())
# 执行转换
python 1.py
# 导出文件
sz audio3.mp3
https://github.com/alekssamos/msspeech