did this function to generate the duration of all the video files in a folder or multiple folders, it works fine for my use (I am no Guru as you may have noticed) but when I give it a lot of folders the calculation get a bit borked.
If any good soul had the energy to look at it and give pointers I will be grateful, but you really don't have to
vidlength ()
{
back=$(pwd)
folder_length=
total_folder_length=
array=()
# Look at the root of the folder
# https://stackoverflow.com/a/23357277 How can I store find command result as arrays in Bash
# $REPLY is the default variable when 'read' is not given one
while IFS= read -r -d $'\0'; do
array+=("$REPLY")
done < <(find . -maxdepth 1 \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \) -print0)
# exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}' GIVES TIME AS 78682.8
# exiftool -n -q -p '${Duration;our $sum;$_=ConvertDuration($sum+=$_)}' "${array[@]}" 2> /dev/null| tail -n1 GIVES TIME AS 0:21:51:22
# This command gives a result of '159' if the array is empty (2 minutes 39 seconds)
total_folder_length=$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')
# I have to add this because for wathever reason the last command fills len with the number 159 when it finds no video in the folder or when the array is empty
if [[ "${total_folder_length%.*}" -eq 159 ]]; then
total_folder_length=0
fi
counter=$(echo ${#array[@]})
for folder in ./*; do
#find . -maxdepth 1 \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \)
if [ -d "$folder" ]; then
array=() # Empty the array between loops
while IFS= read -r -d $'\0'; do
array+=("$REPLY")
done < <(find "$folder" \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \) -print0)
# https://unix.stackexchange.com/a/170973 Way faster with exiftool
# I have to add this because for wathever reason the last command fills len with the number 159 when it finds no video in the folder or when the array is empty
folder_length=$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')
if [[ "${folder_length%.*}" -eq 159 ]]; then
#len=$(echo "$len-159+$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')" | bc)
continue
else
total_folder_length=$(echo "$total_folder_length+$(exiftool -n -q -p '$Duration#' "${array[@]}" 2> /dev/null | awk '{sum += $0}; END{print sum}')" | bc)
fi
counter=$(echo $(echo ${#array[@]})+$counter | bc)
fi
done
# Change it from 78682.8 to 0:21:51:22 https://unix.stackexchange.com/a/34033
total=$(echo $total_folder_length | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}')
days=$(echo ${total} | awk -F ":" '{print $1}')
hours=$(echo ${total} | awk -F ":" '{print $2}')
minutes=$(echo ${total} | awk -F ":" '{print $3}')
seconds=$(echo ${total} | awk -F ":" '{print $4}')
if [[ "${days#0}" -gt 0 ]]; then
#printf "The total duration found is:\n"
printf "%d day(s) %02d hour(s) %02d minute(s) and %02d second(s) in %d videos\n" "${days#0}" "${hours#0}" "${minutes#0}" "${seconds#0}" "$counter"
else
if [[ "${hours#0}" -gt 0 ]]; then
#printf "The total duration found is:\n"
printf "%02d hour(s) %02d minute(s) and %02d second(s) in %d videos\n" "${hours#0}" "${minutes#0}" "${seconds#0}" "$counter"
else
#printf "The total duration found is:\n"
printf "%02d minute(s) and %02d second(s) in %d videos\n" "${minutes#0}" "${seconds#0}" "$counter"
fi
fi
cd "$back"
}