Convert Flac to Mp3

From ArchWiki
Revision as of 05:46, 11 January 2008 by Bionnaki (Talk | contribs) (typo)

Jump to: navigation, search

Here is a script that will convert FLAC to MP3 via the commandline.

Essentially, the .flac files within a directory will be decompressed to .wav and then the resulting .wav files will be encoded to .mp3 using the latest LAME switches for encodings (-V 0 --vbr-new). The ID3 tags of the original .flac files will be passed to the resulting .mp3 files.

The original .flac files will not be harmed and the resulting .mp3s will be in the same directory.

For more information on LAME switches/settings such as V0, visit Hydrogenaudio LAME Wiki. V0 is roughly equivalent to --preset extreme which results in a variable bitrate usually between 220-260. The audio of a V0 is transparent, meaning one cannot tell the difference between the lossy file and the original source (compact disc/lossless), but yet the file size is a quite reasonable.

First you need to install the following packages: flac, lame, and id3

pacman -S flac lame id3

Once those are installed, copy the following script and make it executable:

for a in *.flac

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

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`
DATE=`metaflac "$a" --show-tag=DATE | sed s/.*=//g`

flac -c -d "$a" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"
id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"


Save the script as flac2mp3 in your home directory. As root copy the script to /usr/local/bin

cp flac2mp3 /usr/local/bin

Once that is done, you are ready to use the script. Open up a terminal and cd to the directory of .flac files that you wish to convert and run flac2mp3

Done. The newly-encoded .mp3s will be V0.