如何通過(guò)Python的pyttsx3庫(kù)將文字轉(zhuǎn)為音頻
前言
大家好,我是空空star,本篇給大家分享一下通過(guò)Python的pyttsx3庫(kù)將文字轉(zhuǎn)為音頻。
一、pyttsx3是什么?
pyttsx3是一個(gè)開(kāi)源的Python文本轉(zhuǎn)語(yǔ)音庫(kù),可以將文本轉(zhuǎn)換為自然的人類語(yǔ)音。它提供了豐富靈活的配置選項(xiàng),可以自定義聲音,語(yǔ)速,語(yǔ)調(diào)等等,并且支持多語(yǔ)言轉(zhuǎn)換。此外,它還支持異步操作,可以在后臺(tái)自動(dòng)調(diào)用語(yǔ)音合成引擎,不會(huì)阻塞主程序。Pyttsx3可以廣泛應(yīng)用于各種領(lǐng)域,例如自動(dòng)化語(yǔ)音提示、智能語(yǔ)音助手、語(yǔ)音驗(yàn)證等等,是一個(gè)非常優(yōu)秀的Python語(yǔ)音處理庫(kù)。
二、安裝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.引入庫(kù)
import pyttsx3
2.定義需要轉(zhuǎn)換的文本
text = '大家好,我是空空star,本篇給大家分享一下文字轉(zhuǎn)音頻,這是通過(guò)pyttsx3轉(zhuǎn)換的音頻。'
3.初始化pyttsx3引擎
engine = pyttsx3.init()
4.設(shè)置聲音
# 獲取所有可用的聲音列表
voices = engine.getProperty('voices')
# 選擇一個(gè)指定語(yǔ)音(粵語(yǔ)語(yǔ)音sinji)
engine.setProperty('voice', voices[36].id)這里我用的是mac系統(tǒng)下的粵語(yǔ)語(yǔ)音
com.apple.speech.synthesis.voice.sinji
5.設(shè)置語(yǔ)速
engine.setProperty('rate', 150)6.設(shè)置音量
engine.setProperty('volume', 0.8)7.播放語(yǔ)音
engine.say(text)
8.保存語(yǔ)音
local = '/Users/kkstar/Downloads/video/' engine.save_to_file(text, local+"audio_pyttsx3.mp3")
9.等待完成語(yǔ)言轉(zhuǎn)換
engine.runAndWait()
engine.runAndWait() 用于在文本轉(zhuǎn)語(yǔ)音時(shí),等待文本轉(zhuǎn)換完成后再繼續(xù)程序的執(zhí)行。它的目的是在文本轉(zhuǎn)換為語(yǔ)音時(shí),避免出現(xiàn)程序過(guò)早結(jié)束,導(dǎo)致文本無(wú)法全部轉(zhuǎn)換的情況。
五、語(yǔ)音效果
由于博客中不能插入mp3文件,所以先將其轉(zhuǎn)為mp4,大家通過(guò)mp4的聲音來(lái)聽(tīng)轉(zhuǎn)換效果。
pyttsx3-文字轉(zhuǎn)音頻效果演示

總結(jié)
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
到此這篇關(guān)于如何通過(guò)Python的pyttsx3庫(kù)將文字轉(zhuǎn)為音頻的文章就介紹到這了,更多相關(guān)Python 文字轉(zhuǎn)為音頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python 利用pyttsx3文字轉(zhuǎn)語(yǔ)音過(guò)程詳解
- Python語(yǔ)音合成的項(xiàng)目實(shí)戰(zhàn)(PyQt5+pyttsx3)
- Python語(yǔ)音合成之第三方庫(kù)gTTs/pyttsx3/speech橫評(píng)(內(nèi)附使用方法)
- python利用pyttsx3 API實(shí)現(xiàn)文本轉(zhuǎn)語(yǔ)音處理
- Python調(diào)用pyttsx3實(shí)現(xiàn)離線文字轉(zhuǎn)語(yǔ)音的方式
- Python文本到語(yǔ)音轉(zhuǎn)換庫(kù)pyttsx3的安裝及使用全面指南
- Python pyttsx3庫(kù)實(shí)現(xiàn)文本轉(zhuǎn)語(yǔ)音功能的示例
相關(guān)文章
Python實(shí)現(xiàn)可自定義大小的截屏功能
這篇文章主要介紹了Python實(shí)現(xiàn)可自定義大小的截屏功能,結(jié)合實(shí)例形式分析了Python針對(duì)指定范圍的截圖、保存等功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
Python實(shí)現(xiàn)導(dǎo)彈自動(dòng)追蹤代碼實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)導(dǎo)彈自動(dòng)追蹤代碼實(shí)例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
Python中22個(gè)萬(wàn)用公式的小結(jié)
在大家的日常python程序的編寫過(guò)程中,都會(huì)有自己解決某個(gè)問(wèn)題的解決辦法,或者是在程序的調(diào)試過(guò)程中,用來(lái)幫助調(diào)試的程序公式,本文總結(jié)了22個(gè)萬(wàn)用公式,感興趣的可以了解一下2021-07-07
Python中Django框架利用url來(lái)控制登錄的方法
這篇文章主要介紹了Python中Django框架利用url來(lái)控制登錄的方法,實(shí)例分析了Django框架實(shí)現(xiàn)URL登陸的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

