Loop of function for taking multiple audio files from a directory
<p>I am currently taking input from a directory, for a single audio file and I am saving my output in CSV file, with the file name and converted speech to text output but I have 100 files in that directory (i.e 001.wav,002.wav,003.wav..........100.wav)</p>

<p>I want to write a loop or function which saves the speech to text output in CSV automatically with the respective file name in different rows.</p>

<p>Here's the code:</p>

<pre><code>import speech_recognition as sr
import csv
import os
AUDIO_FILE =path.join(path.dirname('C:/path/to/directory'), "001.wav")
file_name = os.path.basename(AUDIO_FILE)
name = os.path.basename(AUDIO_FILE)

# 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 Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    a =  r.recognize_google(audio)        
except sr.UnknownValueError:
    a = "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
    a = "Could not request results from Google Speech Recognition service; {0}".format(e)

try:
    b = r.recognize_sphinx(audio)
except sr.UnknownValueError:
    b = "Sphinx could not understand audio"
except sr.RequestError as e:
    b = "Sphinx error; {0}".format(e)

with open('speech_output.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(['file_name','google',sphinx])
writer.writerow([file_name,a,b])
</code></pre>

<p>References to the code.
<a href="https://github.com/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py" rel="nofollow">https://github.com/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py</a></p>

<p>You can get all files of a directory and subdirectory with <code>os.walk</code>, which I have included in the <code>get_file_paths()</code> in the code below, here is an example:</p>

<pre><code>import speech_recognition as sr
import csv
import os


DIRNAME = r'c:\path\to\directory'
OUTPUTFILE = r'c:\path\to\outputfiledir\outputfile.csv'

def get_file_paths(dirname):
    file_paths = []  
    for root, directories, files in os.walk(dirname):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)  
    return file_paths    

def process_file(file):
    r = sr.Recognizer()
    a = ''
    with sr.AudioFile(file) as source:
        audio = r.record(source)    
        try:
            a =  r.recognize_google(audio)        
        except sr.UnknownValueError:
            a = "Google Speech Recognition could not understand audio"
        except sr.RequestError as e:
            a = "Could not request results from Google Speech Recognition service; {0}".format(e)  
    return a

def main():
    files = get_file_paths(DIRNAME)                 # get all file-paths of all files in dirname and subdirectories
    for file in files:                              # execute for each file
        (filepath, ext) = os.path.splitext(file)    # get the file extension
        file_name = os.path.basename(file)          # get the basename for writing to output file
        if ext == '.wav':                           # only interested if extension is '.wav'
            a = process_file(file)                  # result is returned to a
            with open(OUTPUTFILE, 'a') as f:        # write results to file
                writer = csv.writer(f)
                writer.writerow(['file_name','google'])
                writer.writerow([file_name, a])            


if __name__ == '__main__':
    main()
</code></pre>

<p>If you want to do multiple recognizers, something like this could work. Please note this is an untested example:</p>

<pre><code>import speech_recognition as sr
import csv
import os


DIRNAME = r'c:\path\to\directory'
OUTPUTFILE = r'c:\path\to\outputfiledir\outputfile.csv'

def get_file_paths(dirname):
    file_paths = []  
    for root, directories, files in os.walk(dirname):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)  
    return file_paths    

def recog_multiple(file):
    r = sr.Recognizer()
    r_types = ['recognize_google', 'recognize_sphinx']
    results = []
    for r_type in r_types:
        result = ''
        with sr.AudioFile(file) as source:
            audio = r.record(source)
            try:
                result = r_type + ': ' + str(getattr(r, r_type)(audio))
            except sr.UnknownValueError:
                result = r_type + ': Speech Recognition could not understand audio'
            except sr.RequestError as e:
                result = r_type + ': Could not request results from Speech Recognition service; {0}'.format(e)        
        results.append(result)
    return results

def main():
    files = get_file_paths(DIRNAME)                 # get all file-paths of all files in dirname and subdirectories
    for file in files:                              # execute for each file
        (filepath, ext) = os.path.splitext(file)    # get the file extension
        file_name = os.path.basename(file)          # get the basename for writing to output file
        if ext == '.wav':                           # only interested if extension is '.wav'
            a = recog_multiple(file)                # result is returned to a
            with open(OUTPUTFILE, 'a') as f:        # write results to file
                writer = csv.writer(f)
                writer.writerow(['file_name','results'])
                writer.writerow([file_name, a])            


if __name__ == '__main__':
    main()
</code></pre>

python python-2.7 python-3.x