Get startedGet started for free

Transcribing audio with one line

Alright, now you've got functions to convert audio files and find out their attributes, it's time to build one to transcribe them.

In this exercise, you'll build transcribe_audio() which takes a filename as input, imports the filename using speech_recognition's AudioFile class and then transcribes it using recognize_google().

You've seen these functions before but now we'll put them together so they're accessible in a function.

To test it out, we'll transcribe Acme's first call, "call_1.wav".

speech_recognition has been imported as sr.

This exercise is part of the course

Spoken Language Processing in Python

View Course

Exercise instructions

  • Define a function called transcribe_audio which takes filename as an input parameter.
  • Setup a Recognizer() instance as recognizer.
  • Use recognize_google() to transcribe the audio data.
  • Pass the target call to the function.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

____ ____(____):
  """Takes a .wav format audio file and transcribes it to text."""
  # Setup a recognizer instance
  ____ = sr.Recognizer()
  
  # Import the audio file and convert to audio data
  audio_file = sr.AudioFile(filename)
  with audio_file as source:
    audio_data = recognizer.record(source)
  
  # Return the transcribed text
  return recognizer.____(audio_data)

# Test the function
print(transcribe_audio(____))
Edit and Run Code