赞
踩
https://ai.youdao.com/DOCSIRMA/html/语音合成TTS/API文档/语音合成服务/语音合成服务-API文档.html
应用ID
和应用密钥:
https://ai.youdao.com/doc.s#guide
文字的UTF-8编码长度不能超过1000.
有道智云TTS API HTTPS地址:
https://openapi.youdao.com/ttsapi
调用API需要向接口发送以下字段来访问服务。
字段名 | 类型 | 含义 | 必填 | 备注 |
---|---|---|---|---|
q | text | 待合成音频文件的文本字符串 | True | 比如:您好 |
langType | text | 合成文本的语言类型 | True | 支持语言 |
appKey | text | 应用 ID | True | 可在 应用管理 查看 |
salt | text | UUID | True | UUID,唯一通用识别码,可在线生成 |
sign | text | 签名 | True | MD5(应用ID+q+salt+应用密钥) |
voice | text | 翻译结果发音选择,0为女声,1为男声,默认为女声 | false | 0 |
format | text | 目标音频格式,支持mp3 | false | mp3 |
speed | text | 合成音频的语速 | false | 比如:"1"为正常速度;最大为“2”,最小为“0.1” |
volume | text | 合成音频的音量 | false | 正常为"1.00",最大为"5.00",最小为"0.50" |
- import uuid
- import requests
- import hashlib
- import os
-
- def encrypt(signStr):
- hash_algorithm = hashlib.md5()
- hash_algorithm.update(signStr.encode('utf-8'))
- return hash_algorithm.hexdigest()
-
- def txt2audio(text):
- try:
- data = {}
- data['langType'] = 'zh-TWN'
- salt = str(uuid.uuid1())
- signStr = APP_KEY + text + salt + APP_SECRET
- sign = encrypt(signStr)
- data['appKey'] = APP_KEY
- data['q'] = text
- data['salt'] = salt
- data['sign'] = sign
-
- headers = {'Content-Type': 'application/x-www-form-urlencoded'}
- response = requests.post(YOUDAO_URL, data=data, headers=headers)
- contentType = response.headers['Content-Type']
- if contentType == "audio/mp3":
- with open(audiofile, 'ab+') as f2:
- f2.write(response.content)
- else:
- print(response.content)
- break
- except Exception as e:
- print("Txt2Audio Error:", e)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。