Skip to content

Instantly share code, notes, and snippets.

@e3oroush
Created September 5, 2018 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e3oroush/4f7c267c66fbebb902fdfe0345c14f4e to your computer and use it in GitHub Desktop.
Save e3oroush/4f7c267c66fbebb902fdfe0345c14f4e to your computer and use it in GitHub Desktop.
A simple, and handy tool for calculating video duration of one file or directory of multiple video files
#
# Author: Ebi: ebrhim.soroush@gmail.com
# You have to install mediainfo and libxml-xpath-perl and source this file or just copy this script into your ~/.bashrc
# sudo apt install mediainfo libxml-xpath-perl
#
read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
};
getProperty()
{
videoFileName=$1
property=$2
mediainfo $videoFileName --Output=XML | while read_dom; do if [[ $ENTITY == $property ]]; then echo $CONTENT; break; fi; done;
};
round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};
calculateVideoDuration(){
videoPath=$1
durationSeconds=0;
while read_dom; do
if [[ $ENTITY == Duration ]]; then
durationSeconds=`echo $durationSeconds+$CONTENT|bc`
fi
done <<< `mediainfo $videoPath --Output=XML | xpath -e '//track[@type="Video"]/Duration' 2>/dev/null`
totalMinutes=$(round $durationSeconds/60 2)
driftSeconds=`a=$(echo $totalMinutes | cut -d"." -f2); echo $a*60/100|bc`
totalMinutes=`echo $totalMinutes | cut -d"." -f1`
echo total time is: $totalMinutes minutes and $driftSeconds seconds
};
@e3oroush
Copy link
Author

e3oroush commented Sep 5, 2018

This is a simple and yet useful script for calculating all video duration existing in a directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment