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
After installation import the package
import speech_recognition
Import the audio file to be converted
audio_file = "sample.wav"
initialize the speech recognizer
sp = speech_recognition.Recognizer()
open the audio file
with speech_recognition.AudioFile(audio_file) as source:
Next is to listen to the audio file by loading it to memory
audio_data = sp.record(source)
Convert the audio in memory to text
converted_text = sp.recognize_google(audio_data)
Print out the converted text
print(converted_text)
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)
Top comments (0)