当前位置 博文首页 > ChenYX的博客:python文字转语音
调用百度的site-package,不知道为什么不行,所以改用官方demo,直接手动post下载,再用playsound库直接读出来。
贴一下源码,感兴趣的也可以试试。前述步骤可以参考(再pip一个playsound包就行了):https://blog.csdn.net/weixin_45974643/article/details/105567722
# -*- coding: utf-8 -*-
# @Time : 2020/7/26 12:42
# @Author : ChenYuxi
# @Email : imchenyuxi@163.com
# @File : TTSTest.py
# @Software: PyCharm
from playsound import playsound
import json
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.parse import quote_plus
API_KEY = 'nu9r2plGFi3s1ugayDPSM6Mk'
SECRET_KEY = 'G62YGnq84eKTqu0mBgvdpmC6gNBzHdai'
TTS_URL = 'http://tsn.baidu.com/text2audio'
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
file_name = 'recognize.mp3'
TEXT = "正在识别,请稍候…………"
volume = 15 # 音量:0-15(默认5)
speed = 6 # 语速:0-15(默认5)
tone = 5 # 语调:0-15(默认5)
people = 4 # 人声:0-4,0-女声,1-男声,3-度逍遥(男),4-度丫丫(女)
"""
获取token
"""
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
post_data = urlencode(params)
post_data = post_data.encode('utf-8')
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
res = f.read()
except URLError as err:
print('token http response http code : ' + str(err.code))
res = err.read()
res = res.decode()
result = json.loads(res)
if 'access_token' in result.keys() and 'scope' in result.keys():
if not 'audio_tts_post' in result['scope'].split(' '):
print('please ensure has check the tts ability')
exit()
return result['access_token']
else:
print('please overwrite the correct API_KEY and SECRET_KEY')
exit()
""" TOKEN end """
if __name__ == '__main__':
token = fetch_token()
tex = quote_plus(TEXT) # 此处TEXT需要两次urlencode
params = {'tok': token, 'tex': tex, 'cuid': "quickstart",
'lan': 'zh', 'ctp': 1, # lan ctp 固定参数
'vol': volume, 'per': people, 'spd': speed, 'pit': tone}
data = urlencode(params)
req = Request(TTS_URL, data.encode('utf-8'))
has_error = False
try:
f = urlopen(req)
result_str = f.read()
headers = dict((name.lower(), value) for name, value in f.headers.items())
has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)
except URLError as err:
print('http response http code : ' + str(err.code))
result_str = err.read()
has_error = True
if has_error:
save_file = "error.txt"
result_str = str(result_str, 'utf-8')
print("tts api error:" + result_str)
else:
save_file = file_name
with open(save_file, 'wb') as of:
of.write(result_str)
print("file saved as : " + save_file)
playsound(save_file)
需要修改语速、语调、声音大小还有音库人声的可以直接修改参数,贴一个官方文档里的表格:
参数 | 可需 | 描述 |
---|---|---|
tex | 必填 | 合成的文本,使用UTF-8编码。小于2048个中文字或者英文数字。(文本在百度服务器内转换为GBK后,长度必须小于4096字节) |
tok | 必填 | 开放平台获取到的开发者access_token(见上面的“鉴权认证机制”段落) |
cuid | 必填 | 用户唯一标识,用来计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内 |
ctp | 必填 | 客户端类型选择,web端填写固定值1 |
lan | 必填 | 固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh |
spd | 选填 | 语速,取值0-15,默认为5中语速 |
pit | 选填 | 音调,取值0-15,默认为5中语调 |
vol | 选填 | 音量,取值0-15,默认为5中音量 |
per(基础音库) | 选填 | 度小宇=1,度小美=0,度逍遥(基础)=3,度丫丫=4 |
per(精品音库) | 选填 | 度逍遥(精品)=5003,度小鹿=5118,度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5 |
aue | 选填 | 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k); 注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。 |
详细开发文档可以访问:https://ai.baidu.com/ai-doc/SPEECH/Gk38y8lzk
cs