Level 1 Python: Create an Audio Clipping Tool

Create an Audio Clipping Tool in Python

Clipping audio files is one of the most basic functions of working with audio data. The `pydub` library makes this super easy. Just like the piece about cropping and resizing images, the only reason this program makes it into the Level 1 Python category is the use of an external library.

In this post we’re going to cover how to use `pydub` to both clip audio, and save it to a file. See this post for a full guide to manipulating audio data in Python. It goes over how to resample, merge, and overlay audio data and more. Before we dive into the code, you’ll need to install `pydub` with your package manager. I use `pip install pydub`. 

In this post we’ll cover:

  • How to Clip an Audio File in Python
  • How to Save and Clip an Audio File
  • Summary of Clipping and Saving Audio Files with Python

How to Clip an Audio File in Python

The first thing that we do is import the `AudioSegment` object from `pydub`. This is going to do most of our work for us. In our `clip_audio` function, we take three parameters. The sound itself, and the start and end of the clip that we want. Start and end have to be specified in milliseconds.

In the function, we simply take advantage of `AudioSegment` objects being able to access their frames like lists. We store the snippet from the passed in start to end milliseconds in a variable and return that variable. Technically, we could skip storing it and just return the slice of audio.

from pydub import AudioSegment
 
def clip_audio(sound: AudioSegment, start, end):
   extracted = sound[start:end]
   return extracted

How to Save and Clip an Audio File

This code goes in the same file as the code above. This function doesn’t just clip an audio file, but also saves it. The `clip_and_save_audio` function takes four parameters. The first three are the same as the `clip_audio` function, the fourth is a filename. 

We pass the first three functions exactly as they’re passed into the `clip_audio` function we made above. This returns an audio clip to us that we then `export` to a filename and format. You can specify the format and filename to whatever format you need. Just make sure that the filename you export to ends in the format extension.

def clip_and_save_audio(sound: AudioSegment, start, end, filename):
   extracted = clip_audio(sound, start, end)
   extracted.export(f"{filename}.wav", format="wav")

Summary of Clipping and Saving an Audio File in Python

Editing audio files doesn’t have to be hard. We can create simple Python tools that will help us do edits like clipping and saving in seconds. In this post we used `pydub` and its `AudioSegment` object to clip and save an audio file. See this post for a full guide to manipulating audio data in Python.

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

Leave a Reply

%d bloggers like this: