Monday, February 8, 2010

Covert .mp3 to .wav and .ogg from command

I have a ton of various music files on my machine that are in various states of migrating from .mp3 to different formats.

The primary reason for this is because of the issue of licensing between Linux and the MP3 format. And although there are GUI tools to do nearly every job you need, there are times when the command line is still your friend.

For instance, say you want to do batch jobs – the command line is good for this. Or say you want to (for whatever reason) secure shell into a remote machine and then convert your files.

For whatever reason you need, it’s good to know that the tools are available for the job.

The tools I am talking about are mpg123 and mpg321. Although mpg321 claims to be a drop-in replacement for mpg123, I still prefer to use both tools (the former for converting .mp3 to .wav and the latter for converting .mp3 to .ogg).

And in this article you will see how simple it is (using the command line) to convert these file types.

Since we will be using the command line for the conversion, we’ll install the tools from the command line. The distribution I am using for example is based on the recent, stable Debian.

You will not need to add any repositories to your /etc/apt/sources.list file, because all of the tools are found on the standard repositories.

To install these tools, follow these steps:
  1. Open up a terminal window.
  2. If needed, su to root (if you use sudo in place of root, just add sudo to the beginning of the installation commands below).
  3. Issue the command apt-get install mpg123 mpg321 vorbis-tools
That’s it. Now let’s take a look at how the tools are used.

Convert .mp3 to .wav
The first conversion is to .wav. Why use .wav? First and foremost, the .wav file is not compressed and is lossless, so the sound is better.

The only downfall is that the files are much bigger. So, if you have a particular file and you want to retain as much quality as you can, .wav is the format to use.

Of course, in this instance we are converting a lossy file type (.mp3) so there is already diminished sound quality.

But why diminish it further?

To make this conversion, the command looks like this:

# mpg123 -w output_file.wav input_file.mp3

Where output_file is the name of the .wav file that will be converted from the mp3 file named input_file. So let’s say you want to convert the file Rush_Tom_Sawyer.mp3 to .wav.

That command would look like:

# mpg123 -w Rush_Tom_Sawyer.wav Rush_Tom_Sawyer.mp3

Convert .mp3 to .ogg
The .ogg format is the open source equivalent to .mp3 and is supported by many players. The .ogg format is a good format to use when creating “mix cd’s” (I’m old, I still want to say “mix tapes”), because you can fit more files per CD than if you were using the .wav format.

But to convert the .mp3 to .ogg the command looks like:

# mpg321 Input_File.mp3 -w raw && oggenc raw -o Output_file.ogg

Let’s examine the same file we converted to .wav above. The command to convert to .ogg from .mp3 would look like:

# mpg321 Rush_Tom_Sawyer.mp3 -w raw && oggenc raw -o Rush_Tom_Sawyer.ogg

Easy right?

Final thoughts
Now you can get crafty and create batch scripts that will allow you to do batch conversions. Naturally many will think

“Why would I go through that, when I can just download a handy GUI tool like Soundconverter to do the job? Why? Because it’s always smart to have the command line option around.

One day you might need it.


  1. Author Jack Wallen on

  2. Hi Attia, I'm wondering if you can validate in the script if the source mp3 file is encoded at 160 kbps or more, then use quality=.45 ELSE use quality=.2 to make a better use of disk space.