python语⾳转⽂字源码_python使⽤百度api实现⽂字转语⾳#百度⽂字转换为txt并存储为mp3为⽴体声41000采样频率。20181110 import os
import datetime
def main():
print('欢迎使⽤⽂本转语⾳程序')
print('输⼊⽂本名称,默认后缀为txt,回车默认为1.txt') filetxt=input('=')
if filetxt=='':
filetxt='1.txt'
outfile = 'xh1.mp3'
else:
气炮
outfile = filetxt+'xh'+ '.mp3'
filetxt=filetxt + '.txt'
txt = gettxt(filetxt)
baidu_txt2sound(txt,outfile,5,5,10,4)
print('顺利⽣成' + outfile)
x=input('回车结束')
def gettxt(txtfile):
# 得到⽂件⽂本
f = open(txtfile,encoding='utf8') # 返回⼀个⽂件对象 line = f.readline() # 调⽤⽂件的 readline()⽅法扎那米韦
txt=''
while line:
# print(line, end = '') # 在 Python 3 中使⽤
txt = txt + line
老板山
line = f.readline()郭德纲打油诗讽刺过世之人
f.close()
#print(txt)
return txt
def baidu_txt2sound(txt,outfile,spd,pit,vol,per):
#百度识别语⾳
print('语⾳识别中')
outtemp='txt2stemp.mp3'
from aip import AipSpeech
APP_ID = '2222222'
API_KEY = 'AAA9RPsBFFjkvzN46AE'
SECRET_KEY = 'bbxddkzm7jbA8IljOuMTgp4oqCIOhY9z'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
from aip import AipSpeech
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis(txt, 'zh', 1, {
'spd':spd,'pit':pit,'vol': vol,'per':per
})
print(result)
# 识别正确返回语⾳⼆进制 错误则返回dict 参照下⾯错误码
if not isinstance(result, dict):
with open(outtemp, 'wb') as f:
f.write(result)
from subprocess import call
浪潮财务共享delfile(outfile)
if ists(outtemp): # 如果⽂件存在
#将百度语⾳转换为⽴体声,并41000hz,可以ae编辑
call( 'ffmpeg -i ' + outtemp + ' -i ' + outtemp + ' -filter_complex "amovie=' + outtemp + ' [l]; amovie=' + outtemp + ' [r]; [l] [r] amerge" -ar 44100 ' + outfile + ' ')
delfile(outtemp)
def delfile(filepcm):
#删除⽂件
if ists(filepcm): # 如果⽂件存在
英国海外志愿服务社# os.unlink(my_file)
print('删除⽂件'+ filepcm)
if __name__ == '__main__':
main()