Skip to main content

Introduction: FLAC, the Free Lossless Audio Codec

Posted on October 18, 2012 by Anthony James Founder/CEOAnthony James Founder/CEO

This article tells about FLAC: what is it, why use it and how to use it?
Note: in this article, you will encounter both FLAC and flac. The uppercase version is used to refer to the format, the lowercase version is used to refer to a flac file and the flac command.

What is FLAC?

As you might have guessed from the title of this article, FLAC is an abbreviation of Free Lossless Audio Codec. The first word (“free”) should be pretty clear (it’s an open-source project), but what is a “lossless audio codec”? Well, the well-known MP3 format is an audio codec. It is used to compress raw audio data. MP3 is a so-called “lossy” codec, meaning that, for example, if you would convert a wav file to an mp3, and then convert the mp3 file back to wav, you won’t end up with the same audio data. MP3 reduces the quality of the audio while encoding. On the other hand, FLAC is “lossless”. If you would convert a wav file to a flac, and then convert the flac file back to wav, you will end up with exactly the same wav file. Nevertheless, a flac file is a lot smaller than a wav file.

You could see a flac file as some kind of zip file (or, for the linux lovers, a .tar.gz file): if you compress some data with it and extract that data from the archive later on, you will just have your old data back. You won’t lose any “quality”. However, because FLAC is entirely optimized for audio data, it will compress audio a lot better than winzip or gzip. Also, flac is designed to be played directly. You don’t have to extract it, you can just tell your music player to play the flac.

Why use it?

Because FLAC gives you cd-quality playback at a lower filesize than, for example, wav. I am a regular FLAC user and as far as I have been working with it, it regularly features a compression rate of about 0.6, or, in other words, the flac file usually is 40% smaller than the wav file I encoded it from. Also, my favourite media players (MPlayer and xmms2) support it. The latter one even plays gapless!

How to use it?

You can both encode and decode a flac file, so the explanation is separated in those two aspects:

  • Encoding a flac file: I personally mainly use Sound Juicer to rip my cd’s to FLAC. It’s very easy to use and has got some useful features. However, it is not able to encode a wav file somewhere on your disc to FLAC. Although there is a range of frontends for flac-encoding, I use the commandline utility flac for encoding. Have a look at the following example:

    [rechosen@localhost ~]$ flac –best examplefile.wav

    This is about the easiest way to encode a wav file to flac. The flac program will start encoding examplefile.wav to examplefile.flac with options that should result in as much compression as possible (note: it will not remove the wav file afterwards). The encoding will take some time, depending on the speed of your system and the length of the audio file. Although you can shorten the time it takes to encode the flac file (by specifying –fast instead of –best, for example), this is not recommended. You will only have to encode the file once, and the better the encoding, the smaller the flac file. After encoding the file, you can play it as many times as you want. Note that playing is actually just a form of decoding.

  • Decoding a flac file (usually playing it): if you want to play a FLAC file, make sure the player you want to play it with supports flac. Some players do not support flac by default, but do once you installed the corresponding plug-in (for example, older versions of Winamp need this plug-in). When dealing with it on Linux, it usually depends on your distro whether you have FLAC support by default or not: some distros require you to install a plug-in, other already have it installed. You will mostly be able to find and install the right plug-in using your favourite package manager. If you just want to decode your flac file to a wav again, you can use the decoding function of the flac command:

    [rechosen@localhost ~]$ flac -d examplefile.flac

    This will decode the examplefile.flac to examplefile.wav, allowing you to use it with other programs that don’t use flac again. Note that the flac command will not remove the source file: after running this command, both examplefile.flac and examplefile.wav will exist.

The flac command features loads of options for tweaking the encoding and specifying locations. I’ll list a few of them here, embedded in examples:

  • The -o option:

    [rechosen@localhost ~]$ flac –best examplefile.wav -o alteredname.flac

    This option allows you to specify the outputfile. It also works when decoding:

    [rechosen@localhost ~]$ flac -d examplefile.flac -o alteredname.wav

  • The –skip and –until options:

    [rechosen@localhost ~]$ flac –best examplefile.wav –skip 06:31 –until 07:31

    This tells flac to encode just a part of the source file; from six minutes and 31 seconds to 7 minutes and 31 seconds. You can also specify fractions of seconds:

    [rechosen@localhost ~]$ flac –best examplefile.wav –skip 06:31.43 –until 07:31.78

    If you specify a” ” sign in front of the –until time, the specified time will become relative to the –skip time:

    [rechosen@localhost ~]$ flac –best examplefile.wav –skip 06:31.43 –until 01:00.35

    The above command will encode exactly the same part as the one before it. You can also specify the –until time relative to the end of the wav file (using a “-” sign):

    [rechosen@localhost ~]$ flac –best examplefile.wav –skip 06:31.43 –until -03:22.14

    If the –until time is before the skip time the flac command will return an error and exit. You can also specify the –skip and –until times in samples:

    [rechosen@localhost ~]$ flac –best examplefile.wav –skip 81920000 –until 163840000

    You cannot use the ” ” and “-” signs in this context, though.

  • The -c option:

    [rechosen@localhost ~]$ flac -c -d examplefile.flac

    This will decode the file examplefile.flac and send the wav output to the stdout, allowing you to pipe it to an other program. This comes in handy if you want to convert a flac file into an mp3 file:

    [rechosen@localhost ~]$ flac -c -d examplefile.flac | lame -h – examplefile.mp3

    Note: if you’re wondering what that “|” sign is doing there, please read the Piping data through commands section of my bash tutorial.

For further information, please refer to the manpage of the flac command.


Leave a Reply

Your email address will not be published. Required fields are marked *