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

by on June 18, 2009 · 9 comments

  1. #!/bin/bash
  2. # A Shell Script To Convert All .flac Files To .MP3 Format
  3. # Note: I found this script somewhere on usenet and I've modified it for my needs
  4. METAFLAC=/usr/bin/metaflac
  5. FLAC=/usr/bin/flac
  6. ID3=/usr/bin/id3
  7. LAME=/usr/bin/lame
  8. FIND=/usr/bin/find
  9.  
  10. t=$(${FIND} . -type f -iname "*.flac")
  11. if [ "$t" == "" ]
  12. then
  13. echo "There are no *.flac file in $(pwd) directory"
  14. exit 1
  15. fi
  16.  
  17. for f in *.flac
  18. do
  19. OUTF=$(echo "$f" | sed s/\.flac$/.mp3/g)
  20. ARTIST=$(${METAFLAC} "$f" --show-tag=ARTIST | sed s/.*=//g)
  21. TITLE=$(${METAFLAC} "$f" --show-tag=TITLE | sed s/.*=//g)
  22. ALBUM=$(${METAFLAC} "$f" --show-tag=ALBUM | sed s/.*=//g)
  23. GENRE=$(${METAFLAC} "$f" --show-tag=GENRE | sed s/.*=//g)
  24. TRACKNUMBER=$(${METAFLAC} "$f" --show-tag=TRACKNUMBER | sed s/.*=//g)
  25. DATE=$(${METAFLAC} "$f" --show-tag=DATE | sed s/.*=//g)
  26. $FLAC -c -d "$f" | $LAME -m j -q 0 --vbr-new -V 0 -s 44.1 - "$OUTF"
  27. $ID3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "$OUTF"
  28. done


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Chris F.A. Johnson

    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
    do
      outf=${f%.flac}.mp3
      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"
    done
    
  • Vivek Gite

    Thanks for sharing your wisdom :)

  • Cool

    Cool Script – Thanks

  • slotwek

    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,
    Lukasz

  • Ramon

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

    ARTIST=PeterGabriel

    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”.

  • Alejandro Alcaide

    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

  • Audiophile

    Transcoding from one lossy format to another isn’t a good idea.

  • Johnny K

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

  • roccatgaming

    Most car mp3-players are supporting only mp3.

Previous Script:

Next Script: