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

本文介绍了Python的pyttsx3库,这是一个用于文本转语音的开源库。文章详细讲解了如何安装pyttsx3,查看版本,以及如何设置声音、语速和音量来转换并播放语音。此外,还展示了如何保存转换后的音频文件。
摘要由CSDN通过智能技术生成


前言

大家好,我是空空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-文字转音频效果演示


总结

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

  • 86
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 133
    评论
评论 133
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空空star

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值