Skip to content

Instantly share code, notes, and snippets.

@ed-cooper
Created December 28, 2019 06:41
Show Gist options
  • Save ed-cooper/02f0c87848789e606d87e0ad8eb3563d to your computer and use it in GitHub Desktop.
Save ed-cooper/02f0c87848789e606d87e0ad8eb3563d to your computer and use it in GitHub Desktop.
Gets the recursive total duration of all the audio and video files in a directory
#!/usr/bin/env bash
#
# Total Video Runtime
# Gets the recursive total duration of all the audio and video files in a directory
#
# Requires ffmpeg and bash 4.0
#
# Usage:
# ./total-video-runtime ~/my_dir/
echo "Getting total video / audio duration for files in $1"
shopt -s globstar nullglob dotglob
total=0;
for f in $1**/*
do
duration=`ffmpeg -i "$f" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }'`
if (( duration > 0 ))
then
echo "$f -> $duration seconds"
fi
total="$((total+duration))"
done
echo "Total duration:"
echo "$total seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment