展示页面 | |
文档 |
在网上没找到类似的文件,搜狗的文档写的又很狗(反正非常不初学者友好),因此在此记录。
第一步:注册
第二步,进入控制台(点击控制台或者注册后用户名部分均可)
ps用户名部分就是被我涂的花花绿绿的部分
第三步,创建新应用
填写完成后,会弹出一个弹窗,提醒注意保存好app id 和app key
(没有保存好应该问题也不大,因为在应用那里会有记录,如下图所示:
第四步,写代码,获取token(注意token的时间限制是一小时,随时用随时申请)
import requests
import json
tmp={
"appid": "刚刚要求保存好的自己的app id",
"appkey": "刚刚要求保存好的自己的app key",
"exp": "3600s"
}
req = requests.post(url='https://api.zhiyin.sogou.com/apis/auth/v1/create_token',
json=tmp,headers={'Content-Type': 'application/json'})
print(req.text)
参考文档:https://ai.sogou.com/doc/?url=/docs/content/auth/create-token/using-rest/
虽然没有Python的示例,但是非常感谢人美心善的小姐姐帮我看了一下!
不知道为什么json=tmp如果写成data=tmp就会莫名其妙报错:
{"code":3,"message":"illegal body format: invalid character 'a' looking for beginning of value"}
原因:
参考https://www.cnblogs.com/yanlin-10/p/9820694.html
用data参数提交数据时,request.body
的内容则为a=1&b=2
的这种形式,用json参数提交数据时,request.body
的内容则为'{"a": 1, "b": 2}'
的这种形式
得到token之后放到json整理的工具里
变成好看的模样。
给出的时间格式比较奇怪,参考https://www.v2ex.com/t/332522可知:
T 就是个分隔符,前面是日期,后面是时间。
Z 表示 UTC
UTC:http://zh.thetimenow.com/utc/coordinated_universal_time
第五步,开始测试
按照文档(https://ai.sogou.com/doc/?url=/docs/content/tts/synthesize/using-rest/)中给出的demo:
import requests
import json
import time
##
##
#此处填入
##
##
appid = "提到的需要妥善保管的app id"
token = "刚刚请求得到的token"
text = '你好,请问你叫什么名字?'
'''
audio_encoding(****必传✓)
待合成的音频的编码格式。目前支持LINEAR16、MP3以及AMR三种格式
pitch(可选)
音高,范围是[0.8,1.2],如果不指定值(0,0),默认会设为1,任何小于0.8或大于1.2的值都会返回错误
volume(可选)
音量,范围是[0.7,1.3],如果不指定值(0,0),默认会设为1,任何小于1.7或大于1.3的值都会返回错误
speaking_rate(可选)
语音的速度,范围是[0.7,1.3],如果不指定值(0,0),默认会设为1,任何小于1.7或大于1.3的值都会返回错误
language_code(******必传✓)
要合成的声音的语言代码
speaker(可选)
发音人的名字,默认为female。支持male和female
text(******必选✓)
需要合成的原始文本,须为utf-8编码,且单次传入的长度不能超过1024
'''
data = {
'input': {
'text': text,
},
'config': {
'audio_config': {
'audio_encoding':'MP3',
'pitch':0.8,
'volume':1.3,
'speaking_rate':1.3
},
'voice_config':{
'language_code':"zh-cmn-Hans-CN",
'speaker':'female'
}
}
}
'''
请求头
格式相关:
Content-Type:string (******必选✓)
标准HTTP头,这里必须设置成application/json
认证相关:
Appid:string
标准知音自定义头,要求填入在用户控制台创建的对应应用的AppID
Authorization:string
标准HTTP头,设置鉴权信息。要求格式必须是标准Bearer<token>的形式。其中token是访问知音creat-token接口创建的访问令牌
'''
headers = {
'Content-Type': 'application/json',
'Appid': appid,
'Authorization': 'Bearer ' + token
}
'''
请求域名和路径
POST https://api.zhiyin.sogou.com/apis/tts/v1/synthesize
'''
time1=time.time()#这里是计算响应时间用的
r = requests.post('https://api.zhiyin.sogou.com/apis/tts/v1/synthesize', data=json.dumps(data), headers=headers)
# save to file
file = open("tts.mp3", "bw")
file.write(r.content)
file.close()
time2=time.time()#这里是计算响应时间用的
print("save finish")
print(time1)
print(time2-time1)#这里是计算响应时间用的
输出结果:
第六步,找到刚刚写入的文件,打开,收获快乐(并没有
确认了,是编译器的锅
我用的是Visual Studio
换成PyCharm就可以解决问题
附录:
最后执行的代码:
import requests
import json
import time
appid = '创建应用时得到的appid'
token = '获取的token'
text = '语音合成测试文本。'
data = {
'input': {
'text': text,
},
'config': {
'audio_config': {
'audio_encoding': 'MP3',
'pitch': 0.8,
'volume': 1.3,
'speaking_rate': 1.3
},
'voice_config': {
'language_code': "zh-cmn-Hans-CN",
'speaker': 'female'
}
}
}
headers = {
'Content-Type': 'application/json',
'Appid': appid,
'Authorization': 'Bearer ' + token
}
time1 = time.time()
r = requests.post('https://api.zhiyin.sogou.com/apis/tts/v1/synthesize', data=json.dumps(data), headers=headers)
# save to file
file = open("tts1.mp3", "bw")
file.write(r.content)
file.close()
time2 = time.time()
print("save finish")
print(time1)
print(time2-time1)
输出:
save finish
1615434584.8794093
0.24152207374572754
部分音频参数:
(音频参数部分代码见百度语音合成测试)
今日碎碎念
偷懒一时爽,报错火葬场