python语音转文字源码_python使用百度api实现文字转语音

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()

本文发布于:2024-09-22 03:34:17,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/233443.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:返回   百度   回车   默认   转语
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议