1.用pywin32模塊來將文本轉化爲語音python
經過pip install pywin32安裝模塊,pywin32是個萬金油的模塊,太多的場景使用到它,但在文本轉語音上,它倒是個青銅玩家,簡單無腦但效果很差。代碼示例:api
import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") speaker.Speak("一天何時最安全?中午,由於遲早會出事...")
2.利用pyttsx3來轉化安全
pyttsx3使用pyttsx移植過來的,由於pyttsx不支持python3…針對不一樣的系統,模塊會自動全部系統對應的語音驅動,前提是你的系統存在該驅動…app
它依賴pywin32模塊,能夠說它時針對無腦的pywin32接口,進行了升級的個性化配置。先來看下最簡單的使用:ide
import pyttsx3 engine = pyttsx3.init() engine.say("明天你好,我叫幹不倒!") engine.runAndWait()
代碼初始化模塊後,填寫你所需轉化的文本,以後執行runAndWait方法完成語音轉化。再來看看其相關操做:oop
事件監聽ui
import pyttsx3 def onStart(name): print('starting', name) def onWord(name, location, length): print('word', name, location, length) def onEnd(name, completed): print('finishing', name, completed) engine = pyttsx3.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()
中斷話語spa
import pyttsx3 def onWord(name, location, length): print 'word', name, location, length if location > 10: engine.stop() engine = pyttsx3.init() engine.connect('started-word', onWord) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()
改變聲音code
import pyttsx3 engine = pyttsx3.init() voices = engine.getProperty('voices') for voice in voices: engine.setProperty('voice', voice.id) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()
改變語速blog
import pyttsx3 engine = pyttsx3.init() rate = engine.getProperty('rate') engine.setProperty('rate', rate+50) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()
改變音量
import pyttsx3 engine = pyttsx3.init() volume = engine.getProperty('volume') engine.setProperty('volume', volume-0.25) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()
運行驅動程序事件循環
import pyttsx3 engine = pyttsx3.init() def onStart(name): print 'starting', name def onWord(name, location, length): print 'word', name, location, length def onEnd(name, completed): print 'finishing', name, completed if name == 'fox': engine.say('What a lazy dog!', 'dog') elif name == 'dog': engine.endLoop() engine = pyttsx3.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine.say('The quick brown fox jumped over the lazy dog.', 'fox') engine.startLoop()
使用外部事件循環
import pyttsx3 engine = pyttsx3.init() engine.say('The quick brown fox jumped over the lazy dog.', 'fox') engine.startLoop(False) # engine.iterate() must be called inside externalLoop() externalLoop() engine.endLoop()
3.使用百度語音
百度語音識別api:baidu-aip是百度開放的公共語音轉化服務。只須要在百度註冊相關的app及祕鑰信息便可使用。
使用流程以下:
訪問語音合成-百度AI開放平臺:http://ai.baidu.com/tech/speech/tts
以後使用百度帳號便可登錄(沒有百度帳號的,本身註冊一個)
建立應用,添加語音識別的功能,並完成註冊
保存你的app_id, API_Key, Secret_Key 三項數據留着後續使用
切換回語音合成首頁,點擊當即使用旁邊的技術文檔按鈕,進入API文檔
定位 語音合成—>SDK文檔—>Python SDK,便可看到詳細的開發文檔說明
接下來,咱們看看文檔中的相關說明:
接口描述
基於該接口,開發者能夠輕鬆的獲取語音合成能力
請求說明
合成文本長度必須小於1024字節,若是本文長度較長,能夠採用屢次請求的方式。文本長度不可超過限制
舉例,要把一段文字合成爲語音文件:
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis('你好百度', 'zh', 1, {
'vol': 5,
})
# 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼
if not isinstance(result, dict):
with open('auido.mp3', 'wb') as f:
f.write(result)
# 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼
if not isinstance(result, dict):
with open('auido.mp3', 'wb') as f:
f.write(result)
在上面代碼中,常量APP_ID在百度雲控制檯中建立,常量API_KEY與SECRET_KEY是在建立完畢應用後,系統分配給用戶的,均爲字符串,用於標識用戶,爲訪問作簽名驗證,可在AI服務控制檯中的應用列表中查看。
相比於前兩種模塊,baidu-aip倒是高端不少啊…喜歡的朋友能夠下載了玩玩
轉載網址:https://bbs.huaweicloud.com/blogs/115130?from=singlemessage&isappinstalled=0
使用Python將任正非400+篇演講批量轉化爲語音https://www.jianshu.com/p/05f9874b6989