How to trim part of audio file using python

In this tutorial you are going to learn about  how  to trim or slice  a part of the mp3 file using python. Now a days if you want to trim the mp3 file you need to download  separate software to trim, Which is very inconvenient. So to solve this problem we can use python to trim the mp3 file with a few lines of code.
so that there will be no need to download a new software in our systems.

Trimming the mp3 file is so easy in python that even the beginner can do it. So let’s start our tutorial

Trimming The Audio File with python

For trimming a mp3 file in python first you need to download a module called pydub, This module will help you to trim or slice the mp3 file. But first, Before going to the procedure we learn about the pydub module.

pydub module:

The pydub is a package in python that is used to manipulate the audio files or mp3 files. By using this library we can perform some activities such as playing the audio, split, merge, etc. So for that reason we are using the pydub library to trim part of our audio files. Here the point to be noted that pydub is not a inbuilt package of python, so we need to install it separately.

Trimming with pydub

To get started with trimming our audio file, first we need to follow with these procedures  :

  • Install the package
  • Give the audio file path
  • Give Starting point and ending point (this specifies at which point to start the trim and which point to end the trim)
  • Finally save it with name and format

Installing The package:

To install the package simply go to your terminal, And write this command

pip install pydub

   Python code for trimming audio :

import pydub
aud = pydub.AudioSegment.from_file("put your file location here with .mp3 format")
starting_time=3000 # to start trim from 3 seconds
ending_time=8000   # to end trim at 8 seconds
trimmed_audio=aud[starting_time:ending_time] #trimming the audio between 3 seconds to 8 seconds
trimmed_audio.export("trimmed_audio.mp3",format="mp3") #here in the place of "trimmed_audio" you can keep your filename to store it
print("Audio is trimmed and saved....")
 

Output:

Audio is trimmed and saved....

Summery of the code:

First install the package with pip command, give audio path to be trimmed. Give starting point and ending point and save it. You can take starting and ending time with input keyword to take runtime input from user

Code with user input:

import pydub
aud = pydub.AudioSegment.from_file("put your file location here with .mp3 format")
starting_time=int(input("enter starting time : ")) # to give starting trim 
ending_time=int(input("enter ending time : "))# to give ending trim 
trimmed_audio=aud[starting_time:ending_time] #trimming the audio between 3 seconds to 8 seconds
trimmed_audio.export("trimmed_audio.mp3",format="mp3") #here in the place of "trimmed_audio" you can keep your filename to store it
print("Audio is trimmed and saved....")

 

Output:

enter starting time : 3000
enter ending time:8000
Audio is trimmed and saved....

Conclusion:

Hence in this topic we have learned to trim the audio files using pydub. It is very efficient to trim audio files. It is simple to implement without downloading other software. we just have to provide file location, starting time and ending time. The trimmed file will store with other name. When you open it, You can hear the trimmed version of it. and don’t forget to install pydub package, Before starting the code. Hence you can trim the audio by this tutorial. For more information about Trimming The Audio File with python visit :HOW TO TRIM MP3 FILES USING PYTHON

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top