Here is a Bourne Shell script that I use to automatically cut the first and the last 15 seconds of mp3 files. Indeed I download mp3 podcast which include advertising at the beginning and at the end of the file.
#! /bin/bash
# auto-mp3-cut.sh version 1.0
# 2018.10.27 shebangthedolphins.net first version
#---------------------------------------------------
# this script automatically cut mp3 audio files
# needs mediainfo to work
#---------------------------------------------------
## If no arguments found
option_found=0
#Functions
FunctionDuration()
{
duration_source=$(mediainfo "$1" | grep Duration | head -n 1 | cut -d ":" -f2 | sed 's/[a-z]//g' | tr -s ' ' | sed -e 's/^\s//' -e 's/\s$//' -e 's/\s/:/g') #get the duration thanks to mediainfo command
duration_temp=$(date -d "1983-12-12 00:$duration_source" +%s) #create a fake date, here only $duration_source is important
duration_diff=$(( (duration_temp - 15) ))
duration_dest=$(date -d"@$duration_diff" "+%M:%S") #calculate the new time thanks to date command
}
usage()
{
echo "usage: ./auto-mp3-cut.sh -d [DIRECTORY_TO_WORK]"
echo "ex : ./auto-mp3-cut.sh -d /tmp/mp3/"
echo ""
exit 3
}
while getopts d: OPTNAME; do
case "$OPTNAME" in
d)
ROOT="$OPTARG"
option_found=1
;;
*)
usage
;;
esac
done
if [ "$option_found" -eq "0" ] || [ -d "ROOT" ] ; then
usage
fi
FOLDER=$(echo "$ROOT" | sed 's/\/$//') #remove "/" if present at the end of the ROOT variable
cd "$ROOT" #go to the destination folder
rc=$?; if [[ $rc != 0 ]]; then echo "ERROR : directory doesn't exist"; exit $rc; fi #exit if directory is not found
for i in *3; do #for each mp3 file
i="${i%.[mM][Pp]3}" #we remove .mp3 from the file name
ffmpeg -y -ss 15 -i "$i".[mM][Pp]3 -acodec copy -async 1 "$i"_NEW.mp3 #we cut 15 s from the beginning and create temporary NEW files
FunctionDuration "$i"_NEW.mp3 #We call FunctionDuration funtion to get the mm:ss information
ffmpeg -y -t "$duration_dest" -i "$i"_NEW.mp3 -acodec copy -async 1 "$i".mp3 #we remove 15 s from the end and replace the original file
rm "$i"_NEW.mp3; rm "$i".MP3 #we remove the temporary NEW files
done
Contact :