Skip to content

Converting flac encoded audio to mp3.

March 15, 2009

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 $? 

Advertisements

From → Solaris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: