What I'm trying to achieve is the difference between chapter 1 start time and chapter 2 start time and so on subtracting each chapter start time from the next in the array e.g. 00:05:57 - 00:01:03 = 00:04:54
Starting with ghostdog74's example (which of course works perfectly) I have thus far been unable to make this work to subtract chapter start/end times. Perhaps someone can spot the reason(s)?
Finally it is working:
ChapStart is an array with start times in this format: 00:00:00 which gets converted to "Seconds since 1970-01-01"with 'date'. This allows us to do the math. Then, we convert it back to 00:00:00 format.
Code:
#~ Time conversion and calculations
for ((s=0; s<=$CHAPTERS; s++)) ## Convert ChapStart 00:00:00 to ChapTimeStart seconds since 1970-01-01
do ChapTimeStart[$s]=$(date +%s -d "${ChapStart[$s]}")
done
for ((e=0; e<$CHAPTERS; e++)) ## Convert ChapEnd 00:00:00 to ChapTimeEnd seconds since 1970-01-01
do ChapTimeEnd[$e]=$(date +%s -d "${ChapEnd[$e]}")
done
unset ChapTimeEnd[0] ##Remove the first chap start time 00:00:00
ChapTimeEnd[$CHAPTERS]=$Disc ## Add $Disc as last array element
ChapTimeEnd=( "${ChapTimeEnd[@]}" ) ## Pack the array
unset ChapEnd[0] ## Offset the time differences - this is printed to screen as "End: $"
ChapEnd[$CHAPTERS]=$DiscTime ## Add $DiscTime as last array element
ChapEnd=( "${ChapEnd[@]}" ) ## Pack the array
for ((t=0; t<$CHAPTERS; t++))
do TimeDiff[$t]=$((${ChapTimeEnd[$t]} - ${ChapTimeStart[$t]}))
Duration=$(echo - |awk '{printf "%02d:%02d:%02d","'"${TimeDiff[$t]}"'"/(60*60),"'"${TimeDiff[$t]}"'"%(60*60)/60,"'"${TimeDiff[$t]}"'"%60}';echo)
echo "Chapter: `printf "%02d" $(($t+1))` Start: "${ChapStart[$t]}" End: "${ChapEnd[$t]}" Duration: $Duration"
done