When I first encountered the iTunes-LAME Encoder, I was excited about the idea of making the process of ripping CDs to LAME-encoded MP3s easier. I was, however, pretty underwhelmed by the product. Despite the genorous comments on MacUpdate and VersionTracker, I found the software inelegent and generally rough around the edges. So I set out to accomplish the same task via the command line, which I did. And more. What began 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. Here are my seven simple scripts to make your audio life more fun:
And away we go! The first script uses LAME to rip a CD to the current directory.
#!/bin/sh
#
# lameit - rip a cd to lame-encoded mp3s
#
if [ "$1" ]
then
for file in "$1"/[1-9]\ *.aiff
do
if [ -e "$file" ]
then
lame -h -m s -b 192 "$file" "0$(basename "$file" .aiff).mp3"
else
echo >&2 "No appropriate files exist in directory: "$1""
exit 1
fi
done
for file in "$1"/[1-9][0-9]\ *.aiff
do
if [ -e "$file" ]
then
lame -h -m s -b 192 "$file" "$(basename "$file" .aiff).mp3"
fi
done
else
echo >&2 "Usage: "$(basename "$0")" /path/to/cd"
exit 1
fi
This script does the same thing, but with FLAC.
#!/bin/sh
#
# flacit - rip a cd to flac format
#
if [ "$1" ]
then
for file in "$1"/[1-9]\ *.aiff
do
if [ -e "$file" ]
then
flac \
--endian=little \
--sign=signed \
--channels=2 \
--sample-rate=44100 \
--bps=16 \
--skip=20 \
--output-name="0$(basename "$file" .aiff).flac" \
"$file"
else
echo >&2 "No appropriate files exist in directory: "$1""
exit 1
fi
done
for file in "$1"/[1-9][0-9]\ *.aiff
do
if [ -e "$file" ]
then
flac \
--endian=little \
--sign=signed \
--channels=2 \
--sample-rate=44100 \
--bps=16 \
--skip=20 \
--output-name="$(basename "$file" .aiff).flac" \
"$file"
fi
done
else
echo >&2 "Usage: "$(basename "$0")" /path/to/cd"
exit 1
fi
And here's the way to get from FLAC to MP3 in one step. It outputs the MP3 files to your current directory but the FLAC files needn't be in your current directory.
#!/bin/sh
#
# flacmp3 - convert a flac file to mp3
#
if [ "$1" ]
then
for file
do
if [ -e "$file" ]
then
flac -c -d "$file" | lame -h -m s -b 192 - "$(basename "$file" .flac).mp3"
else
echo >&2 "No such file: "$1""
exit 1
fi
done
else
echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
exit 1
fi
Next comes the question of labeling the files. I use id3tool to slap together id3 tags before importing them into iTunes because they get lost in my collection otherwise. id3tool works fine for labeling the artist, album, year, and genre, but setting the track number and song title can become tedious. So here's a little hack I whipped up. It only works 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" or "01 Moondance.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.
#!/bin/sh
#
# id3hack - add track names and numbers to id3 tags
#
if [ "$1" ]
then
for file
do
if [ -e "$file" ]
then
id3tool \
--set-title="$(echo "$file" | sed 's/...\(.*\)\.mp3/\1/')" \
--set-track="$(echo "$file" | sed 's/\(..\).*/\1/')" \
"$file"
else
echo >&2 "No such file: "$1""
exit 1
fi
done
else
echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
exit 1
fi
And again, this is the same script as the one above, only it creates vorbis comments for FLAC files as opposed to id3 tags for mp3s:
#!/bin/sh
#
# vchack - add track names and numbers to flac files
#
if [ "$1" ]
then
for file
do
if [ -e "$file" ]
then
metaflac \
--set-vc-field=TITLE="$(echo "$file" |
sed 's/...\(.*\)\.flac/\1/')" \
--set-vc-field=TRACKNUMBER="$(echo "$file" |
sed 's/\(..\).*/\1/' |
sed 's/0\(.\)/\1/')" \
"$file"
else
echo >&2 "No such file: "$1""
exit 1
fi
done
else
echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
exit 1
fi
Speaking of vorbis comments, those things are a pain to work with. Out of frustration, I wrote a script that brought the id3tool interface over to the world of vorbis comments and FLAC.
#!/bin/sh
#
# vctool - set vorbis comments in flac files
#
if [ "$1" ]
then
while getopts t:a:r:y:g:c:h option
do
case "$option" in
t) TITLE="--set-vc-field=TITLE="$OPTARG"";;
a) ALBUM="--set-vc-field=ALBUM="$OPTARG"";;
r) ARTIST="--set-vc-field=ARTIST="$OPTARG"";;
y) DATE="--set-vc-field=DATE="$OPTARG"";;
g) GENRE="--set-vc-field=GENRE="$OPTARG"";;
c) TRACKNUMBER="--set-vc-field=TRACKNUMBER="$OPTARG"";;
h) echo ""$(basename "$0")" <options> <filename>"
echo " -t WORD Sets the title to WORD"
echo " -a WORD Sets the album to WORD"
echo " -r WORD Sets the artist to WORD"
echo " -y WORD Sets the date to WORD"
echo " -g WORD Sets the genre to WORD"
echo " -c WORD Sets the track number to WORD";;
esac
done
shift $((OPTIND - 1))
for file
do
if [ -e "$file" ]
then
for var in "$TITLE" "$ALBUM" "$ARTIST" "$DATE" "$GENRE" "$TRACKNUMBER"
do
if [ "$var" ]
then
metaflac "$var" "$file"
fi
done
else
echo >&2 "No such file: "$file""
exit 1
fi
done
else
echo >&2 "Type "$(basename "$0")" -h for help."
exit 1
fi
Lastly, a script that converts vorbis comments to id3 tags.
#!/bin/sh
#
# vcid3 - convert vorbis comments to id3 tags
#
if [ -e "$1" ]
then
if [ -e "$2" ]
then
TITLE="$(metaflac --show-vc-field=TITLE "$1" |
sed 's/TITLE=\(.*\)/\1/')"
ARTIST="$(metaflac --show-vc-field=ARTIST "$1" |
sed 's/ARTIST=\(.*\)/\1/')"
ALBUM="$(metaflac --show-vc-field=ALBUM "$1" |
sed 's/ALBUM=\(.*\)/\1/')"
TRACK="$(metaflac --show-vc-field=TRACKNUMBER "$1" |
sed 's/TRACKNUMBER=\(.*\)/\1/')"
YEAR="$(metaflac --show-vc-field=DATE "$1" |
sed 's/DATE=\(.*\)/\1/')"
GENRE="$(metaflac --show-vc-field=GENRE "$1" |
sed 's/GENRE=\(.*\)/\1/')"
if [ "$GENRE" ]
then
id3tool --set-genre-word="$GENRE" "$2"
fi
id3tool \
--set-title="$TITLE" \
--set-artist="$ARTIST" \
--set-album="$ALBUM" \
--set-track="$TRACK" \
--set-year="$YEAR" \
"$2"
else
echo >&2 "No such file: "$2""
echo >&2 "Usage: "$(basename "$0")" FLACFILE MP3FILE"
exit 1
fi
else
echo >&2 "No such file: "$1""
echo >&2 "Usage: "$(basename "$0")" FLACFILE MP3FILE"
exit 1
fi
Hope you find these useful. Do with them what you wish...
Mac OS X Hints
http://hints.macworld.com/article.php?story=20040323004759816