如何通過Python的pyttsx3庫將文字轉為音頻
前言
大家好,我是空空star,本篇給大家分享一下通過Python的pyttsx3庫將文字轉為音頻。
一、pyttsx3是什么?
pyttsx3是一個開源的Python文本轉語音庫,可以將文本轉換為自然的人類語音。它提供了豐富靈活的配置選項,可以自定義聲音,語速,語調等等,并且支持多語言轉換。此外,它還支持異步操作,可以在后臺自動調用語音合成引擎,不會阻塞主程序。Pyttsx3可以廣泛應用于各種領域,例如自動化語音提示、智能語音助手、語音驗證等等,是一個非常優(yōu)秀的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系統(tǒng)下的粵語語音
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() 用于在文本轉語音時,等待文本轉換完成后再繼續(xù)程序的執(zhí)行。它的目的是在文本轉換為語音時,避免出現(xiàn)程序過早結束,導致文本無法全部轉換的情況。
五、語音效果
由于博客中不能插入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
到此這篇關于如何通過Python的pyttsx3庫將文字轉為音頻的文章就介紹到這了,更多相關Python 文字轉為音頻內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

