Back to blog
October 19, 2026Sergei Solod13 min read

Best Audio Codecs and How to Build a Safe Perceptual Compression Pipeline

Compare Opus, AAC, MP3, FLAC, and ALAC for audio quality, compatibility, and compression efficiency, then learn how to validate FFmpeg encodes without losing metadata or corrupting batch results.

Audio CompressionOpusFFmpegPerceptual Audio QualityLossless Audio

Choosing the best audio encoding format is not a contest to find the smallest file. A useful decision has to balance perceived quality, playback compatibility, encoding time, latency, storage cost, and the information that must survive conversion. A codec can preserve the audible signal reasonably well while silently discarding artwork, multiple artists, loop markers, cue points, or application-specific metadata. A technically successful transcode is not necessarily a correct file conversion.

A practical default for efficient general-purpose delivery is Opus with variable bitrate. AAC remains a strong interoperability option, MP3 remains useful for older playback environments, and FLAC is the safer choice when preserving audio samples without loss matters more than size. Those are starting points, not universal winners. The harder engineering question is how to make an automatic compressor decide when it should not encode anything at all.

Which audio codec should you choose?

FormatCompressionStrongest use caseImportant limitation
OpusLossyEfficient speech, music, streaming, and low-latency audioContainer and target-device support must be checked; re-encoding can lose metadata
AAC-LCLossyBroad device and MP4 compatibilityEfficiency depends on the encoder and bitrate; low-rate variants need separate evaluation
MP3LossyOlder players, simple downloads, and established compatibilityUsually less efficient than modern codecs at comparable perceived quality
FLACLosslessArchiving, mastering handoff, and preservation of PCM samplesFiles remain substantially larger than perceptually encoded versions; metadata still needs care
ALACLosslessLossless audio within Apple-oriented workflowsIts MP4 container may hold structures that do not map cleanly to another format
WAV / PCMTypically uncompressed PCMEditing, interchange, test signals, and straightforward sample accessLarge files; WAV can contain important non-audio RIFF chunks

For a modern web application that can offer more than one source, Opus is a sensible primary encoding and AAC is a reasonable alternative where compatibility requires it. For an archive whose essential requirement is reversible sample preservation, keep the original and consider FLAC as an additional lossless representation. If an input is already MP3, AAC, or Opus, leaving it unchanged is often preferable to performing another lossy conversion. Every lossy generation can introduce new distortion, and a different codec does not undo information removed earlier.

The distinction between codec, encoder, and container matters. Opus is a codec; libopus is a widely used encoder implementation; Ogg, WebM, and supported MP4 configurations are possible containers. AAC is commonly carried in MP4/M4A. An .m4a extension does not tell you whether the audio is AAC or lossless ALAC. Inspect the actual streams rather than inferring quality or encoding from the filename. The MDN audio codec guide documents the format-versus-container distinction, while RFC 6716 defines Opus.

Why bitrate alone cannot establish audio quality

Bitrate measures how much encoded data is used per second, not how much audible information survives. Two encoders at the same bitrate can produce different artifacts. A quiet voice recording and dense, transient-heavy music also present different compression challenges. Comparing output sizes without identifying the content, codec, channel configuration, and encoder settings can be misleading.

  • Variable bitrate (VBR) allows the encoder to spend more bits on complex passages and fewer on easier ones. It is usually preferable to rigid constant bitrate when predictable network throughput is not the primary constraint.
  • Sample rate defines the frequency of samples in the decoded waveform. Upsampling a low-rate source does not recreate missing high-frequency information. Resampling can also complicate quality comparisons if reference and candidate are processed differently.
  • Bit depth describes PCM quantization precision, not the fidelity of a lossy codec. A high-bit-depth decode of an already damaged source cannot restore lost detail.
  • Channel layout is part of the signal. Mono downmixing may reduce size, but it is not an acceptable automatic optimization for material whose stereo field matters.
  • Latency, encoder delay, and padding matter for interactive playback, gapless transitions, and precisely looped audio. Duration alone does not reveal every alignment problem.

Useful starting points for experiments are approximately 128–192 kbit/s for stereo Opus music and substantially lower rates for mono speech, adjusted for the actual material and playback requirements. These figures are search parameters, not thresholds that guarantee transparency. For low-bit-rate streaming, technologies such as xHE-AAC warrant evaluation where their deployment environment supports them. Emerging neural audio codecs may be attractive for specialized speech or research systems, but a paper or a demonstration is not evidence that a codec is a drop-in replacement for a well-supported delivery format.

Use FFprobe to identify the source and FFmpeg to encode candidates

Start by identifying the actual audio stream, sample rate, channels, and duration. This avoids assuming that every MP4-like container contains audio or that every recognized extension is decodable.

ffprobe -v error -select_streams a -show_entries stream=codec_name,sample_rate,channels,channel_layout:format=duration -of json input.m4a

