Import an audio file with PyDub
PyDub
's AudioSegment
class makes it easy to import and manipulate audio files with Python.
In this exercise, we'll import an audio file of interest by creating an instance of AudioSegment
.
To import an audio file, you can use the from_file()
function on AudioSegment
and pass it your target audio file's pathname as a string. The format
parameter gives you an option to specify the format of your audio file, however, this is optional as PyDub
will automatically infer it.
PyDub
works with .wav
files without any extra dependencies but for other file types like .mp3
, you'll need to install ffmpeg.
A sample audio file has been setup as wav_file.wav
, you can listen to it here.
This is a part of the course
“Spoken Language Processing in Python”
Exercise instructions
- Import
AudioSegment
frompydub
. - Call the
from_file
method and pass it the audio file pathname.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import AudioSegment from Pydub
from pydub import ____
# Create an AudioSegment instance
wav_file = AudioSegment.____(file=____,
format="wav")
# Check the type
print(type(wav_file))
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.
Not all audio files come in the same shape, size or format. Luckily, the PyDub library by James Robert provides tools which you can use to programmatically alter and change different audio file attributes such as frame rate, number of channels, file format and more. In this chapter, you'll learn how to use this helpful library to ensure all of your audio files are in the right shape for transcription.
Exercise 1: Introduction to PyDubExercise 2: Import an audio file with PyDubExercise 3: Play an audio file with PyDubExercise 4: Audio parameters with PyDubExercise 5: Adjusting audio parametersExercise 6: Manipulating audio files with PyDubExercise 7: Turning it down... then upExercise 8: Normalizing an audio file with PyDubExercise 9: Chopping and changing audio filesExercise 10: Splitting stereo audio to mono with PyDubExercise 11: Converting and saving audio files with PyDubExercise 12: Exporting and reformatting audio filesExercise 13: Manipulating multiple audio files with PyDubExercise 14: An audio processing workflowWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.