目录
前言
一、提交请求,获得查询地址URL
二、等待20分钟左右,下载zip文件
三、查询提交过的请求,的id记录
四、根据ID删除全部记录(记录如果满了要手动删除,才能提交新的)
自行注册微软云服务账号,搭建文本转语音标准应用
import json
import ntpath
import requests
import time
import wget
#你的区域
region = ''
#你的密钥
key = ''
#转换长文本txt路径,四百字以上
def submit_synthesis(input_file_path):
global region
global key
locale = 'zh-CN'
url = 'https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis'.format(region)
header = {
'Ocp-Apim-Subscription-Key': key
}
voice_identities = [
{
#云溪,自行更改
'voicename': 'zh-CN-YunxiNeural'
}
]
payload = {
'displayname': 'long audio synthesis sample',
'description': 'sample description',
'locale': locale,
'voices': json.dumps(voice_identities),
'outputformat': 'riff-16khz-16bit-mono-pcm',
'concatenateresult': True,
}
filename = ntpath.basename(input_file_path)
files = {
'script': (filename, open(input_file_path, 'rb'), 'text/plain')
}
response = requests.post(url, payload, headers=header, files=files)
print('response.status_code: %d' % response.status_code)
#打印转换记录的查询地址
print(response.headers['Location'])
#一般得等个10分钟往上,15分钟左右,才能转好
#输出文件的地址如D:\\test.zip,url为提交时响应的url
def get_files(output_file_path,LocationUrl):
global key
#url+参数files
url = LocationUrl+'/files'
print(url)
header = {
'Ocp-Apim-Subscription-Key': key
}
response = requests.get(url, headers=header)
print('response.status_code: %d' % response.status_code)
#响应里的第一个链接是你传上去的txt文件,第二个链接是转好的压缩包文件
#如果有第一个链接,没有第二个链接,就是没转好
if(len(eval(response.text)["values"])==2):
#获取响应里的zip下载链接
ZipResponseUrl=eval(response.text)['values'][1]['links']['contentUrl']
print(ZipResponseUrl)
#wget模块自行安装,下载到输出目录
wget.download(ZipResponseUrl,output_file_path)
print("完成")
else:
#还没转好,再等等
print("还没生成")
region = ''
key = ''
responseData=''
def get_synthesis():
global region
global key
global responseData
url = 'https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis/'.format(region)
header = {
'Ocp-Apim-Subscription-Key': key
}
response = requests.get(url, headers=header)
#打印请求状态
print('response.status_code: %d' % response.status_code)
#替换true为“true”
responseContent=eval(response.text.replace('true','\"true\"'))
#转为字典
responseData=responseContent["values"]
idCount= len(responseData)
i=0
while i < idCount: #打印所有id
print(responseData[i]["id"])
i+=1
def delete_synthesis():
global region
global key
#数据,来自上一个查询的方法
global responseData
idCount= len(responseData)
i=0
while i < idCount: # 循环控制条件
print(responseData[i]["id"])
id=responseData[i]["id"]
url = 'https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis/{}/'.format(region, id)
header = {
'Ocp-Apim-Subscription-Key': key
}
response = requests.delete(url, headers=header)
print('response.status_code: %d' % response.status_code)
i+=1