如何通过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库将文字转为音频_第1张图片

总结

voices如下,可以选择自己喜欢的声音。
com.apple.speech.synthesis.voice.Alex
com.apple.speech.synthesis.voice.alice.premium
com.apple.speech.synthesis.voice.alva
com.apple.speech.synthesis.voice.amelie
com.apple.speech.synthesis.voice.anna
com.apple.speech.synthesis.voice.carmit
com.apple.speech.synthesis.voice.damayanti
com.apple.speech.synthesis.voice.daniel
com.apple.speech.synthesis.voice.diego
com.apple.speech.synthesis.voice.ellen
com.apple.speech.synthesis.voice.fiona
com.apple.speech.synthesis.voice.Fred
com.apple.speech.synthesis.voice.ioana
com.apple.speech.synthesis.voice.joana
com.apple.speech.synthesis.voice.jorge
com.apple.speech.synthesis.voice.juan
com.apple.speech.synthesis.voice.kanya
com.apple.speech.synthesis.voice.karen
com.apple.speech.synthesis.voice.kyoko
com.apple.speech.synthesis.voice.laura
com.apple.speech.synthesis.voice.lekha
com.apple.speech.synthesis.voice.luca
com.apple.speech.synthesis.voice.luciana
com.apple.speech.synthesis.voice.maged
com.apple.speech.synthesis.voice.mariska
com.apple.speech.synthesis.voice.meijia
com.apple.speech.synthesis.voice.melina
com.apple.speech.synthesis.voice.milena
com.apple.speech.synthesis.voice.moira
com.apple.speech.synthesis.voice.monica
com.apple.speech.synthesis.voice.nora
com.apple.speech.synthesis.voice.paulina
com.apple.speech.synthesis.voice.rishi
com.apple.speech.synthesis.voice.samantha
com.apple.speech.synthesis.voice.sara
com.apple.speech.synthesis.voice.satu
com.apple.speech.synthesis.voice.sinji
com.apple.speech.synthesis.voice.tessa
com.apple.speech.synthesis.voice.thomas
com.apple.speech.synthesis.voice.tingting.premium
com.apple.speech.synthesis.voice.veena
com.apple.speech.synthesis.voice.Victoria
com.apple.speech.synthesis.voice.xander
com.apple.speech.synthesis.voice.yelda
com.apple.speech.synthesis.voice.yuna
com.apple.speech.synthesis.voice.yuri
com.apple.speech.synthesis.voice.zosia
com.apple.speech.synthesis.voice.zuzana

到此这篇关于如何通过Python的pyttsx3库将文字转为音频的文章就介绍到这了,更多相关Python 文字转为音频内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(如何通过Python的pyttsx3库将文字转为音频)