04/02/2022

Splitting Audio files by detecting silence using FFMPEG

By snorlaxprime

Recently I downloaded a soothing music from youtube and it was a long audio loop. So I wanted to split it up to individual mp3 files so that I can have it as individual files.

I am also not sure why people wanted to listen to song in Youtube, but that is probably where you can get all the songs and great playlist for free right? The annoying part is playing the song in youtube will drain your device’s battery and also the screen needs to be on, you can enjoy the music, while you are having a run or just want to turn off the screen. So here is an alternative way to do so. First by downloading the song.

Here are the steps that I have gone through to split the mp3 file into smaller files.

Step 1. Detect the silence in the big file

Use the following script to detect the silence and generate a list

ffmpeg -i Long_Audio_File.mp3 -af silencedetect=d=0.1 -f null - |& awk '/silence_end/ {print $4,$5}' | awk '{S=$2;printf "%d:%02d:%02d\n",S/(60*60),S%(60*60)/60,S%60}'

The parameter d=0.1 determine how long the silence is 0.1 seconds, you can adjust this as necessary if it doesn’t produce the correct split. The above command will generate a long list of the silence as shown below:

you will need to copy the list into a separate file, lets call this “timechunk” and remove the first line where it shows 0:00:00, so that the first line start with 0:03:05 in my example above.

Step 2. Create the splitter script

Use the command prompt or copy and paste the following script using your favourite editor

#! /bin/bash
x="00:00:00"
z=0
filename=$(basename -- "$2")
ext="${filename##*.}"
filename="${filename%.*}"
initcmd="ffmpeg  -nostdin -hide_banner -loglevel error -i $2"
while read y
do
initcmd+=" -ss $x -to $y -c copy $filename$z.$ext"
let "z=z+1"
x=$y
done < $1
$initcmd

call the above script “audiosplitter.sh” for example, then change the permission using the following command

chmod +x audiosplitter.sh

Some explanation for the above script if you are interested:

Line 2: set the initial start time

Line 3: set output file name suffix

Line 4, 5 and 6; set the filename and extension

Line 7: the command line parameter for ffmpeg, the -nostdin stops ffmpeg from eating the first character -hide_banner -loglevel error supressed the ffmpeg messages -i set the input file name to parameter no 2

Line 8: while loop by reading the file

Line 10: initcmd+=: adds output name and parameter -ss start time -to end time -c copy use same codec as input, so it is fast processing the file

Line 11: increment the file number suffix

Line 12: set the new starting point

Step 3. Run the script

Now execute the script using the following command

./audiosplitter.sh ./timechunk ./Long_Audio_File.mp3

If all goes well you should see the files get split according to the timechunk file. You can watch the following video.

I hope this is useful and saving you time to do something else. Please subscribe or comment if you like it. Or you can buy me a coffee.