2020-02-17 python+百度AI进行文字转语音,语音

2020-02-17  本文已影响0人  菜菜笛

百度AI在线语音合成入口:https://ai.baidu.com/tech/speech/tts

点击“立即使用”,即可创建应用。访问创建完成的应用,可以获得对应应用的APPID、AK、SK。

有些遗憾的是,好像依然不支持英文。。。。。

安装百度AI模块:

pip install baidu-aip

代码实现:

# -*- coding: GBK -*-
from aip import AipSpeech
 
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

help(client.synthesis)
# Help on method synthesis in module aip.speech:
# synthesis(text, lang='zh', ctp=1, options=None) method of aip.speech.AipSpeech instance
#     语音合成

result0  = client.synthesis('你好百度', 'zh', 1)
# options中的tex字段会覆盖参数text
result1  = client.synthesis('你好百度', 'zh', 1, {
    'tex': '百事可乐','vol': 5,'per':4
})

def create_mp3(result,mp3_name):
    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open(mp3_name, 'wb') as f:
            f.write(result)
create_mp3(result0,"0.mp3")
create_mp3(result1,"1.mp3")

结果:
0.mp3的内容:你好百度
1.mp3的内容:百世可乐

client.synthesis的参数options的字段说明:


image.png

另外,暂时不支持英文。
文档连接:https://ai.baidu.com/ai-doc/SPEECH/Gk4nlz8tc

上一篇下一篇

猜你喜欢

热点阅读