Split Large MP3 File into Multiple Files with Python

Introduction

Are you a music enthusiast looking to create a small playlist, sample collection, or just want to split your long audio files into smaller, manageable segments? Many audio editing software can be daunting, and some come at a hefty price. Fret not; this article will introduce you to an effective and straightforward solution using Python – a versatile programming language widely popular for its simplicity and readability. By following this guide, you will be able to split an MP3 file into multiple segments with ease.

If you want to skip the article and run the Python code, then download it from here.

System setup

To split the mp3 audio file we will use Google Colab and the pydub Python library that is quite handy for audio processing. You can also run this Python code locally on your system.

Install Pydub Library

Go to Colab and install the Pydub library first.

! pip install pydub

The version used in this tutorial is pydub-0.25.1.

Import Python Modules

The next step is to import the modules that we will use to split the audio data into multiple mp3 files.

from pydub import AudioSegment
import os
import math

Reading audio files and splitting into chunks

Let’s try to understand the main Python code that will load the large mp3 file and split it into equally sized audio chunks and save them as multiple mp3 files.

The Python function below accepts three inputs –

  • input_file: path to the input mp3 audio file
  • output_folder: path to the directory where the outputmp3 files will be saved
  • duration: duration of each chunk of audio data after splitting the audio of the input mp3 file

The values for all these inputs will be provided by the user while calling the function.

def split_audio(input_file, output_folder, duration):
    audio = AudioSegment.from_mp3(input_file)
    total_length = len(audio)
    num_parts = math.ceil(total_length / (duration * 1000))

    for i in range(num_parts):
        start = i * duration * 1000
        end = (i + 1) * duration * 1000
        split_audio = audio[start:end]
        output_path = os.path.join(output_folder, f"part_{i+1}.mp3")
        split_audio.export(output_path, format="mp3")
        print(f"Exported {output_path}")

Loading MP3 audio

Inside the function split_audio( ), we are using pydub’s AudioSegment module to load the audio data from the input mp3 file. AudioSegment provides methods for common audio operations, allowing you to modify and manipulate the audio data.

Find count of audio chunks

 total_length = len(audio)
 num_parts = math.ceil(total_length / (duration * 1000))

Then we are finding a the number of parts or chunks to split the audio data into. It can be easily done with the help of ceil function of the math library and the duration input that has been provided by the user.

Splitting audio data into parts of equal size

for i in range(num_parts):
    start = i * duration * 1000
    end = (i + 1) * duration * 1000
    split_audio = audio[start:end]
    output_path = os.path.join(output_folder, f"part_{i+1}.mp3")
    split_audio.export(output_path, format="mp3")
    print(f"Exported {output_path}")

In the for-loop above, the main audio data gets divided into a certain number of audio chunks of more or less same size. Then each chunk is save as an mp3 file at the output path specified by the user.

Perform Audio Splitting

To use the function above and split a large mp3 file into multiple smaller mp3 files, we can use the code below:

input_file = "audio.mp3"  # Replace with your input mp3 file
output_folder = "/output"  # Output folder for the split audio files
duration = 300  # Duration in seconds for each split audio file

split_audio(input_file, output_folder, duration)

We have an mp3 file, ‘audio.mp3’, that will be passed to the function as the input. Apart from that we are also passing the output folder path and the duration (in seconds) to the function split_audio( ).

Download the entire code from here.

End Notes

As you can see, splitting MP3 files into smaller segments doesn’t have to be a daunting task reserved for audio professionals or those with extensive technical knowledge.

By leveraging the power and simplicity of Python, you can now easily create your own custom playlists, sample collections, or manage extended audio files with confidence and ease.

This guide has provided you with a clear and straightforward solution to help you navigate your audio editing journey without the need for expensive or complicated software.

Leave a Reply

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