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

# 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" 
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers ncdu pydf
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • Audiophile Dec 23, 2010 @ 6:56

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

    • Johnny K Mar 9, 2011 @ 19:45

      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 @ 15:50

        Most car mp3-players are supporting only mp3.

  • Alejandro Alcaide Apr 20, 2010 @ 22:27

    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 Jul 18, 2009 @ 23:05

    Cool Script – Thanks

  • Chris F.A. Johnson Jun 22, 2009 @ 1:18

    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 Jun 24, 2009 @ 19:04

      Thanks for sharing your wisdom :)

      • slotwek Aug 1, 2009 @ 23:29

        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 Sep 24, 2009 @ 17:44

          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 Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.