Encoding basics
37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0Media compression is what makes streaming video, music libraries and photo-heavy web pages practical. Uncompressed 1080p video at 24 frames per second in 8-bit 4:2:0 takes roughly 600 Mbit/s; a good AV1 or HEVC encode of the same film looks almost identical at a few Mbit/s. This wiki explains how that works and how to get good results with the tools people actually use.
What is a codec
Section titled “What is a codec”A codec (coder/decoder) is a way of representing media in compressed form, defined by a specification. H.264, AV1 and Opus are codecs. It helps to keep three things apart:
- The format is the standard itself: what a valid compressed stream looks like and how to decode it.
- An encoder is a program that produces such a stream. One format can have many encoders of very different quality and speed: for AV1 there are SVT-AV1, aomenc, rav1e and hardware encoders in GPUs.
- A decoder turns the stream back into pictures or sound. For modern video codecs every conforming decoder produces bit-identical pictures from the same stream, so picture quality is decided at encode time.
Most media codecs are lossy: they throw away information you are unlikely to notice in exchange for much smaller files (see lossy compression). Lossless codecs reconstruct the input exactly and compress far less (see lossless compression).
Codecs exist for each kind of media:
- Video: H.264, HEVC, VP9, AV1, and lossless formats such as FFV1.
- Audio: Opus, AAC, MP3, and lossless FLAC.
- Images: JPEG, PNG, WebP, AVIF and JPEG XL. Image formats also have to handle bit depth, transparency, color profiles, HDR and metadata such as EXIF.
- General data: ZIP, zstd and similar compressors work on any data, not just media.
Containers
Section titled “Containers”A video file is usually a container holding several compressed streams: video, one or more audio tracks, subtitles, chapters and metadata. Matroska (MKV), MP4 and WebM are containers, not codecs. Changing the container (remuxing) copies the streams without re-encoding them; changing the codec (transcoding) decodes and re-encodes, which for lossy codecs always costs some quality.
The terminology page defines these and other terms in more detail.