Extracting audio length using Rails

In: Ruby on Rails

14 Jan 2010

Was working on a module in one of my Rails projects and I had to find the length of the audio file clients uploaded to that they can be billed accordingly, since this was voice sms it was very crucial. I combed the whole internet for solutions and could not find any, came across a few gems that were great but lacked what I really needed, so I decided to sit my butt down and role out the code myself.

The first idea was using FFMPEG on the command line to extract the length, did some few searches and came across the exact line I needed. You need to install ffmpeg first before this will work. I code on Mac OS X, my personal production server at home is RedHat Enterprise 5 and the main production server is running Ubuntu Linux. so I have the luxury of learning all three install methods.

For mac you need macport installed then you run

sudo port install ffmpeg

On RHEL5 i had a lot of dependency problems but resolved them by adding the ATRPMS Repo (Really a saved me), how to add the repo to you Linux distribution is available on thier website. I wont cover it here, once you done, you can install ffmpeg with

sudo yum install ffmpeg

On Ubuntu Linux ( Debian Systems), you install ffmpeg using

sudo apt-get install ffmpeg

All said and done lets try a commandline demo and see it it really works.

ffmpeg -i “/Users/nukturnal/Desktop/try.mp3″ 2>&1 | grep ‘Duration’ | cut -d ‘ ‘ -f 4 | sed s/,//
we get something close to this resultĀ 00:03:45.44
Now the Ruby Code
Yep thats all folks nice and hardcore.

Comment Form

About this blog

Just tackling programming nightmares with flares. Sometimes the tactics are smooth sometimes their rough.

Photostream

About Me

1Alfred is a seasoned programmer with over 8 years of experience, CEO of nCodedev Labs, a Ruby evangelist and activist. Loves programming most of the time, spends my free times with his family or trying out something new. He believes in doing more than expected of you and working hard at anything you find your self working on. As he always says, you don't have to take the bull by its horns all the time, you can take it by the tail too and move on to the horns when you stronger.