DEV Community

Cover image for Convert audio file to text - Python
Emmanuel Larbi
Emmanuel Larbi

Posted on

Convert audio file to text - Python

I had an audio file that I wanted it in text form. Listening and typing it manually is a hassle, as a programmer I made a quick research on how to convert audio files to text.
Let's get started.

Requirements

  • speech_recognition
pip install speech_recognition
Enter fullscreen mode Exit fullscreen mode

After installation import the package

import speech_recognition
Enter fullscreen mode Exit fullscreen mode

Import the audio file to be converted

audio_file = "sample.wav"
Enter fullscreen mode Exit fullscreen mode

initialize the speech recognizer

 sp = speech_recognition.Recognizer()
Enter fullscreen mode Exit fullscreen mode

open the audio file

with speech_recognition.AudioFile(audio_file) as source:
Enter fullscreen mode Exit fullscreen mode

Next is to listen to the audio file by loading it to memory

audio_data = sp.record(source)
Enter fullscreen mode Exit fullscreen mode

Convert the audio in memory to text

converted_text = sp.recognize_google(audio_data)
Enter fullscreen mode Exit fullscreen mode

Print out the converted text

print(converted_text)
Enter fullscreen mode Exit fullscreen mode

Done.

This script works for short audio files and the file format should be .wav

Complete Code

#import package
import speech_recognition

#import audio file
audio_file = "sample.wav"

# initialize the recognizer
sp = speech_recognition.Recognizer()

# open the file
with speech_recognition.AudioFile(audio_file) as source:
    # load audio to memory
    audio_data = sp.record(source)
    # convert speech to text
    text = sp.recognize_google(audio_data)
    print(text)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)