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 os.path.exists(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 os.path.exists(filepcm): # 如果文件存在

os.remove(filepcm) # 则删除

# os.unlink(my_file)

print('删除文件'+ filepcm)

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值