Python: Convert Large Audio File to Text Using SpeechRecognition and pydub

In this tutorial, we will introduce how to convert a large audio file to text using python SpeechRecognition and pydub library.

Python - Convert Large Audio File to Text Using SpeechRecognition and pydub

If you only want to convert a small audio file to text, you can read this tutorial:

Python: Convert Speech to Text Using SpeechRecognition

1.Importing libraries

import speech_recognition as sr 
import os 
from pydub import AudioSegment
from pydub.silence import split_on_silence

2.Create a speech recognition object

r = sr.Recognizer()

3.Split large audio file to chunks to convert

Here is a function example:

def get_large_audio_transcription(path):
    """
    Splitting the large audio file into chunks
    and apply speech recognition on each of these chunks
    """
    # open the audio file using pydub
    sound = AudioSegment.from_wav(path)  
    # split audio sound where silence is 700 miliseconds or more and get chunks
    chunks = split_on_silence(sound,
        # experiment with this value for your target audio file
        min_silence_len = 500,
        # adjust this per requirement
        silence_thresh = sound.dBFS-14,
        # keep the silence for 1 second, adjustable as well
        keep_silence=500,
    )
    folder_name = "audio-chunks"
    # create a directory to store the audio chunks
    if not os.path.isdir(folder_name):
        os.mkdir(folder_name)
    whole_text = ""
    # process each chunk 
    for i, audio_chunk in enumerate(chunks, start=1):
        # export audio chunk and save it in
        # the `folder_name` directory.
        chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
        audio_chunk.export(chunk_filename, format="wav")
        # recognize the chunk
        with sr.AudioFile(chunk_filename) as source:
            audio_listened = r.record(source)
            # try converting it to text
            try:
                text = r.recognize_google(audio_listened)
            except sr.UnknownValueError as e:
                print("Error:", str(e))
            else:
                text = f"{text.capitalize()}. "
                print(chunk_filename, ":", text)
                whole_text += text
    # return the text for all chunks detected
    return whole_text

4.Start to convert large audio file to text

path = "7601-291468-0006.wav"
print("\nFull text:", get_large_audio_transcription(path))

Run this code, you may find this result:

audio-chunks\chunk1.wav : His abode which you had fixed in a bowery or country seat. 
audio-chunks\chunk2.wav : At a short distance from the city. 
audio-chunks\chunk3.wav : Just at what is now called dutch street. 
audio-chunks\chunk4.wav : Sooner bounded with proofs of his ingenuity. 
audio-chunks\chunk5.wav : Patent smokejacks.