python微软长文本转语音API使用

目录

前言

一、提交请求,获得查询地址URL

二、等待20分钟左右,下载zip文件

三、查询提交过的请求,的id记录

四、根据ID删除全部记录(记录如果满了要手动删除,才能提交新的)


前言

自行注册微软云服务账号,搭建文本转语音标准应用

一、提交请求,获得查询地址URL

import json
import ntpath
import requests
import time
import wget

#你的区域
region = '<region>'
#你的密钥
key = '<your_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分钟左右,才能转好

二、等待20分钟左右,下载zip文件

#输出文件的地址如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("还没生成")

三、查询提交过的请求,的id记录

region = '<region>'
key = '<your_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

四、根据ID删除全部记录(记录如果满了要手动删除,才能提交新的)

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术小零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值