Let’s just say you have one big audio file which you want to extract parts of it into individual files. Typical usecase is youtube jukebox videos where you would get a song name and the start time of the song. After downloading the audio file, which is one big file, you can use the below bash script to extract the individual files:
Here we use avconv (ffmpeg can be used as well) to accomplish the task, if you haven’t installed it, you can install it using

sudo apt-get install libav-tools

Save the following file with any file name ending in .sh extension. Do a chmod+x to give it execute permissions, or can use bash scriptname.sh to run it.
Read the comments in the following code to do some minor changes that suits your needs.

#!/bin/bash
#This function calculates time diff in seconds
t2s()
{
  local T=$1;shift
  echo $((10#${T:0:2} * 3600 + 10#${T:3:2} * 60 + 10#${T:6:2})) 
}
 
#replace the following with file names that you need
names=('file1' 'file2'  )
#these are the start times of the audio, observe one extra element here, that is the total audio length
timelength=('00:00:00' '00:06:07' '00:13:02')
 
for (( i = 0 ; i < ${#names[@]} ; i=$i+1 ));
do
	start_time=${timelength[$i]}
	end_time=${timelength[$i+1]}
	diff_time=$(( $(t2s $end_time) - $(t2s $start_time) ))
	#replace input.mp3 with your audio file
	avconv -i input.mp3 -ss ${timelength[$i]} -t ${diff_time} -acodec copy ${names[$i]}.mp3
done
“You might not know it, but chances are you use ffmpeg in some or the other way everyday”
-Rushi

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>