语音识别程序偶尔工作

2024-04-24 12:48:29 发布

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

我不知道为什么,但它只发现,转换和转录目录中的所有mp3文件偶尔,但从来没有百分之百的时间。我不知道为什么。我希望我问得对。我的目标是找到所有m4a文件,然后转换成wav文件,然后找到所有wav文件并转录它们。程序有时会这样做,但不是所有的时间

#!/usr/bin/env python3

    import speech_recognition as sr
    import time
    import subprocess
    import os
    from os import path
    from os import listdir


    # find and convert mp3 files to wav files

    # find all files with an extension, convert to wav files, transcribe to text file then transfer all wav files and mp3 files to finished directory and email transcribed files.

    # find files with mp3 extension

    def list_files1(directory, extension):
        ext = []
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(extension):
                    ext.append(file)
        print(ext)
        return ext


    # get directory path
    originalDir = os.getcwd()

    # call function to find files with mp3 extension
    mp3files = list_files1(originalDir, "m4a")

    # os.chdir("/Users/williamjohnson/Dropbox/goodscrapers/publimaison2/")

    # convert all mp3 files to wav files
    for x in mp3files:
        print(x)
        timestr = time.strftime("%Y%m%d-%H%M%S")
        command = "ffmpeg -i " + x + ' ' + timestr + ".wav"
        print(command)
        subprocess.call(command, shell=True)

    # find all converted wav files

    wavfiles = list_files1(originalDir, "wav")

    for y in wavfiles:
        print(y)
        # obtain path to "english.wav" in the same folder as this script
        AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), y)
        # AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
        # AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")

        # use the audio file as the audio source
        r = sr.Recognizer()
        with sr.AudioFile(AUDIO_FILE) as source:
            audio = r.record(source)  # read the entire audio file

        # recognize speech using Sphinx
        try:
            print("Sphinx thinks you said " + r.recognize_sphinx(audio))
            timestr = time.strftime("%Y%m%d-%H%M%S")
            text_file = open(timestr + ".txt", "a")
            text_file.write(r.recognize_sphinx(audio))
            text_file.close()
        except sr.UnknownValueError:
            print("Sphinx could not understand audio")
        except sr.RequestError as e:
            print("Sphinx error; {0}".format(e))

电子dit:It was 我犯了一个非常愚蠢的错误,我把所有输出的文本文件命名为同一个名称,这样它们就会被覆盖。我确保给它们一个唯一的名称,把名称降到毫秒,然后在文件名中添加一个随机数


Tags: topathinimportosasextensionfiles
1条回答
网友
1楼 · 发布于 2024-04-24 12:48:29

当您使用os.walk()时,它返回没有任何目录的文件,因此您正在收集一个文件列表,但丢弃了它们的目录名

包装器似乎没有在os.walk()上添加任何值;无论如何,我都会重构一次转换一个文件。另外,如果不特别关心当前目录的绝对路径,也不需要调用getcwd

import os

def get_next_file(directory, extension):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(extension):
                yield os.path.join(root, file)

for x in get_next_file('.', 'm4a'):
    print(x)

相关问题 更多 >