Converting audio to the right format
Acme Studios have asked you to do a proof of concept to find out more about their audio files.
After exploring them briefly, you find there's a few calls but they're in the wrong file format for transcription.
As you'll be interacting with many audio files, you decide to begin by creating some helper functions.
The first one, convert_to_wav(filename)
takes a file path and uses PyDub
to convert it from a non-wav format to .wav
format.
Once it's built, we'll use the function to convert Acme's first call, call_1.mp3
, from .mp3
format to .wav
.
PyDub
's AudioSegment
class has already been imported. Remember, to work with non-wav files, you'll need ffmpeg
(docs).
This is a part of the course
“Spoken Language Processing in Python”
Exercise instructions
- Import the
filename
parameter usingAudioSegment
'sfrom_file()
. - Set the export format to
"wav"
. - Pass the target audio file,
call_1.mp3
, to the function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create function to convert audio file to wav
def convert_to_wav(filename):
"""Takes an audio file of non .wav format and converts to .wav"""
# Import audio file
audio = AudioSegment.____(filename)
# Create new filename
new_filename = filename.split(".")[0] + ".wav"
# Export file as .wav
audio.export(new_filename, format=____)
print(f"Converting {filename} to {new_filename}...")
# Test the function
convert_to_wav(____)
This exercise is part of the course
Spoken Language Processing in Python
Learn how to load, transform, and transcribe speech from raw audio files in Python.
In this chapter, you'll put everything you've learned together by building a speech processing proof of concept project for a technology company, Acme Studios. You'll start by transcribing customer support call phone call audio snippets to text. Then you'll perform sentiment analysis using NLTK, named entity recognition using spaCy and text classification using scikit-learn on the transcribed text.
Exercise 1: Creating transcription helper functionsExercise 2: Converting audio to the right formatExercise 3: Finding PyDub statsExercise 4: Transcribing audio with one lineExercise 5: Using the helper functions you've builtExercise 6: Sentiment analysis on spoken language textExercise 7: Analyzing sentiment of a phone callExercise 8: Sentiment analysis on formatted textExercise 9: Named entity recognition on transcribed textExercise 10: Named entity recognition in spaCyExercise 11: Creating a custom named entity in spaCyExercise 12: Classifying transcribed speech with SklearnExercise 13: Preparing audio files for text classificationExercise 14: Transcribing phone call excerptsExercise 15: Organizing transcribed phone call dataExercise 16: Create a spoken language text classifierExercise 17: Congratulations!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.