Lossless Listening

Command Line – FLAC

© Photo by Alphacolor 13 on Unsplash

© Photo by Alphacolor 13 on Unsplash

Article from Issue 207/2018
Author(s):

With a little effort, you can create digital audio files with CD quality sound.

Many music listeners content themselves with the MP3 format. However, for dedicated audiophiles, the preferred digital format is the Free Lossless Audio Codec (FLAC) [1]. FLAC files are supported by most hardware and music players, as well as editors like Audacity. However, creating FLAC files or even ripping to FLAC on Linux requires installation of the flac package for converting to and from FLAC. The package is available for most distributions but is not always installed by default.

Developed by the Xiph.Org Foundation [2], which also provides the reference implementation for the OGG formats [3], FLAC is preferred by music lovers for numerous reasons. To start with, it is licensed under the New BSD license [4], unencumbered by patents, and does not support digital rights management. In fact, the codec's website specifically states that "there is no intention to add any copy prevention methods." Also, being free-licensed, it is easy to convert to other formats and does not require vendor lock-in for users to continue to have access to a file's data.

Just as important are the features of FLAC files. If you compare the same track in MP3 (.mp3) and FLAC (.flac) formats, you will notice that the FLAC file can be as much as 250 percent larger. The main reason for this difference is that, while both formats compress audio information, MP3 is a lossy format [5], whereas FLAC is lossless [6]. In other words, when compressing audio information, MP3 discards content and replaces it with approximations that are acceptable to the ear, and FLAC does not. A lossy format might be preferable when transmitted over limited bandwidth or acceptable when played in a noisy environment, such as on a busy street, but if your priority is the faithfulness of sound reproduction, then a lossless format like FLAC offers better playback. A decade ago, lossy formats were popular because storage was limited on handheld music players. Today, with the availability of 250GB microSD cards and players with high-end digital-to-analog converters (DACs) [7], lossless formats like FLAC are becoming more popular than ever. Independent music labels, for instance, routinely offer FLAC downloads per fanbase requests.

Additionally, FLAC offers other advantages. For example, among lossless formats, FLAC offers the best ratio of compression to speed in decoding for play [8]. The information to decode each frame is contained within the frame, rather than being dependent on other frames. In its OGG FLAC format (.oga), unlike other lossless formats, FLAC thereby limits the damage to the frame in which transmission errors occur. Still another advantage is that FLAC supports tags, album art, seek tables, and cue sheets. Free-licensed, high-quality, and efficient, FLAC sets a standard that no other audio format currently in widespread use can match.

FLAC Encoding and Decoding

The flac command installs with an incomplete man page. To get a summary of the command's options, type flac -h. To get a full explanation of available options, type flac -H. Once a system has the flac package installed, the command structures used in a shell can also be added to audio editors like K3B, which are a front end to the command, to act as the default command for ripping CDs.

To encode a file to FLAC or decode a .flac file to another format, the basic command structure is:

flac [<general-options>] [<encoding/format-options>] [FILE1] [FILE2]

Unless the --decode (-d) option is specified, flac assumes it is encoding to FLAC. The source file(s) must be in another lossless format (e.g., PCM WAVE (WAV), RF64, AIFF, AIFF-C, or a raw audio file); otherwise, the conversion would be pointless, because flac cannot add information that was discarded in making a lossy file.

During processing, flac lists any warnings or errors, the size of the output file, and the compression ratio achieved. The default is to create a .flac file, but the command can be made to create an Ogg FLAC file with --ogg or another lossless format with the options --force-raw-format, --force-aiff-format, --force-rf64-format, and --force-wave64-format. You will need to use an audio editor or converter to create a copy of a .flac file in a lossy format. Somewhat surprisingly, adding other options does not change this basic output to any degree unless a warning or error occurs (Figure 1).

Figure 1: The flac command's default behavior is to encode. The command gives solutions for any warnings or errors, in case you want to recreate the file.

Whether you are encoding or decoding, any number of files can be converted at the same time by adding them at the end of the command in a space-separated list. By default, the flac command writes each input file to a new file with the same name but a .flac extension. If you want to change the name of a single file, add --output-name=FILENAME (-o=FILENAME). Multiple files can be renamed and prefixed with the option --output-prefix=STRING. An output string can also be used for outputting to a different directory, so long as the string ends with a forward slash (/).

When encoding, you also have the option of using --verify (-V), which decodes while encoding and compares the two results to check for errors. Warnings do not stop processing, but when decoding, you can add --decode-through-errors (-F) to change the flac default behavior of stopping decoding when an error occurs. Be aware, though, that decoding through errors can result in missing samples or silent sections in the output file. If you choose, you can also add the general option --delete-input-file when encoding or decoding. Better yet, for safety, combine --delete-input-file with --warnings-as-errors (-w), which will stop the creation of the output file but leave the input file untouched, because it is only deleted when the output file is successfully created.

