1 Star 0 Fork 3

Jack/文字转语音

forked from 小和/文字转语音 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
xiaohe.py 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
小和 提交于 2021-10-08 15:54 +08:00 . 加入多线程
from flask import Flask
from flask import request
from flask import jsonify
app = Flask(__name__)
#version = 1.0
#windows运行命令
#set FLASK_APP=xiaohe
#flask run --host=0.0.0.0
#QQ496631085
import pyttsx3
import hashlib #md5
import pythoncom
@app.route("/")
def xiaohe():
return "<p>你好!此接口是文字生成语音,用于windows环境,详细联系QQ496631085</p>"
@app.route('/tts', methods=['POST', 'GET'])
def tts():
error = None
# return request.values.get('str')
if request.method == 'GET':
if len(request.values.get('str','')) > 0:
pythoncom.CoInitialize()
data = ttsget(request.values.get('str'))
return data #直接返回路径
else:
return "nostr"
if request.method == 'POST':
if len(request.values.get('str','')) > 0:
pythoncom.CoInitialize()
data = ttspost(request.values.get('str'))
return data #直接返回路径
else:
return "nostr"
return "错误"
return render_template('login.html', error=error)
def ttsget(data):
md5 = hashlib.md5(data.encode(encoding='UTF-8')).hexdigest()
engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume) #声音大小
# engine.say('The quick brown fox jumped over the lazy dog.')
file_name = md5+'.mp3'
path = '/' + 'static/' + file_name
engine.save_to_file(data ,'.'+path)
engine.runAndWait()
return path
def ttspost(data):
md5 = hashlib.md5(data.encode(encoding='UTF-8')).hexdigest()
engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume) #声音大小
# engine.say('The quick brown fox jumped over the lazy dog.')
file_name = md5+'.mp3'
path = '/' + 'static/' + file_name
engine.save_to_file(data ,'.'+path)
engine.runAndWait()
return {"path":path,"file_name":file_name}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hopebj2008/python-tts.git
git@gitee.com:hopebj2008/python-tts.git
hopebj2008
python-tts
文字转语音
master

搜索帮助