Skip to content

Instantly share code, notes, and snippets.

@mohsin
Created August 2, 2017 18:01
Show Gist options
  • Select an option

  • Save mohsin/b11c37cfce037797f57462a438b2a6ff to your computer and use it in GitHub Desktop.

Select an option

Save mohsin/b11c37cfce037797f57462a438b2a6ff to your computer and use it in GitHub Desktop.
Bash script to find the total duration of videos in a folder
ext=".mp4"
ffmpeg_location="/usr/local/Cellar/ffmpeg/3.1.2/bin/ffmpeg"
for file in *$ext
do
duration=$("$ffmpeg_location" -i "${file}" 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//)
hours=10#$(echo "$duration" | cut -d":" -f1)
minutes=10#$(echo "$duration" | cut -d":" -f2)
seconds=10#$(echo "$duration" | cut -d":" -f3 | cut -d"." -f1)
millis=10#$(echo "$duration" | cut -d":" -f3 | cut -d"." -f2)
((totalMillis=millis+seconds*1000+minutes*60000+hours*3600000))
((sum=sum+totalMillis))
done
let avSeconds=$sum/1000
let avMillis=$sum-$avSeconds*1000;
let avMinutes=$avSeconds/60;
let avSeconds=$avSeconds-$avMinutes*60;
let avHours=$avMinutes/60;
let avMinutes=$avMinutes-$avHours*60;
str=""
if [[ $avHours -gt 0 ]]; then
str="$avHours hours, "
fi
if [[ $avMinutes -gt 0 ]]; then
str="${str} ${avMinutes} minutes"
fi
if [[ $avSeconds -gt 0 ]]; then
str="${str} and ${avSeconds}.${avMillis} seconds"
fi
echo "$str"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment