使用Python实现文字转语音并生成wav文件的例子
目前手边的一些工作,需要实现声音播放功能,而且仅支持wav声音格式。
现在,一些网站上支持文字转语音功能,但是生成的都是MP3文件,这样还需要额外的软件来转成wav文件,十分麻烦。
后来,研究Python,发现Python可以很容易的实现上面的功能。
步骤如下,
1。使用百度语音实现TTS(TextToSpeech),生成mp3文件;
2。使用pydub和ffmpeg实现mp3转wav格式。
下面,先上简单的示例代码,然后对代码作简单的分析。
#!/usr/bin/python-tt
#-*-coding:utf-8-*-
fromaipimportAipSpeech
frompydubimportAudioSegment
#Step1,UsingbaiduAItogeneratemp3filefromtext
#inputyourAPP_ID/API_KEY/SECRET_KEY
APP_ID='Your_APP_ID'
API_KEY='Your_API_KEY'
SECRET_KEY='Your_Secret_Key'
client=AipSpeech(APP_ID,API_KEY,SECRET_KEY)
result=client.synthesis('你好百度,goodmorning','zh',1,{'vol':5,'per':4})
ifnotisinstance(result,dict):
withopen('test.mp3','wb')asf:
f.write(result)
#Step2,convertthemp3filetowavfile
sound=AudioSegment.from_mp3('test.mp3')
sound.export("test.wav",format="wav")
运行上面的python代码,就会生成test.mp3和test.wav,使用命令“aplaytest.wav”,可以测试播放声音。
下面对代码做解析,
1。在运行之前,需要安装下面的库,
1.1安装百度AI模块,安装命令“pipinstallbaidu-aip”
1.2安装pydub,pydub是python的一个音频处理库处理,能对wav格式的音频直接进行处理,安装命令“pipinstallpydub”
1.3安装ffmpeg,可以实现对mp3格式的处理,安装命令“sudoapt-getinstallffmpeg”
2。需要注册百度的应用开发者账户,
用户可以自行去下面的网站去注册语音合成-百度AIai.baidu.com,注册百度云之后,去控制台创建应用,过程比较简单。
大家可以对上述代码进行进一步的优化,以实现自己想要的功能。
下面是完整的Python代码,大家可以拿来使用,
#!/usr/bin/python-tt
#-*-coding:utf-8-*-
"""
module:baiduVoiceGenerate
platform:Linux
description:GenerateSpeechfromText,andconverttheaudiotowavfile.
Precondition:Pleaseinstallbelowmodulebeforerunthisprogrom,
1.pipinstallbaidu-aip
2.pipinstallpydub
3.sudoapt-getinstallffmpeg
creater:GuangweiJiang
createtime:2018-11-21
"""
fromaipimportAipSpeech
frompydubimportAudioSegment
importtime
#inputyourownAPP_ID/API_KEY/SECRET_KEY
APP_ID='14891501'
API_KEY='EIm2iXtvDSplvR5cyHU8dAeM'
SECRET_KEY='4KkGGzTq2GVrBEYPLXXWEEIoyLL1F6Zt'
print("baiduVoiceGenerate:V1.0,byGuanagwei_Jiang,20181121")
str=raw_input("请输入要转成语音的文字:")
client=AipSpeech(APP_ID,API_KEY,SECRET_KEY)
result=client.synthesis(str,'zh',1,{'vol':5,'per':4})
ifnotisinstance(result,dict):
withopen('temp.mp3','wb')asf:
f.write(result)
sound=AudioSegment.from_mp3('temp.mp3')
sound.export(time.strftime("%Y%m%d_%H%M%S",time.localtime())+".wav",format="wav")
以上这篇使用Python实现文字转语音并生成wav文件的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。