如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻
前言
大家好,我是空空star,本篇給大家分享一下通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻。
一、pyttsx3是什么?
pyttsx3是一個開源的Python文本轉(zhuǎn)語音庫,可以將文本轉(zhuǎn)換為自然的人類語音。它提供了豐富靈活的配置選項,可以自定義聲音,語速,語調(diào)等等,并且支持多語言轉(zhuǎn)換。此外,它還支持異步操作,可以在后臺自動調(diào)用語音合成引擎,不會阻塞主程序。Pyttsx3可以廣泛應(yīng)用于各種領(lǐng)域,例如自動化語音提示、智能語音助手、語音驗證等等,是一個非常優(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.定義需要轉(zhuǎn)換的文本
text = '大家好,我是空空star,本篇給大家分享一下文字轉(zhuǎn)音頻,這是通過pyttsx3轉(zhuǎn)換的音頻。'
3.初始化pyttsx3引擎
engine = pyttsx3.init()
4.設(shè)置聲音
# 獲取所有可用的聲音列表 voices = engine.getProperty('voices') # 選擇一個指定語音(粵語語音sinji) engine.setProperty('voice', voices[36].id)
這里我用的是mac系統(tǒng)下的粵語語音
com.apple.speech.synthesis.voice.sinji
5.設(shè)置語速
engine.setProperty('rate', 150)
6.設(shè)置音量
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.等待完成語言轉(zhuǎn)換
engine.runAndWait()
engine.runAndWait() 用于在文本轉(zhuǎn)語音時,等待文本轉(zhuǎn)換完成后再繼續(xù)程序的執(zhí)行。它的目的是在文本轉(zhuǎn)換為語音時,避免出現(xiàn)程序過早結(jié)束,導(dǎo)致文本無法全部轉(zhuǎn)換的情況。
五、語音效果
由于博客中不能插入mp3文件,所以先將其轉(zhuǎn)為mp4,大家通過mp4的聲音來聽轉(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)于如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻的文章就介紹到這了,更多相關(guān)Python 文字轉(zhuǎn)為音頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在 Linux/Mac 下為Python函數(shù)添加超時時間的方法
這篇文章主要介紹了在 Linux/Mac 下為Python函數(shù)添加超時時間,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02python進(jìn)行debug操作實戰(zhàn)訓(xùn)練
debug是編碼是非常重要的調(diào)試技巧,通過在運行過程中設(shè)置斷點,幫助開發(fā)人員更好的理解運行過程,下面這篇文章主要給大家介紹了關(guān)于python進(jìn)行debug操作的相關(guān)資料,需要的朋友可以參考下2023-06-06Python 中pandas索引切片讀取數(shù)據(jù)缺失數(shù)據(jù)處理問題
pandas是一個Python軟件包,提供快速,靈活和富于表現(xiàn)力的數(shù)據(jù)結(jié)構(gòu),旨在使使用“關(guān)系”或“標(biāo)記”數(shù)據(jù)既簡單又直觀。這篇文章主要介紹了pandas索引切片讀取數(shù)據(jù)缺失數(shù)據(jù)處理,需要的朋友可以參考下2019-10-10python合并RepeatMasker預(yù)測結(jié)果中染色體的overlap區(qū)域
這篇文章主要為大家介紹了python合并RepeatMasker預(yù)測結(jié)果中染色體的overlap區(qū)域?qū)崿F(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Python+ChatGPT實戰(zhàn)之進(jìn)行游戲運營數(shù)據(jù)分析
最近ChatGPT蠻火的,今天試著讓ta用Python語言寫了一篇數(shù)據(jù)分析實戰(zhàn)案例。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02python實現(xiàn)探測socket和web服務(wù)示例
這篇文章主要介紹了python實現(xiàn)探測socket和web服務(wù)示例,需要的朋友可以參考下2014-03-03