需要在pycharm安裝百度aip, ffmpeg, pydub 的包,可識別mp3,wav,pcm, amr檔案
from pydub import AudioSegment
import ffmpeg
sound=AudioSegment.from_file(r"C:\Users\xx\Desktop\樣本.mp3","mp3")
sound = sound.set_channels(1) #多聲道轉單聲道
sound.export("轉換聲道后樣本.wav", format="wav")
ffmpeg.input(r"轉換聲道后樣本.wav").output('轉換完畢樣本.wav', ar=16000).run() #轉換采樣率
# frames_per_second = sound.frame_rate
# print(frames_per_second)
# channel_count = sound.channels
# print(channel_count)
from aip import AipSpeech
#注冊一個百度API就有
APP_ID = '124541517'
API_KEY = '12DViLjkjV2svOcsGzutbi9nt'
SECRET_KEY = '1Xb89xy5LB5ax3F0utUqcsmRis5OBZiv7'
# 百度AI庫獲取的引數
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 構造讀取語音檔案函式
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 識別本地檔案 主函式
result = client.asr(get_file_content(r'D:\編程\python.pycharm\2020\2021\pythonLearning\轉換完畢樣本.wav'), 'wav', 16000, { 'lan': 'zh',})
#此處地址處必須要加r,使其成為絕對地址,要么容易字符轉義出現錯誤
print(result)
參考:
AudioSegment
百度api語音識別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/289709.html
標籤:其他
上一篇:影像模板匹配演算法 c++