For an uncomplicated, permissioned WAV source whose extra chunks are known to be disposable, an illustrative high-quality Opus encode is:

ffmpeg -hide_banner -nostdin -i input.wav -map 0:a:0 -vn -c:a libopus -b:a 160k -vbr on -compression_level 10 -application audio output.opus

For broad MP4-compatible delivery, an AAC-LC candidate can be generated with the native FFmpeg AAC encoder:

ffmpeg -hide_banner -nostdin -i input.wav -map 0:a:0 -vn -c:a aac -b:a 192k output.m4a

For a lossless audio representation:

ffmpeg -hide_banner -nostdin -i input.wav -map 0:a:0 -vn -c:a flac -compression_level 8 output.flac

These are single-file demonstrations, not a safe batch conversion pipeline. They do not prove preservation of every metadata field, auxiliary stream, cue, or container chunk. For the lossless command, verify decoded PCM equivalence at a consistent sample representation before asserting a reversible audio conversion. Encoding with libopus, including its VBR and complexity options, is described in the FFmpeg codec documentation. Browser playback should be tested against the actual codec/container pair, not only the extension.

How to search for the smallest acceptable file

A perceptual optimizer resembles a constrained search problem: minimize output size while satisfying technical checks, a minimum quality criterion, and explicit preservation requirements. Its candidate space includes codec settings, bitrate, channel treatment, and encoder complexity. Smaller is not automatically better, and a low bitrate candidate should never be accepted merely because the encoder exited successfully.

  1. Record a SHA-256 fingerprint of the source and probe its streams and container structure.
  2. Determine whether lossy encoding is permitted. Keep existing lossy inputs and files with unsupported metadata intact by default.
  3. Encode a conservative Opus candidate, then try selected lower rates rather than blindly exploring every possible setting.
  4. Decode each candidate and verify that audio exists, the expected channels remain, timing is plausible, and decoding finishes without errors.
  5. Compare reference and candidate using appropriately aligned excerpts and a perceptual metric, while applying independent checks for stereo integrity, dropouts, clipping, and other failures that the metric may miss.
  6. Choose the smallest candidate that satisfies the policy and offers meaningful savings. Otherwise preserve the original byte-for-byte.
  7. Verify that the source has not changed during processing; publish only after validating the complete staged result.

A useful optimizer records COMPRESSED, PRESERVED, and FAILED as distinct outcomes. Preserving a file can be a correct optimization decision, not an error. A configurable minimum saving avoids spending quality and CPU resources for a negligible reduction in size.

What ViSQOL can measure—and what it cannot

ViSQOL is a full-reference perceptual quality estimator. It compares a reference waveform with a degraded waveform and returns a MOS-LQO estimate. This makes it useful for ranking candidate encodings, but its output is a prediction from a model, not a human listening test or a proof of transparency.

Mode and preprocessing matter. ViSQOL's audio mode expects 48 kHz input and reduces multichannel audio to mono for comparison. Its speech mode uses a different model and expects 16 kHz input. The audio model's upper predictions are approximately 4.75 rather than a universal perfect 5. Consequently, treating any one score—such as 4.45—as a scientifically established threshold for every recording is unjustified. Thresholds need calibration against the content and listening requirements.

Averages can hide local damage. A candidate may sound acceptable across most of a track yet distort one cymbal attack, a sharp consonant, or a brief transient. A more careful policy samples difficult passages and optionally runs overlapping windows across the entire file, retaining the worst relevant result rather than relying on the mean alone. Full coverage costs extra decoding and metric execution. Reference windows should be cached with a bounded disk budget so long recordings do not exhaust temporary storage.

Even a full scan does not solve everything: a mono-based score cannot prove preservation of stereo width or phase relationships, misalignment can bias the comparison, and some artifact classes are poorly represented by a particular model. Keep separate signal checks, and perform controlled listening or ABX tests on representative material before adopting a threshold as a production acceptance rule. In the available implementation work, automated integration checks exercised the ViSQOL interface using a test substitute; they did not establish real ViSQOL accuracy or listening transparency on a target macOS system.

Why correct audio bytes do not mean a correct output file

The most important failure pattern uncovered during development was container data loss masquerading as successful compression. Audio decoders commonly expose a playable stream while ignoring other structures. Transcoding that stream can produce a valid output that has silently discarded information the original owner expected to retain.

WAV and AIFF contain more than samples

RIFF/WAV files can include cue points, loop metadata, broadcast metadata, extra format fields, and unknown chunks. AIFF has its own marker and instrument-related structures. A file can decode perfectly while carrying non-audio bytes that will not survive a standard Opus encode. Conservative handling must inspect known-safe structures and preserve the complete original when a chunk cannot be safely classified. Even a block commonly used as padding can contain nonzero bytes; assuming every such block is disposable is unsafe when byte preservation is part of the contract.

