Skip to Content
Course content

339: Basic Audio Processing in Python

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When you first get into audio processing with Python, it's easy to get overwhelmed by the math—Fourier transforms, sample rates, and buffers. But for most of us, we just want to do "surgical" work: cut a clip, glue two files together, or change the volume. For that, I always reach for pydub. It's a wrapper that makes audio feel like Python lists, which is exactly how I like to think about data.

Today, we're going to build a simple "Sound Bite Generator." Imagine you have a long interview recording and you need to extract a specific 5-second highlight, fade it in and out so it doesn't pop, and save it as a high-quality MP3.

Loading the source file

First, we need to bring the audio into a format Python can manipulate. I'm using an AudioSegment object here. One thing to keep in mind: pydub depends on ffmpeg or avconv being installed on your system to handle formats like MP3 or OGG. If you're just using WAV, you're fine, but for everything else, make sure ffmpeg is in your path.

from pydub import AudioSegment

# Load our long recording
audio = AudioSegment.from_file("interview_raw.mp3")

# Let's check the length just to be sure
print(f"Total length: {len(audio)} ms")

The millisecond trap

Now, I want to grab a clip starting at the 10-second mark and lasting for 5 seconds. This is where I almost tripped up—and where you probably will too. In pydub, everything is measured in milliseconds, not seconds.

Here is my first (wrong) attempt:

# WRONG: I'm thinking in seconds
start_time = 10 
end_time = 15
highlight = audio[start_time:end_time]

When I played that back, I got basically nothing—a tiny flicker of sound. Why? Because I just told Python to give me the audio from the 10th millisecond to the 15th millisecond. That's 0.005 seconds of audio. I've made this mistake more times than I'd like to admit. Let's fix that by multiplying by 1,000.

# RIGHT: Convert seconds to milliseconds
start_ms = 10 * 1000
end_ms = 15 * 1000
highlight = audio[start_ms:end_ms]

Smoothing out the edges

If you just cut a clip and play it, you often get a "click" or a "pop" at the start and end because the waveform is being abruptly severed. It sounds unprofessional. To fix this, we'll apply a linear fade-in and fade-out. I usually go with 200ms; it's enough to smooth the transition without making the clip feel like it's lagging.

# Fade in for 200ms and fade out for 200ms
polished_clip = highlight.fade_in(200).fade_out(200)

# While we're at it, let's make sure it's normalized to a decent volume
# This increases the volume by 6dB
polished_clip = polished_clip + 6 

Exporting the final bite

Now that we've sliced and polished the audio, we need to write it back to disk. I prefer exporting to MP3 for sharing, but you can easily switch this to wav if you need lossless quality for further editing.

polished_clip.export("interview_highlight.mp3", format="mp3", bitrate="192k")
print("Sound bite exported successfully!")

And that's it. We treated the audio like a string, sliced it, applied a few filters, and saved it. It's a straightforward workflow that avoids the headache of manual byte-manipulation.




📋 Practical Task

Project: The Audio Stitcher

Your task is to create a script that merges two different audio files together, but with a specific requirement: there must be exactly 2 seconds of absolute silence between the two clips to act as a separator.

  • Load two separate audio files (you can use any small .wav or .mp3 files you have).
  • Create a "silent" segment of 2000 milliseconds using AudioSegment.silent(duration=2000).
  • Combine the first clip, the silence, and the second clip into one single AudioSegment.
  • Export the final result as combined_output.mp3.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.