调用ali语言合成接口,并将语句文件下载保存为文件
阿里云语言合成控制台
import http.client
from urllib.parse import quote
class AudioCreator:
def __init__(self, appKey, token):
self.appKey = appKey
self.token = token
def createAudio(self, text, fileName):
conn = http.client.HTTPSConnection('nls-gateway.cn-shanghai.aliyuncs.com')
text = quote(text)
url = '/stream/v1/tts?' \
'voice=Ruoxi' \
'&appkey=' + self.appKey \
+ '&token=' + self.token \
+ '&text=' + text \
+ '&format=wav&sample_rate=16000'
conn.request('GET', url)
r = conn.getresponse()
print(r.status, r.reason)
res = b''
while chunk := r.read(200):
res += chunk
with open(fileName, 'wb') as f:
f.write(res)
if __name__ == '__main__':
# 替换为自己的key和token
myKey = 'xxxx'
myToken = 'xxxxxxxxxxxxxx'
audioCreator = AudioCreator(myKey, myToken)
audioCreator.createAudio('语音合成服务', '文件.wav')