Saturday, August 10, 2013

Converting MP4 videos to audio CD (wav) on Ubuntu

My little one loves his nursery rhymes. At home he gets to listen to them on the ipad or on the phone, but this unnecessarily drains the battery out. Plus when we travel by car, i would rather use the car stereo than play it loudly on my phone. So i decided to convert the MP4 videos to an audio CD (unfortunately my care stereo does not support mp3 :-( ).

Here are the steps i followed:

1. After doing a bit of research MPlayer seemed to be the best option suited for my needs. The latest version is 1.1.1. Since my Ubuntu release is quite old (I use 10.10), apt-get returned no matches for mplayer. So i downloaded the source from the site and installed it. This process is rather simple:
  a. cd to the directory where you extracted the mplayer tar
  b. ./configure (add the --enable-gui switch to the command if you wish to use the player in GUI mode)
  c. make (this took forever)
  d. sudo make install

Note: during compilation, i discovered that the yasm library was missing. I had to install yasm 1.2.0 (the latest version) to resolve this.

2. The conversion process itself is quite simple. This simple invocation does the trick.
    >> mplayer -ao pcm:file=inputfile.wav outputfile.mp4

3.  I had over 28 files to convert, so doing this one by one was not an option. A bash for loop took care of this:
>> for i in `ls`
  > do  mplayer -ao pcm:file=${i//.mp4/.wav} $i
  > done

line 1: we loop over the output of the `ls` command. Note the use of backticks around the ls command, this tells the shell to execute the command and present its output to the for function.

line 2: This is the mplayer function to do the conversion with a bit of bash magic to rename the converted file. Say the original file is called foo.mp4, now i want the converted file to be called foo.wav. To make this happen, I use substring replacement. The syntax is as follows: 
 >> ${string//substring/replacement}
In my case, I am replacing the extension .mp4 with .wav

line 3: close the for loop

4. The conversion process is very CPU intensive (as per top, cpu was 75%). Unfortunately, my laptop is quite old and is single CPU. This meant i couldn't do anything GUI based while this was going on. Thankfully the conversion is rather quick, only a few minutes per video.

In some cases, for causes unknown, the conversion process would get stuck despite having successfully converted the video. I simply killed that iteration and my loop continued with the next file.

With the major work done, now i just have to burn a CD and enjoy the music!!

No comments: