Python语音识别脚本只能在一个wav文件上运行,而不能在另一个

时间:2019-09-23 15:13:48

标签: python speech-recognition wav

我正在尝试转录CSR电话。我的Python脚本使用30秒的哈佛语句文件成功完成了此操作,但实际上没有10分钟的通话。我被告知要检查文件的编码,但是我找不到解决方法。

我已经下载了libav并将bin文件夹设置为Path环境变量。

# Import necessary libraries 
from pydub import AudioSegment 
import speech_recognition as sr 
import os
import pydub


chunk_count = 0
directory = os.fsencode(r'C:\Users\zach.blair\Downloads\speechRecognition\New folder')
# Text file to write the recognized audio 
fh = open("recognized.txt", "w+")
for file in os.listdir(directory):
     filename = os.fsdecode(file)
     if filename.endswith(".wav"):
        chunk_count += 1
             # Input audio file to be sliced 
        audio = AudioSegment.from_wav(filename) 

        ''' 
        Step #1 - Slicing the audio file into smaller chunks. 
        '''
        # Length of the audiofile in milliseconds 
        n = len(audio) 

        # Variable to count the number of sliced chunks 
        counter = 1



        # Interval length at which to slice the audio file. 
        interval = 20 * 1000

        # Length of audio to overlap.  
        overlap = 1 * 1000

        # Initialize start and end seconds to 0 
        start = 0
        end = 0

        # Flag to keep track of end of file. 
        # When audio reaches its end, flag is set to 1 and we break 
        flag = 0

        # Iterate from 0 to end of the file, 
        # with increment = interval 
        for i in range(0, 2 * n, interval): 

            # During first iteration, 
            # start is 0, end is the interval 
            if i == 0: 
                start = 0
                end = interval 

            # All other iterations, 
            # start is the previous end - overlap 
            # end becomes end + interval 
            else: 
                start = end - overlap 
                end = start + interval  

            # When end becomes greater than the file length, 
            # end is set to the file length 
            # flag is set to 1 to indicate break. 
            if end >= n: 
                end = n 
                flag = 1

            # Storing audio file from the defined start to end 
            chunk = audio[start:end] 

            # Filename / Path to store the sliced audio 
            filename = str(chunk_count)+'chunk'+str(counter)+'.wav'

            # Store the sliced audio file to the defined path 
            chunk.export(filename, format ="wav") 
            # Print information about the current chunk 
            print(str(chunk_count)+str(counter)+". Start = "
                                +str(start)+" end = "+str(end)) 

            # Increment counter for the next chunk 
            counter = counter + 1


            AUDIO_FILE = filename 

            # Initialize the recognizer 
            r = sr.Recognizer() 

            # Traverse the audio file and listen to the audio 
            with sr.AudioFile(AUDIO_FILE) as source: 
                audio_listened = r.listen(source) 

            # Try to recognize the listened audio 
            # And catch expections. 
            try:     
                rec = r.recognize_google(audio_listened) 

                # If recognized, write into the file. 
                fh.write(rec+" ") 

            # If google could not understand the audio 
            except sr.UnknownValueError: 
                    print("Empty Value") 

            # If the results cannot be requested from Google. 
            # Probably an internet connection error. 
            except sr.RequestError as e: 
                print("Could not request results.") 

            # Check for flag. 
            # If flag is 1, end of the whole audio reached. 
            # Close the file and break.                  
fh.close()    

警告(来自警告模块):

  

文件   “ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pydub \ utils.py”,   165行       警告(“找不到ffmpeg或avconv-默认为ffmpeg,但可能不起作用”,RuntimeWarning)RuntimeWarning:找不到ffmpeg或   avconv-默认为ffmpeg,但可能不起作用

警告(来自警告模块):

  

文件   “ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pydub \ utils.py”,   193行       警告(“找不到ffprobe或avprobe-默认为ffprobe,但可能不起作用”,RuntimeWarning)RuntimeWarning:找不到   ffprobe或avprobe-默认为ffprobe,但可能无法工作   (最近通话最近):文件   “ C:\ Users \ zach.blair \ Downloads \ speechRecognition \ New   folder \ speechRecognition3.py“,第17行,在       音频= AudioSegment.from_wav(文件名)文件“ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pydub \ audio_segment.py”,   第728行,位于from_wav中       返回cls.from_file(文件,“ wav”,参数=参数)文件“ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pydub \ audio_segment.py” ,   from_file中的第665行       info = mediainfo_json(orig_file)文件“ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ pydub \ utils.py”,   mediainfo_json中的第263行       res = Popen(命令,stdin = stdin_parameter,stdout = PIPE,stderr = PIPE)文件   “ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ subprocess.py”,   第769行,在 init       restore_signals,start_new_session)文件“ C:\ Users \ zach.blair \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ subprocess.py”,   第1172行,_execute_child中       startupinfo)FileNotFoundError:[WinError 2]系统找不到指定的文件

0 个答案:

没有答案