需求描述
- 使用python3调用百度tts,生成mp3文件
- 调用ffmpeg将mp3转为8k8bit的wav文件,用于FreeSWITCH的播放
实现步骤
申请百度tts账号
参考:链接
python实现代码
文件:baidutts.py
# coding=utf-8
import sys
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
IS_PY3 = sys.version_info.major == 3
# 替换你的 API_KEY
API_KEY = 'zqyXXkMmH0grrVh7N5tPPxxx'
# 替换你的 SECRET_KEY
SECRET_KEY = 'KV7iADpCqm19q8xj9XkvaKZuHEorXxxx'
TTS_URL = 'http://tsn.baidu.com/text2audio'
""" TOKEN start """
TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'
"""
获取token
"""
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
post_data = urlencode(params)
if (IS_PY3):
post_data = post_data.encode('utf-8')
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print('token http response http code : ' + str(err.code))
result_str = err.read()
if (IS_PY3):
result_str = result_str.decode()
result = json.loads(result_str)
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 """
def get_tts(myText, fileName):
token = fetch_token()
tex = quote_plus(myText) # 此处TEXT需要两次urlencode
params = {'tok': token, 'tex': tex, 'cuid': "quickstart",
'lan': 'zh', 'ctp': 1} # lan ctp 固定参数
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
save_file = "error.txt" if has_error else fileName
with open(save_file, 'wb') as of:
of.write(result_str)
if has_error:
if IS_PY3:
result_str = str(result_str, 'utf-8')
print("tts api error:" + result_str)
print("file saved as : " + save_file)
文件:tts.py
from baidutts import get_tts
import os
text = "你好,你来自什么地方,那个地方漂亮吗"
filemp3 = 'media/text.mp3'
# 输出的8k8bit的文件
finalwav = 'media/my.wav'
# 使用百度tts生成对应的mp3文件
get_tts(text, filemp3)
# 将mp3转为8k8bit的wav文件
cmd = f'ffmpeg -i {filemp3} -ar 8000 -ac 1 -acodec pcm_u8 {finalwav}'
os.system(cmd)
# 删除生成的中间文件
os.remove(filemp3)