Most CDs consist of a single file, typically in WAV format, with individual tracks marked by seek points. Unlike audio editors like abcde [9], flac cannot rip the CD file into separate tracks in a single operation. However, you can use the --until option to stop the creation of an output file at a certain point; --until can be completed by a track number, so that --until=2 includes only the first two tracks in the output file. Alternatively, if you have an album cover or a CD label that lists the length of songs, you can complete the option by specifying a time using the mm:ss:ss format (minutes:seconds:fraction of a second), so that --until=03:58:00 would limit the output to just under the first four minutes of the CD file. You can use the same completions with the --skip option to begin the output file at a particular track.

However, the most important options are probably the compression options. The flac command includes nine compression levels, the lowest being --compression-level-0 (-0 or --fast) and the highest being --compression-level-8 (-8 or --best). All these compression levels are collections of several other options with different settings, including

  • --blocksize (-b)
  • --mid-side (-m)
  • --adaptive-mid-side (-M)
  • --rice-partition-order (-r)

plus some of the Tukey functions available with --apodization (-A). For example, according to the -H option, compression level 0 is synonymous with -l 0 -b 1152 -r 3. However, this jargon probably means as little to most users as it does to me (without further research) and takes longer to enter. For most users, most of the time, what matters is that the compression level can reduce the size of .flac files by almost a third and that the degree of compression makes no observable difference when a .flac file is played back on local equipment, but might be noticeable when streaming – something that would require experimentation even if you were using the individual options. For most users, the compression level options should usually be enough.

Other Options

For many users, the basic options for encoding and decoding .flac files is all they need. However, some users may choose to use options for pictures and metadata tags.

Users who want to recreate digitally the LP or CD album experience may choose to store pictures as part of a .flac file. These pictures will display while the .flac is playing. The full command structure for a picture is:

--picture=TYPE MIME-TYPE DESCRIPTION PIXEL-WIDTHxHEIGHTxCOLOR-DEPTH COLORS FILE

Only the file needs to be specified. The picture types are listed in Table 1. If some parts of the specification are optional, FLAC uses its defaults; the type, for instance, defaults to 3, which is the front cover. Other options, such as MIME type, dimensions, or colors will be read from the file if not specified. Multiple pictures can be added to the file, although only one each of types 1 and 2.

Table 1

Picture Types

Type

Description

0

Other

1

32x32p "file icon" (PNG only)

2

Other file icon

3

Cover (front)

4

Cover (back)

5

Leaflet page

6

Media (e.g., label side of CD)

7

Lead artist/lead performer/soloist

8

Artist/performer

9

Conductor

10

Band/orchestra

11

Composer

12

Lyricist/text writer

13

Recording location

14

During recording

15

During performance

16

Movie/video screen capture

17

A brightly colored fish

18

Illustration

19

Band/artist logotype

20

Publisher/studio logotype

The .flac files use the same system for metadata tags as Xiph.org's Ogg formats. When encoding to .flac format, users can use the option --keep-foreign-metadata to preserve the tags used by other systems, such as the one used in WAV or AIFF files. Unless --no-utf8-convert is also used, existing metadata will be converted to UTF-8, which is usually not a problem but might conceivably cause some unexpected characters to display. Both these options will affect all tags specified in the command after them.

You can add additional tags during encoding with the option --tag=FIELD=VALUE (-T), using one option for each new tag. After you create a .flac file, you can use metaflac, a utility installed with the FLAC codec, to add tags using the option --set-tag=NAME=VALUE or to remove them with --remove-first-tag=NAME or --remove-all-tags. The metaflac command can also perform many of the functions of flac, including importing a picture with --import-picture-from= and suppressing the conversion of tags to UTF-8 with --no-utf8-convert.

Worth the Wait

As the compression example shows, the FLAC codec offers many more options, many of which are of interest mainly to audio experts. However, the Xiph.org Foundation takes its obligation as a reference implementation seriously, and the .flac format is easy for anyone to begin to use.

Of course, like any command, flac requires some effort to master. However, if music is important to you, the results are worth the effort: digital files with CD quality sound. Once you have heard the difference that a lossless format like FLAC can make, you may never be satisfied with MP3 files again.

linux-magazine.com | Linuxpromagazine.com

The Author

Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest coast art. You can read more of his work at http://brucebyfield.wordpress.com

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Security Issue with FLAC Audio Codec

    Loss free audio codecs are gaining in popularity with device manufacturers; right now, a vulnerability in the Free Lossless Audio Codec (FLAC) spoils users listening pleasure.

  • Supporting Role

    With the addition of its supporting utilities, Ogg Vorbis competes with other free audio formats.

  • Free Software Projects

    If you want to convert a bunch of audio files into another format and prefer to steer clear of the command line, the Gnac graphical tool might do the trick. Also, the Gourmet Recipe Manager helps out in the kitchen.

  • Sound Conversion

    Transfer the contents of audio CDs to your PC and convert the contents of music folders with soundKonverter and fre:ac.

  • MystiQ

    Working with multimedia files usually involves converting from one format to another. With MystiQ you can handle this task in next to no time.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News