A codec (coder-decoder) is an algorithm used to compress video or audio for storage or transmission, and to decompress it for playback. Codecs are distinct from container formats: MP4 is a container, H.264 is a codec. The container determines the file structure; the codec determines how the video/audio data is encoded within it. The same H.264 video can be stored in MP4, MKV, or AVI containers.
Video codecs use temporal compression (differences between frames — most of a video doesn't change frame-to-frame, so only changes need to be stored) and spatial compression (redundancy within each frame, like JPEG-style compression). More sophisticated codecs analyze larger regions and longer time windows, finding more compression opportunities at the cost of encoding complexity and time.
Codec evolution: MPEG-2 (DVDs) → H.264/AVC (2003, HD video standard) → H.265/HEVC (2013, 40% better than H.264) → VP9 (Google, 2012) → AV1 (2018, 50% better than H.264). For compatibility: H.264 is universally supported. H.265 is widely supported on modern hardware. AV1 is the future but requires more processing power.