自己搭建[文本转语音]服务器

一、简介

准备用基于esp8266的nodemcu开发板做一个天气时钟。

一步一步记录代码编写过程。

竹壳天气时钟

Bamboo shell weather clock

使用基于esp8266的NodeMCU制作。

计划用竹子做最后成品的外壳,所以才有了这个名称。

上一篇文章已经完成了时钟和天气的显示,下一步要做语音控制报时和报天气,今天记录一下我使用 三丰云 免费云服务器 搭建TTS的过程。三丰云 https://www.sanfengyun.com 我已经用了一段时间,感觉还是很不错的,运行也很稳定。

二、TTS服务器搭建

在云服务器上安装Ubuntu Linux,安装docker-ce docker-ce-cli

然后添加docker代理,复制后粘贴到shell终端即可

bash 复制代码
sudo tee /etc/docker/daemon.json <<-"EOF"
{
    "registry-mirrors": ["https://dockerpull.com"]
}
EOF

然后建立一个tts文件夹并进入,以下代码复制后粘贴到shell终端即可

bash 复制代码
tee ~/tts/gunicorn.conf <<-'EOF'
# gunicorn.conf
# 并行工作进程数
workers = 4
# 指定每个工作者的线程数
threads = 2
# 监听内网端口5000
bind='0.0.0.0:2020'
# 设置守护进程,将进程交给supervisor管理
daemon = 'false'
# 工作模式协程
worker_class = 'gevent'
# 设置最大并发量
worker_connections = 2000
# 设置进程文件目录
pidfile = 'gunicorn.pid'
# 设置访问日志和错误信息日志路径
accesslog = 'gunicorn_acess.log'
errorlog = 'gunicorn_error.log'
# 设置日志记录水平
loglevel = 'debug'
EOF
tee ~/tts/Dockerfile <<-'EOF'
FROM python:3.8.4
COPY requirements.txt ./
RUN pip install  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY . /flask_project/
WORKDIR /flask_project/
EOF
tee ~/tts/dockermake.sh <<-'EOF'
docker build -t python_tts .
EOF
tee ~/tts/requirements.txt <<-'EOF'
Flask
flask_cors
cos-python-sdk-v5
gunicorn
gevent
edge-tts
EOF
tee ~/tts/edge-tts.py <<-'EOF'
'''
原项目作者:https://github.com/lyz1810/edge-tts
'''
import logging
import os
import re
import sys
import uuid
from flask import Flask, request, send_file
from qcloud_cos import CosConfig, CosS3Client
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

voiceMap = {
    "xiaoxiao": "zh-CN-XiaoxiaoNeural",
    "xiaoyi": "zh-CN-XiaoyiNeural",
    "yunjian": "zh-CN-YunjianNeural",
    "yunxi": "zh-CN-YunxiNeural",
    "yunxia": "zh-CN-YunxiaNeural",
    "yunyang": "zh-CN-YunyangNeural",
    "xiaobei": "zh-CN-liaoning-XiaobeiNeural",
    "xiaoni": "zh-CN-shaanxi-XiaoniNeural",
    "hiugaai": "zh-HK-HiuGaaiNeural",
    "hiumaan": "zh-HK-HiuMaanNeural",
    "wanlung": "zh-HK-WanLungNeural",
    "hsiaochen": "zh-TW-HsiaoChenNeural",
    "hsioayu": "zh-TW-HsiaoYuNeural",
    "yunjhe": "zh-TW-YunJheNeural",
}


def getVoiceById(voiceId):
    return voiceMap.get(voiceId)


# 删除html标签
def remove_html(string):
    regex = re.compile(r'<[^>]+>')
    return regex.sub('', string)


def createAudio(text, file_name, voiceId):
    new_text = remove_html(text)
    print(f"Text without html tags: {new_text}")
    voice = getVoiceById(voiceId)
    if not voice:
        return "error params"

    pwdPath = os.getcwd()
    filePath = pwdPath + "/" + file_name
    dirPath = os.path.dirname(filePath)
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    if not os.path.exists(filePath):
        # 用open创建文件 兼容mac
        open(filePath, 'a').close()

    script = 'edge-tts --voice ' + voice + ' --text "' + new_text + '" --write-media ' + filePath
    os.system(script)
    return filePath  # 返回生成的音频文件路径


def getParameter(paramName):
    if request.args.__contains__(paramName):
        return request.args[paramName]
    return ""

@app.route('/dealAudio',methods=['POST','GET'])
def dealAudio():
    text = getParameter('text')
    file_name = str(uuid.uuid4()) + ".mp3"
    voice = getParameter('voice')
    audio_file_path = createAudio(text, file_name, voice)
    return send_file(audio_file_path, as_attachment=True)


@app.route('/')
def index():
    return 'tts!'

if __name__ == "__main__":
EOF
tee ~/tts/run_python_tts <<-'EOF'
docker run -d -p 2020:2020 --name python_tts python_tts gunicorn edge-tts:app -c gunicorn.conf
EOF

容器制作与启动方法

bash 复制代码
#添加运行权限

chmod +x ./dockermake.sh

#运行脚本制作镜像

./dockermake.sh

#运行容器

bash run_python_tts

API:服务器ip:2020/dealAudio?text=欢迎使用tts&voice=xiaoxiao

text 是你需要转换的文本

voice 是配音员 上面自己选

相关推荐
zzzzzz31020 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode20 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220702 天前
如何搭建本地yum源(上)
运维
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz5 天前
Maven依赖冲突
java·服务器·maven
Inhand陈工5 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智5 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_5 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化