FLAC can preserve structured information that Opus conversion cannot

FLAC supports metadata blocks for pictures, cuesheets, application data, seek tables, and Vorbis comments. Its comments can hold repeated keys. A controlled test with separate ARTIST=Alice and ARTIST=Bob entries demonstrated why an apparently successful conversion may be semantically wrong if the result contains only ARTIST=Alice;Bob. Those are not necessarily equivalent representations. A conservative converter should detect repeated keys and unsupported comment encodings, then preserve the original rather than silently flattening the structure. The FLAC format overview explains the metadata-block model.

MP4/M4A requires structural rather than tag-name checks

An M4A file may carry ordinary artist and title tags alongside private or unfamiliar atoms. Unknown atoms can appear at different levels, including inside otherwise familiar metadata boxes. Checking only the outer atom name is not enough: an unrecognized child or unexpected data representation can be lost even when the ALAC or AAC stream decodes cleanly. The safer policy is to recognize a deliberately narrow set of known-safe atom structures and preserve any ambiguous input without transcoding.

Likewise, a file with a recognized extension might carry only subtitles or video, or might have several streams. Such a file should not be forced through an audio encoder or discarded as a failed audio job. Preserve it as an ancillary file, subject to a valid container check and a source-integrity check. Do not infer an audio stream from an extension alone.

Build a batch optimizer as a transaction, not a file overwrite loop

The other major engineering lesson concerns publication. An optimizer should never destroy a previously good result because a later file fails, a process is interrupted, or a candidate looks smaller. The safe model is to treat the final directory as a managed artifact with provenance and a recoverable commit procedure.

  1. Preflight: confirm FFmpeg, FFprobe, required encoders and optional quality tooling. Resolve input and output paths before creating anything; reject containment through symbolic links.
  2. Inventory: enumerate files and directories, reject unsupported filesystem objects, and detect collisions under case-insensitive or Unicode-normalizing naming rules. Remember that maximum filename lengths are often measured in bytes, not characters.
  3. Stage: create candidates in a dedicated temporary workspace on the same filesystem as the eventual destination. Keep original files untouched.
  4. Verify: fingerprint source and output, validate the complete folder, and compare intended content against the staged manifest. Do not overlook a preservation branch simply because no encoding happened.
  5. Commit: use a lock, a transaction journal, controlled renames, and a verified backup to publish a complete folder. Multiple renames are not a single atomic transaction; recovery logic must understand intermediate states.
  6. Recover: restore only outputs whose ownership and hashes can be verified. Leave unknown or suspicious remnants in quarantine instead of deleting them automatically.

Manifest files are part of the attack and failure surface. They must be bounded in size, parsed defensively, and opened as regular files without following symlinks or blocking indefinitely on named pipes. Map keys and restored paths also need validation: an innocent-looking JSON value must never cause a recovery operation to escape the chosen output root. Special JavaScript property names should not be stored in unguarded prototype-bearing dictionaries.

Finally, source files can change during a lengthy encode. Hashing at discovery time alone is insufficient. Verify the same initial fingerprint again when copying an unchanged source, when a perceptual candidate is rejected, and immediately before publication. A controlled fault-injection test showed why: replacing an input after it had been inspected could make an earlier implementation report a successful preserved file even though the published bytes came from a different source state. The corrected rule rejects that run instead of publishing a misleading result.

Validation should test failure paths as aggressively as codecs

Codec tests should include ordinary WAV and FLAC, mono and stereo, and material with distinct transients. Structural tests should include duplicate FLAC comments, nested MP4 metadata, WAV/AIFF auxiliary chunks, files with no audio stream, long names, output-name collisions, and files that change while processing. Recovery tests should inject an interruption between successive rename operations and confirm that an earlier valid result is either retained or restored.

Do not mistake a collection of green tests for a listening-quality result. The engineering work discussed here included actual FFmpeg encode/decode checks and deliberately malformed or unusual test fixtures, as well as mocked perceptual-metric integration tests. Those checks provide evidence for particular safety properties, not a statistically representative codec comparison, universal container coverage, or validation on every operating system. The same distinction should appear in the reports produced by any audio optimizer: a decoded file, a perceptually measured file, and a human-audited file are three different levels of confidence.

A practical decision rule for production audio

For new, disposable delivery copies made from trustworthy lossless masters, begin with high-quality VBR Opus and validate the result against realistic listening conditions. Offer AAC where the delivery environment needs it. Keep MP3 when compatibility justifies it. For originals, editing assets, and files containing irreplaceable non-audio structure, preserve the existing file and use FLAC only where a lossless conversion can be verified against the preservation contract. Avoid lossy-to-lossy transcoding by default.

The most reliable optimizer is therefore not the one that compresses every file. It is the one that can explain, for each input, why it selected a codec, why the output passed the applicable quality and integrity checks, and why declining to recompress was sometimes the only correct result.