Convert .mp3 files to .wav files in Linux
With the popularity of mp3 players, and the frustrations of using DRM-crippled music, it is always nice to be able to rip your own mp3 files. There are plenty of Linux tools to handle this task. But what about the mp3 collection that you want to burn onto a playable CD? Although there are many CD players that will play mp3 format, not all will. For that you need to have .wav file format on the CD. One tool for this conversion is the command-line mpg123 utility. The mpg123 command can do a lot of things, one of the things it is best at is conversion. In this article I am going to show you how to install mpg123 and then use it to convert mp3 files to wav files.
The first thing to do is to get mpg123 installed. This can be done very quickly via command line. One of the following commands will do the trick (depending upon which distribution you use):
apt-get install mpg123
urpmi mpg123
If you are using Fedora Core you will most likely have to stop by rpm.pbone.net, download the correct rpm package and install with the command:
rpm -ivh mpg123-RELEASE_NUMBER.rpm
Where RELEASE_NUMBER is the actual release number you download.
Once installed you are ready to go.
With the wav files located in a directory change to that directory to run the command. The format of the command will be:
mpg123 -w file.wav file.mp3
The "-w" argument tells mpg123 that the output will be in the .wav format. The first file name is the output file name which is user configurable. A word of warning, spaces in file names aren't always the best choice in the Linux operating system. If you want to separate words in a file name you can use "_" character. So creating a .wav of Rush's Tom Sawyer you would do something like:
mpg123 -w Rush_Tom_Sawyer.wav "01 - Tom Sawyer.mp3"
Batch Conversion
What about batch conversion? This requires a bit of shell scripting. Create a Music directory (in modern Linux distributions there should be one in ~/) and dump all of your mp3 files into that directory. Next, create a shell script in your favorite text editor. We'll call that script "batch_conversion". The contents of the script might look like:
#!/usr/bin/perl
my $dir = "~/Music";
opendir DH, $dir or die "Can't open $dir: $!";
$count2=1;
while ($name = readdir DH) {
next unless $name =~ /\.mp3$/;
$wav="$count2.wav";
print "$wav\n";
system "mpg123 -w $wav \"$name\"";
$count2++;
}
Once you save the file you have to give it executable permissions with the command chmod u+x batch_conversion. To run the command you will issue (from the directory the new file is located) ./batch_conversion. Once you run the file you will have both the mp3 and the wav files located in the ~/Music directory.
Final Thoughts
Converting mp3 files to wav files for burning audio CDs is a simple process with mpg123. There are gui tools for this job but the command line tools make for much more flexible jobs.
Advertisement
Hello ,Thank you for your article! It is helpful to me.
And is there a Batch Mp3 Convert software? There is no need to do via the shell scripting.
Thank you!
Thanks for the advice and the script, one word of warning, though. I had to replace all the “ characters with “, (edit. ghacks auto changes the ” character. Just replace it with the correct one in your script) because I copypasta’ed your script into a file. I also had to replace “~/Music” with “/home/username/Music”. But I don’t know if that was just my system or not. Either way, thanks for the advice.
Why go through all that trouble when K3B will automatically convert mp3 files to wav and burn them?