百度语音合成在python中的使用

今天来说一下在python中如何使用百度的语音合成功能;即输入一段文字,请求百度相关服务器后,会返回来一段二进制语音流,将这段二进制数据经过base64编码返回给前端,前端解码后,可以播放出这段语音;也可以将这段二进制语音流保存到一个wav文件中。

使用百度的语音合成,首先需要在百度的ai开放平台上注册。

具体代码如下:

import asyncio
from aiohttp import ClientSession

def get_baidu_voice(text, baidu_voice_token):
    content_audio = {
        'tex': text,
        'tok': baidu_voice_token,
        'cuid': 'default',
        'ctp': '1',
        'lan': 'zh',
        'per': '4',

    }
    
    speech_url = 'https://tsn.baidu.com/text2audio?'
    headers = {
        # 'Content-Type': 'audio/mp3'
        'Content-Type': 'application/json'
    }
    async with ClientSession() as session:
        async with session.post(url=speech_url, data=content_audio,
                                headers=headers) as res:
            ret = await res.content.read()
            try:
                # 将bytes类型转换为str类型
                ret_str = str(ret, encoding="utf-8")
            except Exception as e:
                # 正常返回
                self.speech = base64.b64encode(ret)
            else:
                # 异常返回
                ret_dict = json.loads(ret_str)
                if ret_dict["err_no"] == 502:
                    raise RuntimeError("access token expired, please check")
                elif ret_dict["err_no"] == 501:
                    raise RuntimeError("the input arguments is incorrect, please check")
                elif ret_dict["err_no"] == 503:
                    raise RuntimeError("合成后端出错")
                elif ret_dict["err_no"] == 500:
                    raise RuntimeError("unsupport input")

需要传入两个参数,分别是:需要转换成语音的文本和百度的语音token。

百度语音token的获取方式如下:

def get_baidu_voice_token():
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+ client_id + '&client_secret=' + client_secret
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content_bytes = response.read()
    content_dict = json.loads(str(content_bytes, encoding="utf-8"))
    if content_dict:
        # 28天过期
        return content_dict["access_token"]

注意,token是有有效期的,需要定时获取新的token。

以上就是百度语音合成的调用,如有错误,欢迎交流指正!

相关推荐