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

It begins

I am the senior network and system administrator at a national medium/large retail company in Australia.

I spend most of my day researching, developing and designing solutions to all kind of IT requirements and problems and this blog is intended to allow me to share some of those ideas.

We are primarily a Linux house but also deal with many other products like Microsoft, Cisco, 3Com, APC, etc.

I plan to publish solutions and code in the languages I work with daily.  Like shell, perl, puppet, python, ruby and many more.