Hack 35. Convert from One Audio Format to Another
Use command-line tools to convert between audio formats.
While a desktop user may only encounter a few different types of sound formats on an average day, there are tens, if not hundreds, of different audio formats out there, each with its own specific use and set of features. In this hack, I tell you how to use standard command-line tools to convert between the major audio formats you might encounter.
There are different ways you could categorize audio formats, but one easy way is to split them into lossy and lossless categories. Lossy formats (like MP3s, WMA, and Ogg Vorbis files) not only compress audio, but also strip away parts of the sound that are inaudible or mostly inaudible to the listener. The degree of sound-quality loss varies based on the bitrate of the file and which format you use (certain formats are known for generating higher quality sound at the same or lower bitrates), but with any lossy format, there is a loss in sound qualityhence the name.
Lossless formats (like WAV, CDDA, and FLAC) provide maximum sound quality and do not strip away any sound from the file. This results in a (sometimes much) larger file because some lossless formats don't compress the file, while other lossless formats do.
Some of the tools I will use in this hack can convert to a number of audio formats. For the purposes of simplicity, I'm going to use the WAV file format as the intermediary format because it is lossless and most audio encoders and decoders support it. This way, instead of describing how to convert Ogg Vorbis to FLAC, Ogg Vorbis to MP3, FLAC to MP3, and so forth, I will just describe how to convert to and from WAV files so you can piece the steps together to form whatever format you want.
MP3 is a very popular lossy audio format. Even though there are many competing formats that tout smaller file size and better quality, the large number of MP3 players and MP3-playing programs has kept this format one of the most popular. While there are many different tools to encode and decode MP3 files, for this hack I will use the LAME encoder. [Hack #24] covers installing LAME and encoding MP3s.
220.127.116.11. MP3 to WAV.
To convert MP3s to WAV, pass the --decode argument to LAME. By default LAME will decode the MP3s to a WAV file, so you don't need to pass any extra arguments.
$ lame --decode example.mp3 example.wav
That code snippet is all it takes to convert example.mp3 into example.wav.
18.104.22.168. WAV to MP3.
LAME has a number of complicated and advanced options for MP3 generation, but for the purposes of this hack I will describe the basicscreating a CBR (Constant Bit Rate) and VBR (Variable Bit Rate) MP3. These terms describe whether the MP3 will use the same bitrate throughout the entire file (CBR) or whether it will vary the bitrate depending on the content of the audio at that moment (VBR).
The bitrate you choose is largely a matter of taste, so change these example values to a bitrate that suits you. To convert a WAV to a CBR MP3 with a 192 kilobits per second bitrate, type:
$ lame -b 192 example.wav example.mp3
To convert a WAV to a VBR MP3 that ranges between 128 and 256 kilobits per second, type:
$ lame -b 128 -B 256 --vbr-new example.wav example.mp3
2.24.2. Ogg Vorbis
Ogg Vorbis is another lossy format like MP3, but Ogg Vorbis is a completely Open Source format that touts higher quality audio files at the same or even smaller sizes. For these examples, I will be using the oggenc and oggdec programs from the Ogg Vorbis tool set. These tools should be packaged and easy to install for most major Linux distributions. To get more information about Ogg Vorbis, visit the official site at http://www.vorbis.com.
22.214.171.124. Ogg Vorbis to WAV.
To convert Ogg Vorbis files to WAV, use the oggdec tool. This tool was created to decode .ogg files into WAV or PCM output, and the syntax is pretty straightforward. To convert example.ogg to example.wav, type:
$ oggdec example.ogg -o example.wav
126.96.36.199. WAV to Ogg Vorbis.
Unlike MP3s, all Ogg Vorbis files use VBR encoding. Oggenc is the program of choice for creating Ogg Vorbis files out of WAVs, and when creating an Ogg Vorbis file, the bitrate you specify is the average bitrate Ogg Vorbis will attempt to use based on the content of the file. To convert example.wav to a 192 kilobit per second VBR Ogg Vorbis file, type:
$ oggenc -b 192 example.wav -o example.ogg
FLAC stands for "Free Lossless Audio Codec," and the name says it all. FLAC offers a lossless audio format like WAV but at a high level of compression so you can store CD-quality sound at a fraction of the size. FLAC is an Open Source project. If the encoding tools and libraries aren't packaged for your distribution, you can download them directly from the official site at http://flac.sourceforge.net.
188.8.131.52. FLAC to WAV.
The flac command-line tool is used for both converting to and from WAV files. To convert a FLAC file to a WAV file, add the --decode option:
$ flac --decode example.flac example.wav
184.108.40.206. WAV to FLAC.
The default behavior of the flac tool is to encode files into the FLAC format, so simply specify the input and output filenames on the command line:
$ flac example.wav example.flac
2.24.4. Other Audio Formats
There are many other audio formats that you might want to convert. Linux doesn't necessarily support encoding into some of these proprietary formats, but if it can read them, it can at least convert them to WAVs for you so that you can convert them to your favorite open format. One of the best tools for this is actually the MPlayer program. Many people associate MPlayer with video files, but it supports so many video and audio formats that it is ideal as a universal converter for proprietary audio formats. [Hack #48] provides more information on installing and using MPlayer.
220.127.116.11. Anything to WAV.
To convert a random audio format that Linux can play (in this example a WMA file, example.wma) into a WAV file (in this example a WAV file example.wav) type:
$ mplayer -vo null -vc dummy -ao pcm:waveheader:file= example.wavexample.wma
Since this is just an audio file, we pass null options to the mplayer video arguments (-vo null), and then tell the audio output argument (-ao) to output a WAV file (pcm:wavehandler) named example.wav. You can use this same method with any audio format MPlayer can play.
Sometimes you need to be able to convert a WAV file to a different WAV file. Usually this comes into play when you have WAV files you want to use on an audio CD that either aren't stereo or aren't at a 44.1 kHz sample rate, which is required for CDs. Use SoX for this kind of WAV conversion. SoX is short for Sound eXchange, and it acts like a universal sound sample translator. It is a very popular package and it might already be installed in your distribution. If not, it might already at least be packaged for you.
SoX offers a number of advanced options, but for the purpose of this hack I will discuss how to use it to change the sample rates on a WAV file. SoX is discussed in [Hack #33].
18.104.22.168. Change the Number of Audio Channels.
If you have a mono WAV file that you want to convert to stereo, you need to use SoX to change the number of audio channels (in this case from 1 to 2). In SoX the argument to use is -c followed by the number of channels. To convert a mono example1.wav to a stereo example2.wav, type:
$ sox example1.wav -c 2 example2.wav
22.214.171.124. Change the Sample Rate.
When you create an audio CD out of WAV files, the files need to conform to a 44.1 kHz sample rate. If you got your WAV files from ripping another CD or through conversion from another audio format, chances are the settings are fine, but if they aren't you need to use SoX to resample the audio to the appropriate sample rate. To resample a WAV file example1.wav to a 44.1 kHz sample rate in example2.wav, type:
$ sox example1.wav -r 44100 example2.wav resample