Nav links

Thursday, 9 February 2006

Convert RealAudio to MP3

Download realToMp3.py


RealAudio streams are not very easy to convert to mp3 files. There are some shareware tools that claim to perform the task, but with a little bit of effort you can set up a really simple system yourself for free. Here I shall document how to set up a really easy to use converter for Windows.


The end result is this lovely simple dialog for interactive downloading, or you could set up a scheduled task to grab a RealAudio stream and save it as an MP3 with no user input at all.


There is a price to pay for getting all this for free, and that is that there is no simple one-step installer. Instead, we are going to set up a variety of free tools, and tie them all together with some scripting. Here's the menu:

  1. Retrieve stream with MPlayer
  2. Convert to MP3 with LAME
  3. Automate the previous steps using Python
  4. Add a nice interface with Windows Scripting Host



Part 1: Retrieve stream with MPlayer


MPlayer is a highly regarded Open Source audio and video player. Once you've downloaded and installed it you can test that it's working from the good old command prompt:


mplayer -cache 320 -ao pcm:waveheader:file=stream.wav rtsp://some.stream.ra

Note that you need to pass a .ra or .rm stream. If you have a .ram stream instead, then save that as a local file and have a look in it with a text editor such as notepad to see the actual stream name.


If this does not work then take careful note of the error messages being emitted. This was the point at which I learnt that I needed to download some RealAudio CODECS from the MPlayer site, which were then unzipped into MPlayer's CODEC's folder.


Once you've got this going you'll see the one unavoidable annoyance, that you can't download any faster than the normal stream listening time. Anyway, the result of this is that you have converted a RealAudio stream to a probably rather large .wav file. Now we need to shrink that to a reasonably-sized MP3 file.


Part 2: Convert to MP3 with LAME


LAME is a popular Open Source MP3 encoder. We need the standard windows LAME binaries, which aren't provide by the LAME site, but a quick Google search will provide places such as this. Install it, and test from the command line again:


lame -b 64 stream.wav stream.mp3

That's the hard stuff done. If you don't mind going to the command line and doing some manual labour each time you want to save a stream then you can stop here. However, if you want to make your life easier in future, then you may wish to script those commands.


Part 3: Join parts 1 and 2 with Python


Jkx wrote a little Python script in 2004 to automate the MPlayer and LAME processes, and to parse the stream from the .ram file. Due to bit rot this no longer works, so I made a few small fixes and minor improvements to create realToMp3.py.


To run this you'll need the Open Source scripting language Python installed. Unlike the previous two packages this comes with a nice easy installer. Note that the locations of MPlayer and LAME are hard-coded in the script, so you'll need to edit these. You can use any text editor, but make sure you only ever use spaces, not tabs, or Python will complain. You can also edit the folder in which the resultant mp3s are saved.


Again you can test this from the command line:


realToMp3.py http://location.of.stream.ram

Part 4: Add GUI with Windows Scripting Host


Finally, if you want a nice icon on your desktop, and an easy interface to use your python script, then you can use this very short Windows Script (the successor to batch files), realToMp3.vbs. [Aside: Visual Basic Script (.vbs) may be yucky, but it has the crucial InputBox() command that JScript (.js) lacks.] If it's in the same folder as the python script then it should work without alteration. Then just create a shortcut to that script on your desktop, and set its icon to something nice.


Conclusion


On reflection it's a little odd to use two scripting languages to accomplish this task. However, trying to do parts 3 and 4 in the same language presents some interesting difficulties. I leave this as homework for the reader. In any case, it's extremely satisfying setting up this system instead of paying US$20 for some easier-to-install but harder-to-configure all-in-one software.


Updated 25-Feb-2006 Python script: Destination folder now works properly.

Updated 15-Oct-2006 Python script: Now works for URLs with querystrings.

Updated 9-Apr-2007 Python script: Added podcast support (read more).

Updated 17-Feb-2009 Python script: Switched to Python 3. Added RTSP stream support. (read more).

Updated 9-Apr-2010 Python script: Added WMV stream support. MP3 name now based on info provided in stream. (read more).