程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python calls Baidu text to voice API

編輯:Python
from aip import AipSpeech
import os
import shutil
class getMp3(object):
"""docstring for getMp3"""
def __init__(self):
super(getMp3, self).__init__()
self.APP_ID = '25602943'
self.API_KEY = 'vyYLov63W6x33nIPvwVdLLsX'
self.SECRET_KEY = 'EzgQoFF9xp62SeGCXCBaD8FjWNxvl9kZ'
self.mp3Path = r'C:\Users\HK\Desktop\ speech recognition \sound2'
def main(self, text, filename, per):
client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) # These three parameters need to be registered with Baidu AI Obtain through cloud platform
result = client.synthesis(text, 'zh', 1, {
'vol': 5,
'per': per
}) # For the text to be converted, fill in the first attribute in the brackets
# vol For volume ,per Is the sound category
# Recognize the correct return speech binary Error returns dict Refer to the following error code , Generating audio
if not isinstance(result, dict):
with open(filename, 'wb') as f:
f.write(result)

mp3Path Is the path to save the voice file ,main In the method ,filename Is the filename of the audio file ,per Is the sound type of the audio file

aip The installation of the package pip install baidu-aip==2.2.18.0


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved