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("/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("/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.

4 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:


    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.

  3. Leon says:

    I have been searching for a simple solution such as this! Thanks!
    But, shouldn’t it be ‘EndTime = EndMin*60*1000+EndSec*1000’ to get the desired results?

  4. tagoor says:

    Iam getting this errors while excuting the programing can you please help me

    Warning (from warnings module):
    File “C:\Python39\lib\site-packages\pydub\”, line 170
    warn(“Couldn’t find ffmpeg or avconv – defaulting to ffmpeg, but may not work”, RuntimeWarning)
    RuntimeWarning: Couldn’t find ffmpeg or avconv – defaulting to ffmpeg, but may not work
    Traceback (most recent call last):
    File “C:/Users/TAGOOR/Desktop/”, line 21, in
    extract.export(r”C:/Python39/Scripts/tagoor.mp3″, format=”mp3″)
    File “C:\Python39\lib\site-packages\pydub\”, line 963, in export
    p = subprocess.Popen(conversion_command, stdin=devnull, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    File “C:\Python39\lib\”, line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
    File “C:\Python39\lib\”, line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
    FileNotFoundError: [WinError 2] The system cannot find the file specified

Leave a Reply

Your email address will not be published.