Converting FLAC to AAC

I wrote a script that converts a FLAC file to an AAC file, copying over all metadata. Why? Because iTunes doesn't support FLAC files (nor does anything else from Apple) but it's a good format to re-download tracks from.

Download the script here
Source Code

Pre-requisites

You will need faac and flac installed. If you use a mac with MacPorts then you can install them by running:

sudo port install faac flac

For Debian users, you can install them by running:

sudo apt-get install flac faac

You also need a working copy of Perl (should be installed by default).

Installing the script

Download the script and copy it to an appropriate directory. In this example, I will use /usr/local/bin - but you may put it elsewhere (e.g. ~/bin):

unzip convflac.zip
sudo mv convflac /usr/local/bin/
sudo chmod +x /usr/local/bin/convflac

To test if it works, type the command convflac:

$ convflac
No input flac file

Usage: convflac <filein.flac> [fileout.m4a]
If no fileout is specified, then filein.m4a will be created

Converting

The flac converter is as easy as this:

convflac "my song.flac"

This will convert "my song.flac" in to "my song.m4a". If there is meta information in the flac file, it will also copy that. You can specify the output file as well:

convflac "my song.flac" "aac/my song-converted.m4a"

Mass Converting

To convert an entire directory, a simple shell script will suffice:

for i in *.flac; do convflac "$i"; done;

Voila! Your entire directory should get converted to aac.

Advanced Tweaking

By default, convflac will sort out most metadata for you. However, you can customise this by editing ~/.convflac.

For example, FLAC files have the metadata tracknumber whilst AAC files call this track. To facilitate this conversion, we use a mutation map:

mutation_tracknumber = track

You can customise any FLAC value in this way - by default convflac will create a couple for you though, so you don't need to worry about track numbers or date meta data.

You can also pass arguments to faac and flac using the codec_faac and codec_flac options respectively. These will overrwrite any used by the script, so take great care with them.

Sample Configuration File

# convflac config file
# lines starting with # are comments, everything else is key=value

# Codec options - uncomment to make changes to quality settings
# Default is -q 300 -w -s for faac, it's recommended you leave flac alone
# See man flac and man faac for more info
#codec_faac = -q 300 -w -s
#codec_flac = -cd

# Mutation options (for tags):
#mutation_flacname = faacname
mutation_tracknumber = track
mutation_notes = comment
mutation_date = year

Troubleshooting

The most common problem is going to be the Perl directory. To fix this, type:

which perl

This should give you a directory - e.g. /usr/bin/perl - edit the convflac file to put it on the top line, prefixed with a "bang". For example:

#!/usr/bin/perl

This script almost certainly won't work on Windows and probably never will. I don't know enough about how Windows handles data pipes, but I'm going to assume it doesn't do it very well if at all.