搜狗语音API
/doc/?url=/docs/content/tts/
首先打开控制台创建应用,获得APP_ID,APP_KEY,语音合成只有100万字符的免费调用。
调用示例:
import requestsimport jsonimport os""" 你的 APPID APPKEY """appid = 'XXXXX'appkey = 'XXXXXX'# 获取tokendef getToken():data = {'appid': appid,'appkey': appkey,'exp': '3600s'}headers = {'Content-Type': 'application/json'}r = requests.post('https://api./apis/auth/v1/create_token', data=json.dumps(data), headers=headers)jsonObj = json.loads(r.text)return jsonObj['token']# 文字转音频def Text2Audio(text, audiofile):data = {'input': {'text': text,},'config': {'audio_config': {'audio_encoding':'MP3','pitch':1,'volume':1,'speaking_rate':1},'voice_config':{'language_code':"zh-cmn-Hans-CN",'speaker':'female'}}}token = getToken()headers = {'Content-Type': 'application/json','Appid': appid,'Authorization': 'Bearer ' + token}r = requests.post('https://api./apis/tts/v1/synthesize', data=json.dumps(data), headers=headers)with open(audiofile, 'ab+') as f2:f2.write(r.content)