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).
Enjoy.
Notes:
  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"

15 comments:

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

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

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

      Delete
  2. Very elegant, thanks for a neat solution.

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

    Just a little shorter, nothing different to OP.

    ReplyDelete
    Replies
    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.

      Delete
  4. This is turbo sweet man, thanks.

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

    That's even cleaner :)

    ReplyDelete
  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

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This article was written by a real thinking writer without a doubt. I agree many of the with the solid points made by the writer. I’ll be back day in and day for further new updates. sbunoah ewe getsemane live mp3 download

    ReplyDelete
  9. The trunk has a higher exposure to heat and humidity that’s why it is not advisable to place it there. sa mp3 download

    ReplyDelete
  10. Amazing script. I easily understood everything. Very simply written, anyone can understand. Now I can convert FLAC to mp3 and convert mp3 to wav. Thanks for sharing the beautiful post.

    ReplyDelete