赞
踩
大家好,我是空空star,本篇给大家分享一下通过Python的pyttsx3库将文字转为音频。
pyttsx3是一个开源的Python文本转语音库,可以将文本转换为自然的人类语音。它提供了丰富灵活的配置选项,可以自定义声音,语速,语调等等,并且支持多语言转换。此外,它还支持异步操作,可以在后台自动调用语音合成引擎,不会阻塞主程序。Pyttsx3可以广泛应用于各种领域,例如自动化语音提示、智能语音助手、语音验证等等,是一个非常优秀的Python语音处理库。
pip install 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
import pyttsx3
text = '大家好,我是空空star,本篇给大家分享一下文字转音频,这是通过pyttsx3转换的音频。'
engine = pyttsx3.init()
# 获取所有可用的声音列表
voices = engine.getProperty('voices')
# 选择一个指定语音(粤语语音sinji)
engine.setProperty('voice', voices[36].id)
这里我用的是mac系统下的粤语语音
com.apple.speech.synthesis.voice.sinji
engine.setProperty('rate', 150)
engine.setProperty('volume', 0.8)
engine.say(text)
local = '/Users/kkstar/Downloads/video/'
engine.save_to_file(text, local+"audio_pyttsx3.mp3")
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。