Thursday, September 6, 2007

convert flac to mp3

HowTo
flac2mp3.sh:

#!/bin/bash

DESTDIR=$1
PRESET=$2

if test "x$PRESET" = "x"; then
PRESET=standard
fi

if test "x$DESTDIR" = "x"; then
DESTDIR=.
else
mkdir -p "$DESTDIR"
fi

echo
echo "Preset=$PRESET Destination=$DESTDIR Source=`pwd`"
echo

for a in *.flac
do
OUTF=`echo "$a" | sed s/\.flac/.mp3/g`

echo
echo "Source=`pwd`/$a Destination=$DESTDIR/$OUTF"
echo

ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`
TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`
ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`
GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`
TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`
YEAR=`metaflac "$a" --show-tag=DATE | sed s/.*=//g | cut -b -4`

echo
echo "Launching: flac -c -d $a | lame --preset $PRESET - $DESTDIR/$OUTF"
echo

flac -c -d "$a" | lame --preset $PRESET - "$DESTDIR/$OUTF"

echo
echo "Setting id3 ($TITLE, $TRACKNUMBER, $ARTIST, $ALBUM, $GENRE, $YEAR)"
echo

if test "x$TITLE" != "x"; then
id3v2 -t "$TITLE" "$DESTDIR/$OUTF" > /dev/null
fi

if test "x$TRACKNUMBER" != "x"; then
id3v2 -T "$TRACKNUMBER" "$DESTDIR/$OUTF" > /dev/null
fi

if test "x$ARTIST" != "x"; then
id3v2 -a "$ARTIST" "$DESTDIR/$OUTF" > /dev/null
fi

if test "x$ALBUM" != "x"; then
id3v2 -A "$ALBUM" "$DESTDIR/$OUTF" > /dev/null
fi

if test "x$GENRE" != "x"; then
id3v2 -g "$GENRE" "$DESTDIR/$OUTF"
fi

if test "x$YEAR" != "x"; then
id3v2 -y "$YEAR" "$DESTDIR/$OUTF"
fi
done

8 comments:

Anonymous said...

+ installed packages:
lame
flac
id3v2

Anonymous said...

very sexy! thanks!

leo said...

You're very welcome!

Anonymous said...

Thanks, this was helpful.

Anonymous said...

Changed encoding to preset extreme, but easy enough. Thanks for doing the dirty work.

davedave said...

Great script, thanks!

Anonymous said...

Too bad it's not recursive...

Anonymous said...
This comment has been removed by a blog administrator.