Created
September 5, 2018 20:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple and yet useful script for calculating all video duration existing in a directory.