Python语音识别脚本可以在一个wav文件上工作,但不能在其他文件上工作

2024-06-26 17:45:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在录CSR电话。我的Python脚本在一个30秒的Harvard语句文件中成功地实现了这一点,但在实际的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()    

警告(来自警告模块):

File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pydub\utils.py", line 165 warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning) RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work

警告(来自警告模块):

File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pydub\utils.py", line 193 warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning) RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work Traceback (most recent call last): File "C:\Users\zach.blair\Downloads\speechRecognition\New folder\speechRecognition3.py", line 17, in audio = AudioSegment.from_wav(filename) File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pydub\audio_segment.py", line 728, in from_wav return cls.from_file(file, 'wav', parameters=parameters) File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pydub\audio_segment.py", line 665, in from_file info = mediainfo_json(orig_file) File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE) File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 769, in init restore_signals, start_new_session) File "C:\Users\zach.blair\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1172, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified


Tags: thetoinfrompylinestartaudio