Converting FLAC to MP3 in Linux

June 4th, 2008 by Strawp

A quick howto…

If you’re using a desktop debian distro, just use SoundConverter:

sudo apt-get install soundconverter

If you’re shelling into a server (or you just prefer CLI), you’ll need a script and a few audio tools. Here’s the bash script (based on this one):

#!/bin/bash

# Converts all flac files in a folder into mp3s, plus the id3 tag
# Requires flac, metaflac, lame, id3v2

for a in *
do
  # Check the file is a flac file
  if [[ "$a" =~ flac$ ]]
  then
    # Name of outfile
    OUTF=`echo "$a" | sed s/\.flac/.mp3/g`
    echo "$a => $OUTF"

    # Capture all the FLAC metadata
    ARTIST=`metaflac "$a" --show-tag=ARTIST | sed s/.*=//g`
    TITLE=`metaflac "$a" --show-tag=TITLE | sed s/.*=//g`
    ALBUM=`metaflac "$a" --show-tag=ALBUM | sed s/.*=//g`
    GENRE=`metaflac "$a" --show-tag=GENRE | sed s/.*=//g`
    TRACKNUMBER=`metaflac "$a" --show-tag=TRACKNUMBER | sed s/.*=//g`

    # Convert the audio data from FLAC to MP3
    flac -c -d "$a" | lame -m j -b 256 -s 44.1 - "$OUTF"

    # Tag the resulting MP3 with the captured metadata
    id3v2 -t "$TITLE" -T "$TRACKNUMBER" -a "$ARTIST" -A "$ALBUM" -g "$GENRE" "$OUTF"
  fi
done

Save that off in your path, make it executable:

chmod u+wrx flac2mp3

and run it from an location with mp3s in. It’ll convert all FLAC files to identically named MP3 files with the id3v2 information based on the FLAC metadata.

You’ll need flac, metaflac, id3v2 and lame:

sudo apt-get install flac id3v2 lame

Happy converting 🙂

3 Responses to “Converting FLAC to MP3 in Linux”

  1. alf Says:

    sneetchalizer is brilliant for this kind of thing…

  2. Strawp Says:

    Oh nice! Thanks for the heads up!

  3. Martin Says:

    Great script — thanks