Sunday, 29 January 2012

Converting Flac to Mp3 (or other formats)

Sometimes you need to have .mp3 files for compatibility reasons (mp3 player?). Here's an easy way to convert Flac (or any other formats readable by ffmpeg, m4a, wav, whatever else) to Mp3 on Linux (or windows using Cygwin or ffmpeg for windows... which is hard to find). First, install lame codecs and ffmpeg
ubuntu: sudo apt-get install lame ffmpeg libavcodec-extra-53 
Then cd to the directory with flac files and use find (or change ffmpeg for avconv):
find -name "*.flac" -exec ffmpeg -i {} -acodec libmp3lame -ab 128k {}.mp3 \;
Done! You now have the exact same filenames with .mp3 encoding (also adds .mp3 at the end but keeps the .flac as well).
  1. Obviously, change *.flac to whatever extension your input files are in
  2. You can also change the output codec by replacing libmp3lame to whatever codec you'd like
  3. Heck, just use man find ;)
  4. If you don't like the .flac.mp3 thing, you can rename easily (I like it since it tells me it came from a lossless source, which is likely sitting somewhere on my hard drive):
    rename 's/\.flac//' *.mp3

Alternative without ffmpeg

If you don't have ffmpeg and prefer using lame and flac directly, first install the codecs if you dont have them already.
Ubuntu: sudo apt-get install flac lame 
Arch: sudo pacman -Syu flac lame
Then, convert away!
find . -name '*.flac' -exec sh -c 'flac -cd "{}" | lame - "{}".mp3' \;
You can also do this in the current folder only with a for-loop:
for f in *.flac; do flac -cd "${f}" | lame - "${f}.mp3"; done;
Set the target bitrate depending on the quality/size you prefer with the -b option of lame:
lame -b 320 - "${f}.mp3"


  1. NB: don't forget the 'k' on the bitrate, eg `-ab 128k`

    1. I'm not sure about your version of ffmpeg but on my system the 'k' isn't necessary.

    2. I still updated the post to add it. They both work.

  2. Very elegant, thanks for a neat solution.

  3. You may use
    ls *.flac|xargs -IQ ffmpeg -i Q -ab 320k Q.mp3

    Just a little shorter, nothing different to OP.

    1. Actually not quite the same. The find binary will also look in subfolders while ls won't without the -R. If you add the -R to your command it becomes essentially the same yes.

  4. This is turbo sweet man, thanks.

  5. ls *.flac|sed -e s/.flac//|xargs -IQ ffmpeg -i Q.flac -ab 320k Q.mp3

    That's even cleaner :)

  6. How would you use this code to scan all of home/Music/.....folders and sub folders ?

    ls *.flac|sed -e s/.flac//|xargs -IQ ffmpeg -i Q.flac -ab 320k Q.mp3

  7. This comment has been removed by the author.