Hack 24. Rip CDs from the Command Line
Use command-line tools to rip CDs the old-school way.
There are a lot of CD-ripper and MP3-and Ogg Vorbisencoder GUI programs out there nowadays that streamline and automate the process of turning a CD into a directory of digital music files. Most of these programs, however, still make use of the same old ripping and encoding tools that have been used on the command line for ages. If you want to hearken back to the era of command-line CD rippingor maybe your machine isn't even running a graphical environmentthis hack will walk you through the two-step process of ripping and encoding so you can quickly turn a CD into a directory full of MP3s or Ogg Vorbis goodness.
2.13.1. Rip the CD
The first step to convert a CD into MP3 or Ogg files is to rip the CDDA audio tracks from the CD to individual WAV files. My tool of choice for this is cdparanoia, which is a commonly used program for ripping CDs. Chances are that your distribution includes it, but if not, you can download the source directly from the official project page at http://www.xiph.org/paranoia. There are a number of other CD-ripping programs available including the original cdda2wav program, but I like cdparanoiaas because, well, it's more paranoid. CD drives and CDs themselves aren't perfect, and a jittery CD drive, or a scratched CD can often result in pops or other errors in your tracks when you are ripping. cdparanoia compensates for all these problems and reads the CD very carefully, bit by bit. It attempts to repair any scratches it finds and compensates for a jittery CD-ROM drive. The result is as accurate a WAV file as you are going to get from a CD, particularly if it is damaged. Of course, there's a downside to all this paranoiaspeed. Even on a fast CD-ROM drive, don't be surprised if cdparanoia rips at 2x speed through the entire CD.
cdparanoia is pretty simple to use and has a few basic options. The simplest example is to rip an entire CD into WAV files:
$ cdparanoia -B
The -B option tells cdparanoia to go into batch mode, which causes it to split the output into multiple files at track boundaries and name each track "track[number]" where number is simply the number of the track. If you don't specify which tracks to rip, cdparanoia will default to ripping every track on the CD. As cdparanoia rips, you will notice live output, which tells you how far along it is in a particular track, and you will probably notice that there is an animated smiley face (hopefully) at the far right of the screen. This smiley isn't just for decoration; it alerts you to any problems cdparanoia is having at a particular point on the track. Table 2-3 lists the different smileys and what they stand for.
You don't have to rip the entire CD with cdparanoia either. If you only want to extract a certain range of tracks, specify the track number or track range on the command line. For instance, to extract tracks 2 through 5, type
$ cdparanoia -B 2-5
After cdparanoia is finished ripping the CD, you should have a directory full of .wav files. The next step is to encode them into MP3 or Ogg Vorbis.
2.13.2. Encode the WAVs to MP3s
Once you have a directory of WAV files and you want to convert them to MP3s, the next step is to choose an MP3 encoder. There are a number of different encoders out there, but probably the most common encoder under Linux is LAME. LAME stands for "Lame Ain't an MP3 Encoder," because originally it wasn'tit was a GPL'ed patch against the dist10 ISO distribution engine. Over time, that engine has been replaced and now LAME is a fully independent LGPL MP3 encoder hosted at http://www.mp3dev.org.
LAME is only distributed in source code form from the web site, so if you want to get it directly from the source (pun intended), download the tarball from the official page and follow the installation instructions to compile it. There are also a number of third parties that distribute LAME packages for various Linux distributions, but you will likely not find LAME prepackaged in a commercial distribution's official package repository. Why? Patents. The company Fraunhofer Gesellschaft owns a number of patents on specific parts of the technology that creates MP3s. While the legal ramifications of these patents are complicated, the short story is that, to avoid the risk of lawsuit, many Linux distributors have opted not to include MP3-encoding software in their distributions. For more information on this issue, check out the excellent FAQ at http://web.media.mit.edu/%7Eeds/mpeg-patents-faq.
Once LAME is installed, the basic encoding process is rather simple. To encode the file track01.cdda.wav to an MP3, type:
$ lame track01.cdda.wav track01.cdda.mp3
By default, LAME encodes in 128 kilobits per second at a Constant Bit Rate (CBR). This means that each second of audio will use 128 kilobits of disk space. For many people, the default is rather low, so you will likely want to use the -b option to specify your own bitrate. To double the bitrate to 256 kilobytes type:
$ lame -b 256 track01.cdda.wav track01.cdda.mp3
LAME also supports Variable Bit Rate (VBR) encoding. In VBR encoding, you specify a minimum and maximum bitrate, and LAME will vary the bitrate between those values depending upon the requirements of the audio it is encoding. Sections of the audio that don't need high quality will use a lower bitrate, ideally resulting in a file that sounds almost as good as a file encoding at the maximum bitrate, but with a reduced file size. LAME offers two different VBR algorithms, --vbr-old and --vbr-new. The new algorithm has undergone considerable tuning and is now considered to be on par with the original --vbr-old algorithm, but it is almost twice as fast. To encode an MP3 using VBR that ranges between 128 and 256 kb, type this:
$ lame -b 128 -B 256 --vbr-new track01.cdda.wav track01.cdda.mp3
With a little bit of shell scripting, you can use these commands to encode every WAV in your current directory to a 192 kbps CBR MP3:
$ for i in *.wav; do j=`echo $i | sed -e 's/\.wav/.mp3/'`; lame -b 192 $i $j; done;
Or if you want to do the same thing, only with a VBR MP3 that varies between 128 and 256 kbps:
$ for i in *.wav; do j=`echo $i | sed -e 's/\.wav/.mp3/'`; \ lame -b 128 -B 256 --vbr-new $i $j; done;
2.13.3. Encode the WAVs to Ogg Vorbis
Because of the issues with the MP3 codec and patents, a group of developers got together to create a completely open source compressed audio codec to replace MP3. The result was called Ogg Vorbis, and it touts higher quality audio files at the same or even smaller sizes compared to MP3s. Most audio players under Linux now support Ogg Vorbis files, and because it is not encumbered with any patents, most distributions also ship with Ogg Vorbis plug-ins, encoders, and other software. oggenc is the command-line program used to convert files into the Ogg Vorbis format.
For basic options, oggenc is executed much like LAME and shares many of the common arguments. Ogg Vorbis files automatically use VBR encoding, so instead of specifying a hard bitrate to use, the -b option specifies an average bitrate to use. The simplest use of oggenc is this:
$ oggenc track01.cdda.wav
This will produce a file called track01.cdda.ogg with an average 128 kbps bitrate. To encode at a higher average bitrate such as 256 kbps, use the -b option:
$ oggenc -b 256 track01.cdda.wav
To automate this process for a complete directory of WAV files, we just modify the earlier lame shell script slightly:
$ for i in *.wav; do oggenc -b 192 $i; done;