O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
iPod and iTunes Hacks
By Hadley Stern
October 2004
More Info

HACK
#94
Manipulate Audio Using the Terminal
iTunes isn't the only way to encode your CDs. With Mac OS X, you can get to the heart of Unix to rip in alternative encoders such as LAME.
[Discuss (0) | Link to this hack]

One of the greatest advantages of Apple's OS X operating system is its Unix core. Unix is a flexible environment (once you learn how to use it) that lets you get your hands dirty and solve problems when other applications fall short. While I love iTunes' powerful "jukebox" environment, I wish it allowed more options when it comes to audio encoding. The LAME encoder (http://lame.sourceforge.net; free) is the Internet standard for quality MP3 encoding.

In the Usenet MP3 groups, you will find LAME in much wider use than any other codec, and your ears will hear the difference. Once I realized what I was missing by using the iTunes encoder, I set out to make using LAME easier.

Most command-line audio tools (such as the FLAC and LAME encoders) are designed to be used on only one file at a time, which makes batch processing tricky. A simple Unix shell script seemed the obvious solution to this problem. All big problems start small, however, and after I had written my batch LAME encoding script, I realized there were lots of little problems that I needed to solve in order to make CD archiving a more pleasant experience.

What began for me as a simple hack for batch encoding CDs and setting ID3 tags turned into a suite of programs aimed at streamlining the handling of MP3 and FLAC files from the command line. I chose the LAME (LAME Ain't an MP3 Encoder) and FLAC (Free Lossless Audio Codec: http://flac.sourceforge.net/; free) encoders, in part because they are both distributed under a version of the GNU General Public License (http://www.gnu.org/licenses/licenses.html), which keeps their development out in the open and ensures that end users are given rights to change the programs if they like. There are many advantages to both of these encoders, but audio quality and openness are chief among them.

Here, then, are eight scripts to make your audio life easier:

lameit

Rips CDs to MP3 format using LAME

flacit

Rips CDs to FLAC format

id3hack

Uses the filename to set a track's name and number in its ID3 tag

vchack

Creates a Vorbis comment for a FLAC file using the id3hack script

vctool

Borrows the id3tool interface to set Vorbis comments in FLAC files

vcid3

Converts Vorbis comments in FLAC files to ID3 tags in MP3 files

flacmp3

Converts FLAC files to MP3 files

striptoc

Reformats a cdrdao-generated table of contents file for use with FLAC files

There are, of course, some binaries that you need in order to make these scripts work. I recommend you install them using fink (http://fink.sourceforge.net) or darwinports (http://darwinports.opendarwin.org/,) or compile them by hand and put them in your $HOME/bin directory:

To install each of these using fink, simply type:

% fink install <package-name>

Some packages might not be available under fink, or fink might use an out-dated version, and you might need to compile the executable yourself. Instructions for doing this can be found on each program's web site and in the README file included with the source archive. Compilation usually involves running a configure script, followed by the make command. I recommend placing the compiled binaries in either /usr/local/bin (for system-wide use) or $HOME/bin (if you are not the system administrator or don't want to share the utilities with other users).

lameit

The first script uses LAME to rip a CD to the current directory. This process works best if you first set the CD metadata in iTunes. The easiest way to do this is to get the information from the Gracenote CDDB automatically by selecting Get CD Track Names from the Advanced menu in iTunes. If the CDDB information is incorrect, you can then edit it by hand within iTunes. Once you've done that (you might have to eject and reinsert the CD to make sure the info is updated), you're ready to start ripping.

id3hack

Next comes the question of labeling the files. I use id3tool (http://nekohako.xware.cx/id3tool/) to slap together ID3 tags before importing them into iTunes, because otherwise they get lost in my collection. id3tool works fine for labeling the artist, album, year, and genre, but setting the track number and song title can become tedious, so I whipped this little hack.

TIP

This hack works only if the files are named with the two-digit track number followed by its name—for example, 04 And Here We Test Our Powers of Observation.mp3, 01Moondance.mp3, or 05 500 Miles.mp3. You can specify as many files as you want on the command line. I usually just use the *.mp3 wildcard.

vctool

Vorbis comments can be tricky to work with. Out of frustration, I wrote a script that brought the id3tool interface over to the world of Vorbis com-ments and FLAC. Type vctool -h at the command line to get usage information.

vcid3

The vcid3 script converts Vorbis comments to ID3 tags.

flacmp3

Here's the way to get from FLAC to MP3 in one easy step. The script outputs the MP3 files to your current directory, but the FLAC files needn't be in your current directory.

TIP

This script performs the metadata conversion (without id3tool) side by side with the format conversion, so you won't need to do that separately.

striptoc

Lastly, here's a script that takes a cdrdao table of contents (TOC) file and strips away all the unnecessary information in the file. cdrdao (http://cdrdao.sourceforge.net) is used for reading and writing raw CD data from the command line. Its most useful feature is its plain-text TOC files, which can be used to extract pregap information from source CDs. But the TOC files presume a single, huge data file, which is a really inconvenient way to archive a CD. This awk script takes a listing of FLAC files from the current directory and substitutes them for the track data file.

TIP

You'll have to decompress your FLACs before burning, of course.

The code

Here's an example of the striptoc script in action, using a CD that contains only two tracks:

% ls
01 So Long Eric.flac 02 Praying With Eric.flac Town Hall Concert.toc
% cat "Town Hall Concert.toc"
CD_DA

// Track 1
TRACK AUDIO
NO COPY
NO PRE_EMPHASIS
TWO_CHANNEL_AUDIO
FILE "data.wav" 0 17:48:03
// Track 2
TRACK AUDIO
NO COPY
NO PRE_EMPHASIS
TWO_CHANNEL_AUDIO
FILE "data.wav" 17:48:03 27:31:27
START 00:00:49
% striptoc "Town Hall Concert.toc" > "Town Hall Concert.toc.new"
% cat "Town Hall Concert.toc.new"
CD_DA
TRACK AUDIO
FILE "01 So Long Eric.wav" 0
TRACK AUDIO
PREGAP 00:00:49
FILE "02 Praying With Eric.wav" 0
% mv "Town Hall Concert.toc.new" "Town Hall Concert.toc"
%

As you can see, the file generated by cdrdao also explicitly states several defaults for each file. This script throws that information out, as well as any ISRC codes and catalog information (which this CD doesn't have). Here's the script that does all the work:

#!/usr/bin/awk -f
#
# striptoc - Reformat cdrdao toc files for use with individual track files.
#
BEGIN { print "CD_DA\n" }
{ FS = "\n"; RS = ""
  if ($2 == "TRACK AUDIO") {
    print $2
    if ($NF ˜ /^START/) {
      sub(/^START/, "PREGAP", $NF)
      print $NF
    }
    FS = " "; RS = "\n"
    "ls *.flac" | getline file
    sub(/flac$/, "wav", file)
    print "FILE \"" file "\" 0\n"
  }
}

awk can be harder to follow than a shell script, but basically, this script creates a new TOC file based on the pregap information given in the original file and the listing of .flac files in the current directory. It doesn't do any checks on the data beforehand, though, so make sure everything is in order before you run the script.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.