1.用pywin32模塊來將文本轉化為語音
通過pip install pywin32安裝模塊,pywin32是個萬金油的模塊,太多的場景使用到它,但在文本轉語音上,它卻是個青銅玩家,簡單無腦但效果不好。代碼示例:
import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") speaker.Speak("一天什么時候最安全?中午,因為早晚會出事...")
2.利用pyttsx3來轉化
pyttsx3使用pyttsx移植過來的,因為pyttsx不支持python3…針對不同的系統,模塊會自動所有系統對應的語音驅動,前提是你的系統存在該驅動…
它依賴pywin32模塊,可以說它時針對無腦的pywin32接口,進行了升級的個性化配置。先來看下最簡單的使用:
import pyttsx3 engine = pyttsx3.init() engine.say("明天你好,我叫干不倒!") engine.runAndWait()
代碼初始化模塊后,填寫你所需轉化的文本,之后執行runAndWait方法完成語音轉化。再來看看其相關操作:
事件監聽
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()
中斷話語
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()
改變聲音
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()
改變語速
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