The following script will trans encode FLAC to MP3 and read the VORBIS tags and write ID3 tags to the MP Will not alter original FLAC.

The scrip needs the following libraries and tools :

  • lame
  • flac
  • mp3gain

Requires command line parameters in the following format :-

# ./flac2mp3.sh
No Source Dir given Please specify the full path to the source flac file

The code is :

#!/bin/bash
#
# Desc :- Script will convert flac to mp3
#         Will pull flac tags into mp3 as ID3v2 tags
# Version :- 1.0.0
# Date :- 1-02-09
# Author :- undersys
# Dependencies :- lame-3.98.2, flac-1.2.1-r3, mp3gain-1.4.6-r2
#

# -------------------
# Set Global Config
# -------------------
#
# Only change seven settings below
#

# Set the base dir to write mp3s to, artist and album structure will be build under this
BASEDIR=/store/mp3

# Set who should own the files at the end
REALUSR=samba

# Set group to own files/folders
REALGRP=users

# Set path to lame binary
LAME=/usr/bin/lame

# Set path to flac binary
FLAC=/usr/bin/flac

# Set path to metaflac, part of flac
METAFLAC=/usr/bin/metaflac

# Set path to mp3gain binary
MP3GAIN=/usr/bin/mp3gain

# -------------------
# End Global Config
# -------------------

# -------------------
# Set Global Vars
# -------------------

# Must check for valid input first, befour we set any Global Vars
if [[ -z $1 ]]; then
        echo No Source Dir given
        echo Please specify the full path to the source flac files
        exit
fi

# Name of the file that holds file list
FLIST=flist.out

# Name of tags file
TAGSFILE=tags.out

# Find total coloums in input path
MAXCOL=`echo $1 | awk -F/ '{print NF}'`

# Select Album coloum
ALCOL=$(($MAXCOL - 1))

# Selet Artist coloum
ARCOL=$(($MAXCOL - 2))

# Select artist name from full path
ARTIST=`echo $1 | awk -F/ '{print $v1}' v1=$ARCOL`

# Select album name from full path
ALBUM=`echo $1 | awk -F/ '{print $v1}' v1=$ALCOL`

# Set source dir from input
SOURCEDIR=$1

# create a list of .flac files and write to file
FILES=`find $1 -name "*.flac" >$FLIST`

# ------------------
# End Global Vars
# ------------------

# --------------------
# Start Function Block
# --------------------

function cdir {
        if [[ -d $BASEDIR/$ARTIST/$ALBUM ]]; then
                echo This Album Has been encoded
                echo Script will now exit
                exit
        fi

        mkdir -p $BASEDIR/$ARTIST/$ALBUM
        }

function transcode {
        cp flist.out $BASEDIR/$ARTIST/$ALBUM/$FLIST
        cd $BASEDIR/$ARTIST/$ALBUM
        cat $BASEDIR/$ARTIST/$ALBUM/flist.out | while read line; do
        $METAFLAC --no-utf8-convert --export-tags-to=- ${line} | sed 's/=\(.*\)/="\1"/' >$TAGSFILE
        . ./$TAGSFILE
        FLACN=`echo ${line} | awk -F/ '{print$NF}'`
        MP3N=`echo $FLACN | sed 's/\..\{4\}$//'`
        $FLAC -d -c ${line} | $LAME -V3 --vbr-new \
        --tt "$TITLE" \
        --tn "$TRACKNUMBER" \
        --tg "$GENRE" \
        --ty "$DATE" \
        --tc "$COMMENT" \
        --ta "$ARTIST" \
        --tl "$ALBUM" \
        --add-id3v2 \
        - $MP3N.mp3
        done
       }

function replaygain {
        $MP3GAIN  -k -p -s r  $BASEDIR/$ARTIST/$ALBUM/*
        }

function setperms {
        chown -R "$REALUSR:$REALGRP" $BASEDIR/$ARTIST/$ALBUM
        chmod -R 774 $BASEDIR/$ARTIST/$ALBUM
        }

function clearvars {
        cat $BASEDIR/$ARTIST/$ALBUM/$TAGSFILE | while read line;
        do
        TAG=`echo "${line}" | awk -F= '{print $1}'` | unset $TAG
        done
        rm $BASEDIR/$ARTIST/$ALBUM/$TAGSFILE
        rm $BASEDIR/$ARTIST/$ALBUM/$FLIST
       }

# --------------------
# End Function Block
# --------------------

# --------------------
# main section
# --------------------

cdir
transcode
replaygain
setperms
clearvars

exit

# --------------------
# End main section
# --------------------

# -------------------
# End Script
# -------------------