How to cut a particular portion of an MP3 file in Python

In this tutorial, we are going to learn about one short python program as mentioned in the title that is ‘How to cut a particular portion of an MP3 file in Python’. It’s easy and simple let’s see how it works.

There are various libraries available in python for various applications and work. What we want to perform in python. It gives us a Library to use for working on it to make our task simple. For working with Audio files here we are using ‘pydub’ library. By using this library we can play, cut, merge, split, or edit Audio files.

For this first, we have to install ‘pydub’ library to our system. We can do this using the pip command as shown below in your terminal or shell.

pip install pydub

After executing the above command pydub will be installed in your machine. In the next code, we can select the duration of the file we want to cut. Or we can select the portion we required.

Selecting and saving a particular portion of an MP3 file in Python

from pydub import AudioSegment

#importing file from location by giving its path
sound = AudioSegment.from_mp3("https://cdn.codespeedy.com/content/audio/file.mp3")

#Selecting Portion we want to cut
StrtMin = 0
StrtSec = 8

EndMin = 0
EndSec = 22

# Time to milliseconds conversion
StrtTime = StrtMin*60*1000+StrtSec*1000
EndTime = StrtMin*60*1000+EndSec*1000

# Opening file and extracting portion of it
extract = sound[StrtTime:EndTime]

# Saving file in required location
extract.export("https://cdn.codespeedy.com/content/audio/new/portion.mp3", format="mp3")

# new file portion.mp3 is saved at required location

In the above code firstly we imported AudioSegment from pydub library. Then we are storing our file file.mp3 as an input in a variable called sound for that we have given the path of our file.  Later we declared variables for selecting the portion of the mp3 file, which is in Minutes and Seconds.

This library uses milliseconds as a unit to measure time, so we have to convert our time to milliseconds. For that, we have declared variables named StrtTime and EndTime. To perform the cut operation we used the python Slicing operation with ranges as StrtTime and EndTime. We extracted part of our mp3 file and stored it into the variable extract.

Next, we will use the export function to save extracted part. It takes the path/location of the file as a parameter also we have to attach the file name with it. We are giving format=mp3 so as a result file will be saved as mp3.
After execution of the above code. The output file portion.mp3 will be saved in the given location.

2 responses to “How to cut a particular portion of an MP3 file in Python”

  1. Mark Thompson says:

    Had trouble getting this working at first (I had never used Python). Turns out that it depends on ffmpeg and won’t work if that’s not installed first. But once it’s installed, it works quite nicely.

  2. Alex says:

    Hi,

    Thanks a lot for this article! It helped me in preparation for the funeral of my grandma.

    As a small aside: There’s a typo in the code “EndTime = StrtMin*60*1000+EndSec*1000”, it ought to be EndMin.

Leave a Reply

Your email address will not be published.