≡ Menu

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

{ 9 comments… add one }
  • Audiophile December 23, 2010, 6:56 am

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

    • Johnny K March 9, 2011, 7:45 pm

      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 May 21, 2013, 3:50 pm

        Most car mp3-players are supporting only mp3.

  • Alejandro Alcaide April 20, 2010, 10:27 pm

    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

  • Cool July 18, 2009, 11:05 pm

    Cool Script – Thanks

  • Chris F.A. Johnson June 22, 2009, 1:18 am

    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"
    • Vivek Gite June 24, 2009, 7:04 pm

      Thanks for sharing your wisdom :)

      • slotwek August 1, 2009, 11:29 pm

        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,

        • Ramon September 24, 2009, 5:44 pm

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

Security: Are you a robot or human?

Leave a Comment