大规模文本语音合成
先通过pip install baidu-api安装必要的库,然后通过调用百度api实现大规模文本的语音合成,这样就可以很容易把文本格式的图书转化为语音,从此开始听书生活。当然,也可以调用百度的api,实现大规模的文字识别,把pdf格式的图书转化为tx格式,然后再通过本文的方法实现语音合成。
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '********'
API_KEY = '**************************'
SECRET_KEY = '*****************************'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
file=open("耶路撒冷三千年.txt", encoding='utf-8')
text=file.readlines()
file.close()
soundF = open('audio.mp3','+wb')
for line in text:
#print(line)
data=[]
#split long sentences into small ones
if len(line)>2000:
data = line.split('。')
else:
data.append(line)
#convert txt to sound by baidu API
for sentences in data:
result = client.synthesis(sentences, 'zh', 1, {
'vol': 5,'per':4
})
if not isinstance(result, dict):
soundF.write(result)
print(len(sentences))
else:
print("failed length" + str(len(sentences)))
soundF.close()