Batch Convert AVIF Using ImageMagick
This article explains how ImageMagick utilizes the
libavif library as an underlying delegate to handle AV1
Image File Format encoding and decoding, enabling high-efficiency batch
conversions. You will learn the technical mechanics behind this
integration, how ImageMagick interfaces with libavif via
its modular architecture, and how to execute scalable batch conversion
workflows directly from the command line.
The Delegate Architecture
ImageMagick does not implement native AV1 codec algorithms directly
within its core codebase. Instead, it relies on an extensible delegate
system defined in configuration files such as
delegates.xml. When ImageMagick encounters an AVIF file or
is instructed to output to the .avif format, it calls
libavif—the open-source reference library developed by the
Alliance for Open Media (AOM).
During compilation or dynamic runtime loading, ImageMagick identifies
the presence of libavif. It registers the library to handle
MIME types and file signatures associated with AVIF
(image/avif). When an operation is executed, ImageMagick
translates its internal canvas and pixel representations into the
avifImage and avifEncoder structures defined
by the libavif API, handing off the heavy compression and
decompression tasks to the underlying codec (such as aom,
dav1d, or rav1e).
Verification of AVIF Support
Before executing batch processes, verify that your local ImageMagick
build has successfully linked libavif. Run the following
command:
magick identify -list format | grep -i avifIf integrated correctly, the output will display AVIF
with read (r) and write (w) capabilities.
How Batch Processing Works
ImageMagick facilitates batch processing primarily through two
approaches: the mogrify command for in-place or
directory-wide transformation, and the magick command
paired with shell scripting for fine-grained parallel processing.
1. Native
In-Place Batch Conversion with mogrify
The mogrify utility simplifies batch jobs by iterating
through file lists natively without external loops:
magick mogrify -format avif -quality 60 -define heic:speed=6 *.jpgIn this pipeline:
- ImageMagick loads each
.jpgsequentially using its standard JPEG delegate. - The image is converted into an internal RGBA pixel cache.
- ImageMagick routes the pixel data through
libavif. - The
-define heic:speedargument passes directly tolibavif's encoder configuration to adjust the CPU effort (ranging from 0 for slowest/best to 10 for fastest).
2. Multi-Core Batch Processing with Parallel Loops
Because AVIF encoding is computationally intensive, processing large
directories using standard shell loops alongside utilities like GNU
parallel maximizes CPU core utilization:
find . -name "*.png" | parallel -j $(nproc) magick {} -quality 65 -define heic:speed=4 {.}.avifEach thread spawns an independent ImageMagick instance that
interfaces with its own libavif encoder context, ensuring
total isolation, lower risk of memory fragmentation, and full saturation
of modern multi-threaded processors.
Handling Advanced AVIF Features
Through libavif, ImageMagick exposes several
format-specific controls that can be applied across batch sets:
- Lossless Compression: Adding
-define heic:lossless=truedirectslibavifto use mathematically lossless AV1 encoding. - Chroma Subsampling: Setting
-sampling-factor 4:2:0or4:4:4controls color compression, allowing users to preserve sharp color edges in graphics or reduce file sizes for photographic imagery. - Color Depth Management: ImageMagick handles
bit-depth translation (e.g., converting 8-bit source files to 10-bit or
12-bit AVIF containers) using the
-depthflag, providedlibavifwas compiled with high-bit-depth codec support.