Convert FLAC to MP3 on OS X for iTunes

Sorry. No Perl in this post, but this appears to be such a common problem that I've decided to write up a simple description of how to convert FLAC to run in iTunes.

FLAC is the "Free Lossless Audio Codec". It's a fantastic file format, but there are a couple of issues. First, songs typically run 25 to 40 megs per song. This means that they take up roughly 10 times the hard disk space as a corresponding MP3. For extremely large music collections this could be a problem.

The other problem is that Apple has chosen not to support FLAC for iTunes. I tried many solutions listed online, including Xiph and Fluke and got nowhere. Finally I decided it was time to go "old-school" and fall back to the command line.

I downloaded the FLAC tools and installed them. That gave me access to the flac program. Then I installed the LAME MP3 encoder. I used MacPorts for the latter, but you can get it from the Sourceforge link I provided.

Then I wrote this and saved it in my path as flac_to_mp3:

#!/bin/bash

WAV='.wav'
MP3='.mp3'

flac -d *.flac
for oldfilename in *$WAV
do
    newfilename="${oldfilename/%${WAV}/${MP3}}"
    lame "$oldfilename" "$newfilename" && rm "$oldfilename"
done

I just cd into the directory containing my flac files and run the above code (don't forget to chmod +x flac_to_mp3 for the above).

Takes your flac files, converts them to .wav, and then converts each of those to mp3 before deleting the .wav (if the conversion to mp3 was successful).

On a typical album, it's taking between 3 to 4 minutes to run on my MacBook Pro.

Please note that I'm not an audiophile, so I've no idea if the above is the best strategy. I only know that it works. Suggestions for improving it welcome.

6 Comments

I would use AAC rather than MP3, nowadays; and you can do without the uncompressed temporaries:

flac -dc $flac | faac -wo ${flac%.flac}.mp4 -

(iTunes/OSX may or may not prefer .m4a to .mp4). If you've got the information available some of the --artist, --title &c. options to faac might be handy, as well. If it's in the FLAC you can get it out with metaflac(1) (or, presumably, one of the FLAC modules on CPAN), but it may not be worth the effort.

I agree with Ben about the metadata. A simple conversion to WAV and re-encode to MP3 completely ignores the tag data. I don't like iTunes, in general, because so much of my music is in Ogg Vorbis and FLAC format and because I don't use an i-Anything to play my music. But, there are many times when I need to convert music from FLAC to MP3 (or Ogg).

A friend of mine, Scott Robertson, wrote a utility in Perl a few years ago called 'oggify' which converts whole directories of FLAC files to Ogg Vorbis (or to MP3). He's since ported it to Python (I still use the Perl version). You can download this utility and/or read more about it at http://scottr.org/oggify/.

I'm getting errors when I chmod +x flactomp3 , when is the command given? not sure but maybe I'm not putting in the code right. I had one of my developers come in and run this for me and he's having issues too. Conversion process is stuck and errors coming up.

Bonnie Smith COO/Director FXP http://www.forexpulse.com

I wrote this small program for the same problem:

https://gist.github.com/799328

It does more or less the same, but grabs the metadata and saves the files in another dir.

If I have music in FLAC that I want to play in macbook, I prefer to convert it to another lossless format. In ITunes only AppleLossless format is supported, so i use XLD. It has a GUI but it also can be used in a command line and it works great!

Thanks for this. Exactly what I needed, up and running on conversion in 3 minutes.

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/