使用Python实现文字转语音并生成wav文件的例子
作者:jgw2008 时间:2021-06-27 03:29:52
目前手边的一些工作,需要实现声音播放功能,而且仅支持wav声音格式。
现在,一些网站上支持文字转语音功能,但是生成的都是MP3文件,这样还需要额外的软件来转成wav文件,十分麻烦。
后来,研究Python,发现Python可以很容易的实现上面的功能。
步骤如下,
1。使用百度语音实现TTS(Text To Speech),生成mp3文件;
2。 使用pydub和ffmpeg实现mp3转wav格式。
下面,先上简单的示例代码,然后对代码作简单的分析。
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
from aip import AipSpeech
from pydub import AudioSegment
# Step 1, Using baidu AI to generate mp3 file from text
#input your APP_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('你好百度, good morning', 'zh', 1, { 'vol': 5,'per':4 })
if not isinstance(result, dict):
with open('test.mp3', 'wb') as f:
f.write(result)
# Step 2, convert the mp3 file to wav file
sound = AudioSegment.from_mp3('test.mp3')
sound.export("test.wav", format="wav")
运行上面的python代码,就会生成test.mp3和test.wav,使用命令“aplay test.wav”,可以测试播放声音。
下面对代码做解析,
1。在运行之前,需要安装下面的库,
1.1 安装百度AI模块,安装命令“pip install baidu-aip”
1.2 安装pydub,pydub是python的一个音频处理库处理,能对wav格式的音频直接进行处理,安装命令“pip install pydub”
1.3 安装ffmpeg,可以实现对mp3格式的处理,安装命令“sudo apt-get install ffmpeg”
2。需要注册百度的应用开发者账户,
用户可以自行去下面的网站去注册语音合成-百度AIai.baidu.com,注册百度云之后,去控制台创建应用,过程比较简单。
大家可以对上述代码进行进一步的优化,以实现自己想要的功能。
下面是完整的Python代码,大家可以拿来使用,
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
"""
module: baiduVoiceGenerate
platform: Linux
description: Generate Speech from Text, and convert the audio to wav file.
Precondition: Please install below module before run this progrom,
1. pip install baidu-aip
2. pip install pydub
3. sudo apt-get install ffmpeg
creater: Guangwei Jiang
create time: 2018-11-21
"""
from aip import AipSpeech
from pydub import AudioSegment
import time
#input your own APP_ID/API_KEY/SECRET_KEY
APP_ID = '14891501'
API_KEY = 'EIm2iXtvDSplvR5cyHU8dAeM'
SECRET_KEY = '4KkGGzTq2GVrBEYPLXXWEEIoyLL1F6Zt '
print("baiduVoiceGenerate: V1.0, by Guanagwei_Jiang, 20181121")
str = raw_input("请输入要转成语音的文字: ")
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis(str, 'zh', 1, { 'vol': 5,'per':4 })
if not isinstance(result, dict):
with open('temp.mp3', 'wb') as f:
f.write(result)
sound = AudioSegment.from_mp3('temp.mp3')
sound.export(time.strftime("%Y%m%d_%H%M%S", time.localtime())+".wav", format="wav")
来源:https://blog.csdn.net/jgw2008/article/details/84324156
标签:Python,文字,语音,wav
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
八个超级好用的Python自动化脚本(小结)
2021-03-02 14:52:43
![](https://img.aspxhome.com/file/2023/8/131208_0s.jpg)
python 获取毫秒级时间问题的解决
2022-01-06 15:27:30
python3字符串操作总结
2023-08-23 22:31:23
Python SQLite3数据库操作类分享
2023-08-24 01:04:02
python数据分析之DataFrame内存优化
2021-09-03 23:08:29
![](https://img.aspxhome.com/file/2023/4/121404_0s.png)
Python打工人必备之windows倒计时锁屏功能的实现
2021-01-18 04:21:21
Python反射的用法实例分析
2023-11-09 13:29:54
python 算法 排序实现快速排序
2022-09-04 03:20:17
一个CSS图片切换效果代码
2008-02-12 12:17:00
![](https://img.aspxhome.com/file/UploadPic/20082/12/2008212122716396s.jpg)
python中列表元素连接方法join用法实例
2023-01-20 17:49:31
Python实现识别手写数字大纲
2022-06-28 05:19:13
通过js获取div的background-image属性
2023-08-23 06:07:23
python飞机大战pygame碰撞检测实现方法分析
2023-01-05 18:55:52
关于windos10环境下编译python3版pjsua库的问题
2021-06-04 08:12:13
![](https://img.aspxhome.com/file/2023/3/85293_0s.png)
解析Oracle数据库中的对象集合schema
2023-07-22 20:42:34
![](https://img.aspxhome.com/file/2023/8/64458_0s.jpg)
PHP脚本监控Nginx 502错误并自动重启php-fpm
2023-09-26 00:11:45
为WordPress增加微博功能
2010-08-31 15:01:00
[翻译]标记语言和样式手册 Chapter 4 引用
2008-01-20 14:19:00
![](https://img.aspxhome.com/file/UploadPic/20081/20/2008120144013757s.jpg)
Python闭包实现计数器的方法
2021-01-23 09:11:00
在Python中进行自动化单元测试的教程
2023-07-16 04:12:30
![](https://img.aspxhome.com/file/2023/1/97711_0s.png)