Crop particular portion of an Mp3 file using Python
In this tutorial, we will learn how to crop particular portions of an Mp3 file using Python.
The Purpose
The main purpose of this tutorial is to teach you how to crop a particular portion of an Mp3 file using Python.
There are several different approaches to do so, and here I will demonstrate using the MoviePy Python library.
You will understand well with the help of code so let’s get started.
MoviePy Library
- Before running the code make sure to install the moviepy library.
- ‘
pip install moviepy
‘ command is used to install the MoviePy library in Python. - You can simply open the terminal and type the command and the installation will be done.
Installation purpose
- Purpose: installs the moviepy library into your Python envirnoment,
- Usage: It installs all the necessary files and dependencies required to use MoviePy for video editing and audio manipulation tasks.
Importing Libraries
from moviepy.editor import AudioFileClip
- AudioFileClip is used to load audio files like Mp3 into python for processing.
- The purpose of the line is to import the AudiFileClip class from the MoviePy library
Defining the trim_audio function
def trim_audio(input_path, output_path, start_time, end_time): audio = AudioFileClip(input_path) trimmed_audio = audio.subclip(start_time, end_time) trimmed_audio.write_audiofile(output_path, codec="mp3")
Purpose: Defines a function ‘trim_audio’ that trims a section of an audio file and saves it as an Mp3 file.
Parameters:
- input_path: Path to the input Mp3 file that you want to trim.
- output_path: Path where the trimmed mp3 file will be saved.
- start_time: Start time of the clip you want to keep.
- end_time: End time of the clip you want to keep.
Functionality:
- AudioFileClip(input_path): Loads the audio file specified by ‘
input_path
‘ into the ‘audio
‘ object using MoviePy’s ‘AudioFileClip
‘ class. - audio.subclip(start_time, end_time): Extracts a subclip from
'start_time'
to ‘end_time
‘ from the loaded audio file, creating ‘trimmed_audio
‘. - trimmed_audio.write_audiofile(output_path, codec=”mp3″): Writes the trimmed audio (‘
trimmed_audio
‘) to the file specified by ‘output_path
‘ in Mp3 format (‘ codec=”mp3″ ‘).
Example Usage
input_file = "C:/Users/bindu/OneDrive/Documents/souraa.mp3" output_file = "C:/Users/bindu/OneDrive/Documents/trim.mp3" start_time = 80 # Start trimming from 80 seconds end_time = 100 # End trimming at 100 seconds trim_audio(input_file, output_file, start_time, end_time)
Purpose: Demonstrates how to use the ‘trim_audio’ function with specific parameters.
Example parameters:
input_file = “C:/Users/bindu/OneDrive/Documents/souraa.mp3”———->specifies the path to input Mp3 file.
output_file = “C:/Users/bindu/OneDrive/Documents/trim.mp3”————>specifies the path to where to save the clip/ trimmed mp3 file.
start_time = 80 ———> this indicates the starting time of the trim operation that is 80seconds.
end_time = 100 ———-> this indicates the ending time of the trim operation that is 100 seconds.
Once the code is executed successfully you can see the cropped file in the directory/path you mentioned.
Also read: Cut or trim a video using moviepy in Python
Here is the full Python code and output
Code
from moviepy.editor import AudioFileClip def trim_audio(input_path, output_path, start_time, end_time): audio = AudioFileClip(input_path) trimmed_audio = audio.subclip(start_time, end_time) trimmed_audio.write_audiofile(output_path, codec="mp3") input_file = "C:/Users/bindu/OneDrive/Documents/souraa.mp3" output_file = "C:/Users/bindu/OneDrive/Documents/trim.mp3" start_time = 80 # Start trimming from 80 seconds end_time = 100 # End trimming at 100 seconds trim_audio(input_file, output_file, start_time, end_time)
Output
PS C:\Users\bindu\OneDrive\Documents> c:; cd 'c:\Users\bindu\OneDrive\Documents'; & 'c:\Users\bindu\anaconda3\python.exe' 'c:\Users\bindu\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '64653' '--' 'c:\Users\bindu\OneDrive\Documents\from moviepy.py' MoviePy - Writing audio in C:/Users/bindu/OneDrive/Documents/trim.mp3 MoviePy - Done. PS C:\Users\bindu\OneDrive\Documents>
Leave a Reply