JPEG XL: Encoding, decoding and animation with FFmpeg

Introduction

JPEG XL (.jxl) represents the future of image compression, offering superior quality at reduced file sizes. Since version 5.1 (with full animation support added in late 2024/early 2025), FFmpeg natively integrates this format via the libjxl library.

This guide details the essential commands to leverage this modern codec for both static images and animations, including a dedicated section on lossless workflows.

Technical Prerequisites

Before starting, ensure your FFmpeg installation is compatible:

1. Encoding Static Images

Converting to JPEG XL significantly reduces PNG or JPG file sizes while preserving visual quality.

1.1 Standard Lossy Encoding

ffmpeg -i input.png -c:v libjxl output.jxl

1.2 Lossless Encoding (Static)

For archival or pixel-perfect requirements, set -distance to 0. This forces the encoder into Modular mode, ensuring bit-exact reconstruction.

ffmpeg -i input.png -c:v libjxl -distance 0 -effort 9 output_lossless.jxl

2. Decoding and Conversion

FFmpeg handles conversion from JPEG XL to standard formats seamlessly, preserving color space and bit depth.

ffmpeg -i image.jxl output.png

3. Creating JPEG XL Animations

Replace heavy GIFs with lightweight, high-color-depth animated JPEG XL files. This requires the libjxl_anim codec and the image2pipe format.

3.1 Standard Lossy Animation

ffmpeg -i video.mp4 -c:v libjxl_anim -f image2pipe -distance 0.5 -effort 5 animation.jxl

3.2 Lossless Encoding (Animation)

Creating a truly lossless animation (e.g., for screen recordings or pixel art) follows the same logic as static images but uses the animation codec.

ffmpeg -i source.mov -c:v libjxl_anim -f image2pipe -distance 0 -effort 9 animation_lossless.jxl

Performance Tip: Lossless animation encoding is computationally intensive. Using -effort 9 may be slow for long videos. If speed is critical, -effort 7 offers a good compromise with minimal size increase.

4. Summary of Key Parameters

Parameter

Lossy Value

Lossless Value

Description

Codec

libjxl / libjxl_anim libjxl / libjxl_anim Select based on static vs. animated.

Distance

0.5 – 1.0 0.0 0.0 triggers bit-exact lossless mode.

Effort

4 – 7 9 Maximize effort (9) for best lossless compression.

Format

(Optional) image2pipe Mandatory for animations.

Conclusion

JPEG XL combined with FFmpeg offers a robust, future-proof alternative to GIF and WebP, delivering both high-efficiency lossy compression and true lossless capabilities in a single format.

FFmpeg Options Explanation

-distance: Controls the visual quality (fidelity) of the image.

-effort: Controls the compression speed vs. file size trade-off (how hard the encoder works).

Note: Unlike -distance, increasing -effort never reduces quality; it only increases the time required to encode.

Regarding Alpha (Transparency): If your input image has transparency (like a PNG), JPEG XL preserves it automatically. There is a separate parameter, -alpha_distance, if you wish to compress the transparency channel differently from the color data, but -distance only affects the color/visual fidelity.