SoFunction
Updated on 2024-12-13

Python to cut MP3 files easily

application scenario

1. Large MP3 files need to be cut into smaller parts for uploading or sending.

2. Need to extract specific audio clips from MP3 files for other purposes.

3. Need to quickly create ringtones or music clips for use on devices such as cell phones.

source code (computing)

import subprocess
import wx
 
class MyFrame():
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(400, 600))
        panel = (self)
 
        vbox = ()
 
        hbox1 = ()
        start_label = (panel, label='Start time (seconds):')
        (start_label, flag=wx.ALIGN_CENTER_VERTICAL | , border=5)
        self.start_input = (panel)
        (self.start_input, flag=, border=5)
        (hbox1, flag= | , border=10)
 
        hbox2 = ()
        end_label = (panel, label='End time (seconds):')
        (end_label, flag=wx.ALIGN_CENTER_VERTICAL | , border=5)
        self.end_input = (panel)
        (self.end_input, flag=, border=5)
        (hbox2, flag= | , border=10)
 
        hbox3 = ()
        btn_browse = (panel, label='Select file', size=(100, 30))
        btn_browse.Bind(wx.EVT_BUTTON, self.on_browse)
        (btn_browse, flag=, border=5)
        (hbox3, flag=wx.ALIGN_CENTER | , border=10)
 
        hbox4 = ()
        name_label = (panel, label='Song Title:')
        (name_label, flag=wx.ALIGN_CENTER_VERTICAL | , border=5)
        self.name_input = (panel)
        (self.name_input, flag=, border=5)
        (hbox4, flag= | , border=10)
 
        hbox5 = ()
        btn_cut = (panel, label='Cutting', size=(100, 30))
        btn_cut.Bind(wx.EVT_BUTTON, self.on_cut)
        (btn_cut, flag=, border=5)
        (hbox5, flag=wx.ALIGN_CENTER | , border=10)
 
        (vbox)
        ()
 
    def on_browse(self, event):
        dlg = (self, "Select MP3 file", wildcard="MP3file (*.mp3)|*.mp3", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        if () == wx.ID_OK:
            selected_file = ()
            self.input_file = selected_file
        ()
 
    def on_cut(self, event):
        start_time = self.start_input.GetValue()
        end_time = self.end_input.GetValue()
        name = self.name_input.GetValue()
 
        if not start_time or not end_time or not name or not hasattr(self, 'input_file'):
            ('Please enter a valid start time, end time, song title, and select the MP3 file to cut!', 'Error',  | wx.ICON_ERROR)
            return
        output_file = f'{name}.mp3'
 
        # Cutting audio files with FFmpeg
        cmd = f'D://ffmpeg//bin//ffmpeg -i {self.input_file} -ss {start_time} -to {end_time} -c copy {output_file}'
        print(cmd)
        try:
            (cmd, shell=True)
            ('Cutting successful!', 'Hints',  | wx.ICON_INFORMATION)
        except :
            ('Cutting failed, please check if the time entered is correct!', 'Error',  | wx.ICON_ERROR)
 
app = ()
MyFrame(None, title='MP3 Cutting Tool')
()
 

source code interpretation

The core logic of this tool is to use the FFmpeg library for audio processing and the wxPython library to build the GUI. specifically, the FFmpeg library provides powerful audio processing capabilities to easily extract, clip, or transcode audio from audio files, while the wxPython library provides easy-to-use GUI elements and layout managers to help users create beautiful and easy-to-use GUIs.

Note that this widget uses the FFmpeg library, so before using it you need to make sure that you have installed the FFmpeg library and added it to your system's environment variables. At the same time, this gadget also only supports cutting MP3 files, if you need to deal with other types of audio files, you need to make the appropriate changes.

The effect is as follows

Users can follow the steps below to use this gadget:

Run the code and open the GUI interface.

Select the MP3 file you want to cut by clicking the "Select File" button.

Enter the start time and end time, and the name of the file to be output.

Click the "Cut" button and wait for the program to finish processing.

Find the cut MP3 file in the output folder.

to this article on the Python implementation of easy to cut MP3 files to this article, more related to Python cut MP3 file content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!