time-expanseR/split_5s.sh

73 lines
1.6 KiB
Bash
Raw Permalink Normal View History

2021-05-01 08:02:59 +02:00
#!/bin/bash
##########################################################
#
# Split wav sounds into 5s wav using ffmpeg
2021-05-01 10:40:48 +02:00
# Samuel ORTION
2021-05-01 08:02:59 +02:00
##########################################################
2021-05-01 10:40:48 +02:00
function usage {
printf "./$(basename $0) -h - - shows help \n"
printf "./$(basename $0) -i input_dir -o output_dir - - split long wav into 5s wav \n"
2021-05-01 10:40:48 +02:00
}
function split {
for item in `ls $indir/*.WAV`
2021-05-01 10:40:48 +02:00
do
audio_dur=`sox --i -D $item`
# Convert float to int
audio_dur=${audio_dur%.*}
echo "$item ($audio_dur)"
ss=0
to=5
for ss in `seq 0 5 $(( $audio_dur - 5 ))`
do
2021-05-01 10:40:48 +02:00
to=$(( $ss + 5 ))
echo "Spliting $item ($ss/$audio_dur)"
ffmpeg -ss $ss -i $indir/`basename $item` -t $to -c copy $outdir/`basename $item`-$ss.wav
2021-05-01 10:40:48 +02:00
done
done
}
optstring=":hi:o:"
# Defaults
indir="raw"
outdir="split"
while getopts ${optstring} arg
do
case "${arg}" in
h)
printf "$( basename $0 ) usage: \n"
2021-05-01 10:40:48 +02:00
usage
exit 0
2021-05-01 10:40:48 +02:00
;;
i)
indir="${OPTARG}"
echo "indir: $indir"
2021-05-01 10:40:48 +02:00
;;
o)
outdir="${OPTARG}"
echo "outdir: $outdir"
2021-05-01 10:40:48 +02:00
;;
:)
echo "$( basename $0 ): Must supply an argument to -$OPTARG."
2021-05-01 10:40:48 +02:00
exit 1
;;
?)
echo "Invalid option: -${arg}."
2021-05-01 10:40:48 +02:00
echo
usage
exit 1
2021-05-01 10:40:48 +02:00
;;
esac
done
2021-05-01 08:02:59 +02:00
if [ ! -d $outdir ];
then
mkdir $outdir
fi
2021-05-01 10:40:48 +02:00
split