Skip to content

Instantly share code, notes, and snippets.

@lidopaglia
Created December 10, 2021 18:08
Show Gist options
  • Save lidopaglia/abf41600092fd170dae611a4219fe2b5 to your computer and use it in GitHub Desktop.
Save lidopaglia/abf41600092fd170dae611a4219fe2b5 to your computer and use it in GitHub Desktop.
Print duration of all movies with 'Christmas' in the title
#!/usr/bin/env bash
# use mediainfo to get total playtime of one or more video files
# and then calculate total playtime.
# e.g. mediainfo --Inform="Video;%Duration%" [inputfile]
err() {
echo "Error: $1"
exit 1
}
duration() {
# returns video file playtime in seconds (convert from ms).
# also accounting for some files that return decimal values.
ms=$(mediainfo --Inform="Video;%Duration%" "$1") || err "failed to read video"
awk -v v="$ms" 'BEGIN{printf "%d", v/1000}'
}
# check for mediainfo
if ! command -v "mediainfo" 1> /dev/null 2>&1; then
echo "mediainfo not found!" && exit
fi
movies=()
mapfile -t movies < <(find /srv/pve/zpool/movies -type f \( -name '*Christmas*.mp4' -o -name '*Christmas*.mkv' \))
IFS=""
total=0
for m in ${movies[@]}; do
sec=$(duration $m)
total=$(($total+$sec))
done
num_movies=${#movies[@]}
echo "Movies with 'Christmas' in the title"
echo "===================================="
echo "Total Movies: $num_movies"
echo "Total Seconds: $total"
printf 'Avg. Runtime: %.2f hrs\n' $(echo "scale=2; 510275 / 3600 / 97" | bc)
echo "----------------------"
printf '%d days, %d hours, %d minutes, %d seconds\n' \
$((total/86400)) \
$((total%86400/3600)) \
$((total%3600/60)) \
$((total%60))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment