Tuesday, June 12, 2012

How to Convert FLAC to MP3 in a Batch

Converting flac to mp3 package pre-requirements are the same as published in another post (How to Convert APE+CUE to MP3). So I will add what is necessary. You will need flac package.
apt-get -y install flac
Here is a script that does the rest (file flac-mp3.sh):
#!/bin/bash

for f in *.flac
do
    metaflac --export-tags-to=- "$f" | \
        sed 's/=\(.*\)/="\1"/' | \
        sed 's/\(.*\)=/\L&/' > tags.sh
    . ./tags.sh
    rm ./tags.sh
   
    out_dir="mp3/$artist/$album"
    if [ ! -d "$out_dir" ]; then
        mkdir -p "$out_dir"
    fi
    flac -cd "$f" | lame -h - -v --preset cd \
        --tt "$title" \
        --tn "$tracknumber" \
        --tg "$genre" \
        --ty "$date" \
        --ta "$artist" \
        --tl "$album" \
        --add-id3v2 \
        "$out_dir/${f%.*}.mp3"
done
Drop that file into a directory that has flac files. Run the script and in few minutes you will get a mp3 directory with your mp3 tracks folded by artist/album.