Shell Script To Convert All .flac Files To .MP3 Format

in Categories Multimedia last updated June 18, 2009
# A Shell Script To Convert All .flac Files To .MP3 Format
# Note: I found this script somewhere on usenet and I've modified it for my needs
t=$(${FIND} . -type f  -iname "*.flac")
if [ "$t" == "" ]
	echo "There are no *.flac file in $(pwd) directory"
	exit 1
for f in *.flac
	OUTF=$(echo "$f" | sed s/\.flac$/.mp3/g) 
	ARTIST=$(${METAFLAC} "$f" --show-tag=ARTIST | sed s/.*=//g)
	TITLE=$(${METAFLAC} "$f" --show-tag=TITLE | sed s/.*=//g)
	ALBUM=$(${METAFLAC} "$f" --show-tag=ALBUM | sed s/.*=//g)
	GENRE=$(${METAFLAC} "$f" --show-tag=GENRE | sed s/.*=//g)
	TRACKNUMBER=$(${METAFLAC} "$f" --show-tag=TRACKNUMBER | sed s/.*=//g)
	DATE=$(${METAFLAC} "$f" --show-tag=DATE | sed s/.*=//g)
	$FLAC -c -d "$f" | $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" 

Share this on:

9 comment

    1. FLAC is not lossy it is in fact lossless. I don’t see any reason to convert a lossless format to a lossy format?

  1. Hi guys,

    I want to write a shell script based in some of the code posted here. I just want to ask you a few things…

    I want to normalize my music collection, which is about 20 GB by now. Some files are encoded as mp3 vbr 320 kbps. I want to go OSS so, do you think it’s a good idea to convert those big mp3 bitrate files to quality, say 4 or 5, ogg files??

    So, in order to achieve this, I need to read various directory levels with files encoded at different bitrates, which I only want to encode if they’re higher or equal than 160 kbps.

    I have read a large number of articles and it seems that the only way to encode to ogg is decoding to wav first. So in summary, I need to check bitrate, stream out (I’m guessing here) the mp3 file, somewhere in the middle get the ID3 tags, pipe it to ogg vorbis function that encodes it. Right?

    I’m hoping you guys can help me by maybe pointing at some guide where I can understand how to get the properties needed, stream out, encode in ogg format. Thanks in advance :D

  2. An incredibly badly written and inefficient script.

    First, the hardcoding of utility locations will not work on many systems.

    Instead, this section would have been better used to check for the existence of the necessary commands — no matter where they are located. (On my system, for example, lame is in /usr/local/bin)

    That is followed by an unnecessary find and test block.

    Then for each file, there is an unnecessary call to sed when shell parameter expansion was called for.

    That is followed by six calls each to metaflac and sed when one would have done.

    ## flac2mp3 - copyright 2009 Chris F.A. Johnson
    for f in *.flac
      eval "$(
         metaflac "$f" --show-tag=ARTIST \
                       --show-tag=TITLE \
                       --show-tag=ALBUM \
                       --show-tag=GENRE \
                       --show-tag=TRACKNUMBER \
                       --show-tag=DATE | sed 's/=\(.*\)/="\1"/'
      flac -c -d "$f" | 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"
      1. Both scripts are really nice… But could you explain me what sed does in those scripts.

        sed s/.*=//g
        sed ‘s/=\(.*\)/=”\1″/’

        It seems that it assigns variables – but how? I know sed a bit – but this writing doesn’t tell me a thing.

        Kind regards,

        1. The output of the metaflac –show-tag command is apparently something like:


          The first sed command in the fist script removed the “ARTIST=” portion, leaving “PeterGabriel” as the result and assigning that to a shell variable named ARTIST. (Without loss of generality there)

          The 2nd sed is craftier. It encloses the value (PeterGabriel) in this case, in quotes and uses eval to do the assignment. Hence, the string to be evaled os morphed from ARTIST=PeterGabriel to ARTIST=”PeterGabriel”.

Leave a Comment