网络编程
位置:首页>> 网络编程>> Python编程>> 如何通过Python的pyttsx3库将文字转为音频

如何通过Python的pyttsx3库将文字转为音频

作者:空空star  发布时间:2023-01-11 19:54:59 

标签:Python,pyttsx3,文字,音频

前言

大家好,我是空空star,本篇给大家分享一下通过Python的pyttsx3库将文字转为音频。

一、pyttsx3是什么?

pyttsx3是一个开源的Python文本转语音库,可以将文本转换为自然的人类语音。它提供了丰富灵活的配置选项,可以自定义声音,语速,语调等等,并且支持多语言转换。此外,它还支持异步操作,可以在后台自动调用语音合成引擎,不会阻塞主程序。Pyttsx3可以广泛应用于各种领域,例如自动化语音提示、智能语音助手、语音验证等等,是一个非常优秀的Python语音处理库。

二、安装pyttsx3

pip install pyttsx3

三、查看pyttsx3版本

pip show pyttsx3

Name: pyttsx3
Version: 2.90
Summary: Text to Speech (TTS) library for Python 2 and 3. Works without internet connection or delay. Supports multiple TTS engines, including Sapi5, nsss, and espeak.
Home-page: https://github.com/nateshmbhat/pyttsx3

四、pyttsx3的使用

1.引入库

import pyttsx3

2.定义需要转换的文本

text = '大家好,我是空空star,本篇给大家分享一下文字转音频,这是通过pyttsx3转换的音频。'

3.初始化pyttsx3引擎

engine = pyttsx3.init()

4.设置声音

# 获取所有可用的声音列表
voices = engine.getProperty('voices')
# 选择一个指定语音(粤语语音sinji)
engine.setProperty('voice', voices[36].id)

这里我用的是mac系统下的粤语语音
com.apple.speech.synthesis.voice.sinji

5.设置语速

engine.setProperty('rate', 150)

6.设置音量

engine.setProperty('volume', 0.8)

7.播放语音

engine.say(text)

8.保存语音

local = '/Users/kkstar/Downloads/video/'
engine.save_to_file(text, local+"audio_pyttsx3.mp3")

9.等待完成语言转换

engine.runAndWait()

engine.runAndWait() 用于在文本转语音时,等待文本转换完成后再继续程序的执行。它的目的是在文本转换为语音时,避免出现程序过早结束,导致文本无法全部转换的情况。

五、语音效果

由于博客中不能插入mp3文件,所以先将其转为mp4,大家通过mp4的声音来听转换效果。

pyttsx3-文字转音频效果演示

如何通过Python的pyttsx3库将文字转为音频

来源:https://blog.csdn.net/weixin_38093452/article/details/130148334

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com