Thursday, December 16, 2010

Get the average length of your OGG audio library

This bash script gets the average length of all Vorbis ogg audio files within a directory recursively.

#!/bin/bash

n=0
sum=0
tmpf=$(mktemp)
tmps=$(mktemp)

cd $1

echo "INFO: Getting length from music tracks."
find -type f -iname "*.ogg" | while read file
do 
  ogginfo -v "$file" 2>/dev/null | awk '/Playback length/ {print $3}' >> $tmpf
done

echo "INFO: Calculating length into seconds."
while read time
do
  min=${time%:*}
  min=${min%m*}
  sec=${time#*:}
  sec=${sec%.*}

  echo $(expr $min \* 60 + $sec)
done < $tmpf >> $tmps

awk '{ sum += $1 } END { printf "Average length: %d seconds (%.2f min)\n", sum/NR,sum/NR/60 }' $tmps

rm -f $tmpf $tmps

No comments:

Post a Comment