Convert Flac to Mp3

From ArchWiki
Revision as of 21:04, 7 January 2010 by Flexoid (Talk | contribs)

Jump to: navigation, search


Tip: You can use flac2mp3-bash script from aur to convert Flac to Mp3 easily.

Introduction

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. All other files in the directory (.nfo, images, .sfv, etc) will be ignored and unharmed.

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.

More information on flac: FLAC

Installation

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 into your preferred editor:

for a in *.flac

do
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"

done

Alternatively, below is a script that will search for all FLAC audio files beyond where the script resides on your filesystem and convert them to MP3; including those pesky filenames with spaces.

find -name *.flac -print0 | while read -d $'\0' a


do
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"

done

Save the script as flac2mp3 and make the script executable:

chmod a+x flac2mp3

As root, copy the script to /usr/local/bin (or anywhere else that is in your $PATH).

cp flac2mp3 /usr/local/bin

To make /usr/local/bin in your $PATH, do (as normal user):

PATH=$PATH:/usr/local/bin/

and then (as normal user):

nano .bashrc

and add export PATH=$PATH:/usr/local/bin/

Usage

Open up a terminal and cd to the directory of .flac files that you wish to convert and enter flac2mp3

You'll see the verbose decoding/encoding process in the terminal which may take a few moments.

Done.