Split audio files using silence detection in Python

In this tutorial, we are going to see how to Split audio files using silence detection in Python. We are going to split audio files using silence detection in python.  It’s easy and simple let’s see how it works.

For this, we are using a library available in python for audio file handling that is pydub. This library is used to work with audio files. By using this pydub we can play, cut, merge,  split or edit Audio files. You can check it also: How to cut a particular portion of an MP3 file in Python

Proceeding towards our task we need to install the ‘pydub’ library to our system. We can do this using the pip command as shown below in your terminal or shell. If you already installed it that’s well good you can directly use it.

pip install pydub

After executing the above command pydub will be installed in your machine. In the next code, we are importing this library and its required functions.

Splitting audio files into chunks in Python

#Importing library and thir function
from pydub import AudioSegment
from pydub.silence import split_on_silence

#reading from audio mp3 file
sound = AudioSegment.from_mp3("/content/Audio/song_with_silence.mp3")

# spliting audio files
audio_chunks = split_on_silence(sound, min_silence_len=500, silence_thresh=-40 )

#loop is used to iterate over the output list
for i, chunk in enumerate(audio_chunks):
   output_file = "/content/Audio/output/chunk{0}.mp3".format(i)
   print("Exporting file", output_file)
   chunk.export(output_file, format="mp3")

# chunk files saved as Output


Exporting file /content/Audio/output/chunk0.mp3 
Exporting file /content/Audio/output/chunk1.mp3 
Exporting file /content/Audio/output/chunk2.mp3

As shown in the above code after importing AudioSegment from pydub. There are special functions defined in pydub for handling silence so we importing split_on_silence function from silence. Next, we are giving our audio input file which contains silence also in it.  We are storing it in the ‘sound’ variable which reads that audio file using AudioSegment. Audio file song_with_silence, we are giving it with the path.

Now we have defined the audio_chunk variable and by using the split_on_silence function we are splitting the audio file. This function takes sound as a parameter which is our audio file next it takes min_silence_len by default it is 1000. The minimum length for silent sections is in milliseconds. If it is greater than the length of the audio segment an empty list will be returned. Here we are giving it as 500 milliseconds and silence_thresh by default it is -16. It is the upper bound for how quiet is silent in dBFS. We are giving it as -40. This function returns a list of audio segments.

For iterating over that list and saving our audio files we are going to use for loop over here. We are creating chunks of an audio file and storing output audio files into it. The format of the file is mp3. For exporting that audio chunk we are using the export function of pydub. So in output, we get audio chunks that contain audio without silence.

Leave a Reply

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