FLAC: The Complete Guide to Free Lossless Audio Codec
FLAC (Free Lossless Audio Codec) is the de facto standard for lossless audio compression. Developed by Josh Coalson and released by Xiph.Org Foundation in 2001, FLAC compresses audio without any information loss — a decoded FLAC file is bit-for-bit identical to its PCM source. With compression ratios typically between 40–60% of the original size, FLAC is the primary format for audiophile archival, CD ripping, and high-resolution audio distribution.
Why Lossless Matters
Lossy codecs (MP3, AAC, Vorbis) permanently discard audio information deemed inaudible by psychoacoustic models. Once encoded lossy, the discarded data is gone forever. FLAC stores every sample identically to the original:
- Archives: Ripping a CD collection to FLAC creates a perfect digital archive. Re-encode to any lossy format later without generation loss.
- Mastering and production: Audio engineers require lossless formats throughout the production chain.
- High-resolution audio: 24-bit/96kHz, 24-bit/192kHz files from streaming services (Qobuz, Tidal HiFi) are delivered as FLAC.
- Quality assurance: Blind A/B testing between FLAC and the original is statistically indistinguishable by definition.
FLAC File Structure
A FLAC file consists of:
1. fLaC marker: 4-byte magic number 0x66 0x4C 0x61 0x43 (fLaC)
2. METADATA_BLOCK sequence (variable count):
- STREAMINFO (required, always first): 34 bytes — minimum/maximum block size, minimum/maximum frame size, sample rate (up to 655,350 Hz), number of channels (up to 8), bits per sample (4–32 bits), total samples, MD5 signature of the decoded audio stream
- PADDING: Optional zero-filled block for future tag edits without file rewrite
- APPLICATION: Embedded application-specific data with registered 4-byte ID
- SEEKTABLE: Array of seek points (sample number → byte offset + samples in frame) enabling O(1) seek to any point
- VORBIS_COMMENT: UTF-8 key=value metadata pairs (identical format to Ogg Vorbis comments). Common tags: TITLE, ARTIST, ALBUM, TRACKNUMBER, DATE, GENRE, REPLAYGAIN_TRACK_GAIN, REPLAYGAIN_TRACK_PEAK
- CUESHEET: CD table of contents, track indices, ISRC codes
- PICTURE: Embedded images (album art) with MIME type, width, height, color depth, color count, binary data
3. AUDIO FRAME sequence: Variable-size frames each encoding a block of samples
Audio Frame Internals
Each FLAC audio frame contains:
Frame header:
- Sync code: 14 bits (0x3FFE) + reserved + blocking strategy
- Block size: 4 bits (encoded or verbatim)
- Sample rate: 4 bits
- Channel assignment: 4 bits (independent, left/side, right/side, or mid/side stereo)
- Sample size: 3 bits
- UTF-8 encoded frame/sample number
- CRC-8 of header bytes
Subframes (one per channel):
- Subframe type determines the predictor:
- SUBFRAME_CONSTANT: All samples in block have same value (extreme silence)
- SUBFRAME_VERBATIM: Raw PCM, no prediction (useful for random noise)
- SUBFRAME_FIXED: Linear prediction with fixed coefficients (orders 0–4)
- SUBFRAME_LPC: Finite Impulse Response linear predictor with Burg-method or Levinson-Durbin computed coefficients (orders 1–32)
Frame footer: CRC-16 of entire frame
Linear Predictive Coding (LPC)
FLAC's compression core is LPC. Rather than storing raw samples, FLAC:
- Predicts each sample from previous samples using a linear combination:
predicted_n = sum(coefficients[i] * signal[n-i-1])for i=0..order-1 - Computes residuals:
residual[n] = actual[n] - predicted[n] - Encodes residuals with Rice coding: residuals are typically small numbers (near zero) regardless of the original amplitude, making Rice codes efficient
The predictor order (1–32) trades accuracy vs. decoder complexity. Higher orders predict music better (more redundancy removed) at the cost of more coefficient storage.
Rice Coding (Golomb-Rice Entropy Coding)
FLAC uses Rice codes to compress the prediction residuals:
- For each partition of residuals, a Rice parameter
kis chosen that minimizes the encoded size - Residuals are mapped to non-negative integers (positive values → even, negative → odd)
- Each value
nis encoded asfloor(n / 2^k)unary bits +n mod 2^kin binary - FLAC supports two Rice coding methods: Method 0 (parameters 0–14) and Method 1 (parameters 0–30)
The SEEKTABLE enables random access: without it, decoding would require reading from the beginning of the file (Rice-coded residuals are not independently decodable mid-stream without predictor state).
Compression Levels
FLAC offers 8 compression levels (0–8):
| Level | Preset | Block Size | LPC Order | Compression | Speed |
|---|---|---|---|---|---|
| 0 | Fastest | 1152 | FIXED 0–4 | ~55% | Very fast |
| 5 | Default | 4096 | LPC 8 | ~50% | Moderate |
| 8 | Best | 4096 | LPC 12 | ~45% | Slow |
The difference between level 0 and level 8 is typically only 10–15% additional compression at the cost of 10× slower encoding. Decoding speed is similar across all levels. Level 5 (default) is recommended for general use; level 8 for archival where encode time is acceptable.
Encoding and Decoding Commands
# Encode WAV to FLAC (default compression level 5)
flac input.wav -o output.flac
# Encode with maximum compression
flac -8 input.wav -o output.flac
# Decode FLAC back to WAV (verify losslessness)
flac -d input.flac -o output.wav
# Test integrity (decode in memory, verify MD5)
flac -t input.flac
# Encode with embedded album art
flac --picture="3||||cover.jpg" input.wav -o output.flac
# FFmpeg: convert to FLAC with 24-bit
ffmpeg -i input.wav -c:a flac -sample_fmt s32 output.flac
# FFmpeg: FLAC to MP3 V2 (transcoding from archive)
ffmpeg -i archive.flac -codec:a libmp3lame -q:a 2 output.mp3
# Batch rip CD tracks to FLAC with tags
for i in track*.wav; do
flac -8 "$i" --tag=ARTIST="Artist Name" --tag=ALBUM="Album Title"
done
ReplayGain
FLAC natively supports ReplayGain metadata stored in VORBIS_COMMENT tags:
REPLAYGAIN_TRACK_GAIN: dB adjustment to normalize individual track loudnessREPLAYGAIN_TRACK_PEAK: Maximum sample value (0.0–1.0)REPLAYGAIN_ALBUM_GAIN: dB adjustment for album-level normalizationREPLAYGAIN_ALBUM_PEAK: Maximum sample value across the album
ReplayGain targets -18 LUFS (older standard) or -23 LUFS (EBU R128 alignment). Applied by the player at playback time without modifying the audio data.
# Calculate and tag ReplayGain for an album
metaflac --add-replay-gain track01.flac track02.flac track03.flac
FLAC vs Other Lossless Formats
| Format | Compression | Bit Depth | Channels | Container | Notes |
|---|---|---|---|---|---|
| FLAC | 40–60% | 4–32 bit | Up to 8 | Native or Ogg | Open, royalty-free |
| ALAC | 40–60% | 16–32 bit | Up to 8 | MP4/M4A | Apple format, iTunes/iOS native |
| WAV | 0% | 8–32 bit | Up to 18 | RIFF | Universal, no metadata |
| AIFF | 0% | 8–32 bit | Up to 2 | IFF | Apple legacy, uncompressed |
| WMA Lossless | 50–60% | 16–24 bit | Up to 8 | ASF | Windows-only, DRM-capable |
| Wavpack | 40–65% | 8–32 bit | Up to 256 | WV | Hybrid lossy+lossless mode |
| Monkey's Audio (APE) | 50–70% | 8–32 bit | Up to 2 | APE | High compression, slow decode |
FLAC's advantages over ALAC: Open specification, cross-platform (including Linux, Android), broader software support. ALAC's advantage: Native iOS/iPod/iTunes support without conversion.
High-Resolution Audio
FLAC supports high-resolution formats natively:
- 24-bit/96 kHz: Standard "Hi-Res Audio" from streaming services. File size ~5× larger than CD-quality FLAC (~60 MB for a 4-minute track).
- 24-bit/192 kHz: Studio master resolution. ~120 MB for a 4-minute track.
- DSD-over-PCM (DoP): DSD64/DSD128 encoded into 24-bit PCM frames within FLAC for compatible DACs.
Sample rate support extends to 655,350 Hz, making FLAC suitable for ultrasonic research recordings.
Streaming and Seeking
The SEEKTABLE metadata block enables efficient random access — the player can jump to any time position by looking up the nearest seek point and decoding forward from that frame. Without a seek table, seeking requires linear scanning from the file start.
FLAC in Ogg container (OGG FLAC) uses Ogg page granule positions for seeking, compatible with Ogg streaming infrastructure. Native FLAC files are streamable but require the seek table for efficient seeking.
Related conversions
Common video conversions that pair well with this guide: