python 百度引擎批量语音转文字

from pydub import AudioSegment
from aip import AipSpeech
import os

import shutil
#baidu-aip

# 百度AI库获取的参数

# 构造读取语音文件函数

def makedir():
    mydir = 'record'  # 基本逻辑就是分两种情况,文件夹有和无,有时,为了避免里面数据污染,删了重建一个;没有则是正常建一个就好

    if os.path.exists(mydir) == 0:
        os.mkdir(mydir)
    else:
        shutil.rmtree('record')

    if os.path.exists(mydir) == 0:
        os.mkdir(mydir)


def cut_wav(segment):
    wav[segment*60*1000:(segment+1)*60*1000].export('record'+'/'+filename+'cut'+str(segment)+'.wav', format="wav")
    #wav[2*60*1000:3*60*1000].export(filename+'cut'+segment+'.wav', format="wav")

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

if __name__ == '__main__':
    #开始第一步,将录音文件切成多个小片段


    makedir()
    filename = '04月05日_1'

    wav = AudioSegment.from_wav(filename + '.wav')
    totoal_time = wav.duration_seconds

    cut_count = int(wav.duration_seconds/60)
    print(cut_count)
    cut_wav(cut_count)

    for i in range(1, cut_count):
        cut_wav(i)

    #开始第二步,批量语音转文字

    APP_ID = '26029972'
    API_KEY = 'uDbACFSOmNVOkXyvydMSUuQR'
    SECRET_KEY = 'cuI1zBwGjgHtNFI3Mx15RXCQvrIrMYVN'

    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    result = client.asr(get_file_content(r'C:\Users\1\PycharmProjects\pythonProject\record\04月05日_1cut1.wav'), 'wav', 16000,
                        {'lan': 'zh', })
    # 此处地址处必须要加r,使其成为绝对地址,要么容易字符转义出现错误
    print(result)



    











你可能感兴趣的:(python批量语音转文字)