一、問題
在學習的過程中,我們會涉及到將文本信息,轉化成語音的過程,比如:我爬取了一個小說的網站,我要將里面的內容進行語音處理。
目前能夠進行語音的方法還是很多,比如win32com,百度ai。
二、解決
1.這里介紹如何使用百度ai的方法,首先在http://ai.baidu.com/?track=cp:aipinzhuan|pf:pc|pp:AIpingtai|pu:title|ci:|kw:10005792,進入百度ai,
點擊【控制台】語音技術,登錄。
2.創建應用,按照信息填即可。
3.點擊查看sdk,根據sdk里面的內容,進行配置。
三、代碼
from config_pz import *
from aip import AipSpeech from playsound import playsound client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) with open(r'F:\exploitation\codes\python\Spider\text\reading.txt', 'r', encoding='utf-8') as f: content_s = f.read() result = client.synthesis(content_s, 'zh', 1, { 'vol': 5, 'spd': 1, 'pit': 7, 'per': 4, }) if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result) playsound("auido.mp3")
四、總結
可以將信息寫在配置文件中,這樣可以確保信息安全。百度ai是有讀取的限制,可在官方文檔查看,因此超額可能會失敗。(后面繼續補充)