Converting flac encoded audio to mp3.

Solaris has the flac command which will happily decode flac encoded file and metaflac to read the flac meta data but you need to download lame either precompiled or from sourceforge and build it. Then it is a simple matter to convert your flac encoded files:

$ flac -c -d file.flac | lame - file.mp3

you can add various flags to change the data rates and put tags into the mp3. I feel sure someone should have written a script that would convert an entire library but I could not find one so here is the one I wrote to do this:

#!/bin/ksh93
umask 22
SRC="$1"
DST="$2"
BITRATE=128

export PATH=/opt/lame/bin:/usr/sbin:/usr/sfw/bin:/usr/bin

function flactags2lametags
{
        typeset IFS="$IFS ="
        typeset key value
        metaflac --export-tags /dev/fd/1 "$1" | while read key value
        do
                print typeset "${key}=\\"$(print $value| sed 's/["'\\'']/\\\\&/g')\\";"
        done
}

function cleanup
{
        if [[ "$FILE" != "" ]]
        then
                rm "$FILE"
        fi
}

function convert_file
{
        typeset -i ret=0
        typeset f="${1%.flac}"
        if [[ "$f" != "${1}" ]]
        then
                typeset out="${DST}/${f}.mp3"
                if ! [[ -f "${out}" ]] || [[ "$1" -nt "$out" ]]
                then
                        print $out

                        eval $(flactags2lametags "$1") 
                        FILE="$out"
                        flac --silent -c -d "$1" | \\
                                lame --quiet -b ${BITRATE} -h \\
                                ${ARTIST:+--ta }"${ARTIST}" \\
                                ${ALBUM:+--tl }"${ALBUM}" \\
                                ${TRACKNUMBER:+--tn }"${TRACKNUMBER}" \\
                                ${DATE:+--ty }"${DATE%%-\*}" \\
                                - "${out}"
                        ret=$?
                        FILE=""
                fi
        elif [[ $1 != "${1%.jpg}" ]]
        then
                [[ -f "${DST}/${1}" ]] || cp "$1" "${DST}"
        fi
        return ${ret}
}

function do_dir
{
        typeset i
        for i in "$1"/\*
        do
                if [[ -f "$i" ]]
                then
                        convert_file "$i" || return 1
                elif [[ -d "$i" ]]
                then
                        if test -d "${DST}/$i" || mkdir "${DST}/$i"
                        then
                                do_dir "$i" || return 1
                        fi
                fi
        done
        return 0
}

function usage
{
        print USAGE: $1 src_dir dest_dist >&2
        exit 1
}

trap cleanup EXIT

(( $# == 2 )) || usage $0
if test -d "${DST}/$1" || mkdir "${DST}/$1"
then
        do_dir "$1"
fi
exit $?
Comments:

Thx.

Posted by muhabbet on March 15, 2009 at 03:23 PM GMT #

I use http://projects.robinbowes.com/flac2mp3/trac to do exactly that.

Posted by Ben on March 17, 2009 at 01:22 AM GMT #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today