Archive for May, 2009

Converting OGG and FLAC to MP3, Copying Tags

May 9, 2009 Leave a comment

I received an iPod Nano 8GB 4G (Model #A1285) as a present the other day. This is the first device of this kind I have used, so I was not really sure where to start. Despite reading several promising reports of this model’s compatibility with both Amarok and GNUpod, I was only able to get things working with Rhythmbox.

I quickly found however that it did not like my FLAC and OGG files, and Rhythmbox would not convert them to MP3 automatically. The script below converts FLAC and OGG files to MP3 using lame. It also attempts to copy over the album, artist, year, title, and track number tags using ogginfo, metaflac, and id3v2.

set -e
set -u

# usage: <source directory> <parent of new destination directory>"

sd=`readlink -f "$1"`
ddp="`readlink -f "$2"`/`basename "$sd"`"
mkdir "$ddp"
cd "$sd"
for f in *; do
 case "$f" in
 echo "$f"
 ogg123 --quiet --audio-buffer 8192 --device wav --file - "$f" \
 | lame --quiet -b 192 - "$ddp/${f%.ogg}.mp3"
 id3v2 \
 -a "`ogginfo "$f" | grep ARTIST | sed -e 's/^\s\+ARTIST=//'`" \
 -y "`ogginfo "$f" | grep DATE | sed -e 's/^\s\+DATE=//'`" \
 -A "`ogginfo "$f" | grep ALBUM | sed -e 's/^\s\+ALBUM=//'`" \
 -T "`ogginfo "$f" | grep TRACKNUMBER | sed -e 's/^\s\+TRACKNUMBER=//'`" \
 -t "`ogginfo "$f" | grep TITLE | sed -e 's/^\s\+TITLE=//'`" \
 echo "$f"
 flac -sdc "$f" \
 | lame --quiet -b 192 - "$ddp/${f%.flac}.mp3"
 id3v2 \
 -a "`metaflac --show-tag=artist "$f" | sed -e 's/^ARTIST=//'`" \
 -y "`metaflac --show-tag=date "$f" | sed -e 's/^DATE=//'`" \
 -A "`metaflac --show-tag=album "$f" | sed -e 's/^ALBUM=//'`" \
 -T "`metaflac --show-tag=tracknumber "$f" | sed -e 's/^TRACKNUMBER=//'`" \
 -t "`metaflac --show-tag=title "$f" | sed -e 's/^TITLE=//'`" \
 echo "skipping $f"
cd - > /dev/null