DeveloperBreeze

Python Speech-To-Text Development Tutorials, Guides & Insights

Unlock 1+ expert-curated python speech-to-text tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your python speech-to-text skills on DeveloperBreeze.

How to Convert Audio to Text with Python

Tutorial August 04, 2024
python

Save the following code as audio_to_text.py.

import speech_recognition as sr
from pydub import AudioSegment

def convert_audio_to_text(audio_file_path):
    # Initialize recognizer
    recognizer = sr.Recognizer()

    # Convert audio file to wav format if necessary
    if not audio_file_path.endswith('.wav'):
        audio = AudioSegment.from_file(audio_file_path)
        audio_file_path = 'converted.wav'
        audio.export(audio_file_path, format='wav')

    # Load the audio file
    with sr.AudioFile(audio_file_path) as source:
        audio_data = recognizer.record(source)

    # Recognize and convert audio to text
    try:
        text = recognizer.recognize_google(audio_data)
        print("Transcribed Text:")
        print(text)
        return text
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print(f"Could not request results from Google Speech Recognition service; {e}")

if __name__ == '__main__':
    audio_file = 'your_audio_file.wav'  # Replace with your audio file path
    convert_audio_to_text(audio_file)