Split audio files using Python

Read an audio file / Split audio files using Python

In this blog, you will learn, how to read an audio file, and how to split audio files using python.

Audio data analysis is about handling and analyzing audio data and generate insight into the form of digital signal processing. For example cases like voice sentiment analysis, Audio classification, music classification, Speech recognition, etc require extensive audio data analysis. In these cases, you have to handle a large number of audio files to analyze data.

Audio files can be handled using the below libraries.

Install Libraries:

Install Pydub using pip:

pip3 install pydub

Install Pydub in Jupiter notebook:

!pip install pydub

Install Librosa using pip:

pip3 install librosa

Install Librosa in Jupiter notebook:

!pip install librosa
Read  an Audio file using librosa library:

Use the below code to read an audio file.

import librosa
import librosa.display
data, sampling_rate = librosa.load("Sample Speech/audio 1.wav");
plt.figure(figsize=(12, 4))

sampling_rate: Sampling rate is the number of samples per second.

Upon plotting data, the graph of the audio file looks like below.

Split audio files using Python

Read an Audio file using  pydub library:
from pydub import AudioSegment
sound_file = AudioSegment.from_wav("rec1.wav")

To verify the audio file.

# Embed audio files in the notebook
import IPython.display as ipd

Split the audio file / Split on silence :

Now let’s try to split audio files using python. To split audio, you can specify the minimum duration of silence between each word to consider as silence. This parameter passed using the argument ‘min_silence_len’.

min_silence_len = 500, which means the minimum duration of silence must be a half-second.

Also, you can use ‘decibel‘ as a parameter to specify the silence. If the sound is quieter than certain dBFS values, then consider silence. This parameter passed using the argument ‘silence_thresh’.

from pydub import AudioSegment
from pydub.silence import split_on_silence

sound_file = AudioSegment.from_wav("audio 1.wav")
audio_chunks = split_on_silence(sound_file, min_silence_len=500, silence_thresh=-40 )

for i, chunk in enumerate(audio_chunks):
   out_file = "chunk{0}.wav".format(i)
   print("exporting", out_file)
   chunk.export(out_file, format="wav")

In this blog, you learned how to split audio files using Python. Browse other tutorials for topics related to analytics. Please share your thoughts below. Happy Learning.

3 thoughts on “Split audio files using Python

Leave a Reply