Skip to content

Instantly share code, notes, and snippets.

@javascripto
Created September 4, 2023 01:07
Show Gist options
  • Select an option

  • Save javascripto/53d474b70f6c82d141450a7e0008447c to your computer and use it in GitHub Desktop.

Select an option

Save javascripto/53d474b70f6c82d141450a7e0008447c to your computer and use it in GitHub Desktop.
A simple shell script to calculate duration of some videos in the current directory with *.MP4 extension
#!/usr/bin/env zsh
pad_zero() {
local NUMBER=$1
if [[ $NUMBER -lt 10 ]]; then echo 0$NUMBER; else echo $NUMBER; fi
}
convert_seconds_to_hours() {
local SECONDS=$1
HOURS=$(expr $SECONDS / 3600)
MINUTES=$(expr $SECONDS % 3600 / 60)
SECONDS=$(expr $SECONDS % 3600 % 60)
echo "$(pad_zero $HOURS):$(pad_zero $MINUTES):$(pad_zero $SECONDS)"
}
calculate_MP4_seconds() {
local TOTAL_SUM=0
for file in *.MP4; do
SECONDS=$(ffmpeg -i $file 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] }')
TOTAL_SUM=$(expr $TOTAL_SUM + $SECONDS)
done
convert_seconds_to_hours $TOTAL_SUM
}
calculate_MP4_seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment