logo hsb.horse
← Back to snippets index

Snippets

Batch Convert Images to AVIF with avifenc

A Bash snippet that recursively scans jpg/jpeg/png files and converts them to .avif using avifenc.

Published: Updated:

A minimal script that converts all jpg/jpeg/png files under a directory to .avif. Uses find -print0 and read -d '' to handle filenames containing spaces.

#!/usr/bin/env bash
set -euo pipefail
while IFS= read -r -d '' file; do
avifenc "$file" -o "${file%.*}.avif" >/dev/null 2>&1
done < <(
find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \) -print0
)

Notes

  • If a .avif file with the same name already exists, it will be overwritten
  • To see conversion logs, remove >/dev/null 2>&1

Practical Note

This snippet fits well when I do not want to rewrite the same operation or check around bash, avifenc, image, conversion, cli over and over. Keeping it as a small helper makes the caller easier to read because the intent stays in the foreground.

If the branches and preconditions start growing, it is usually better not to force everything into one snippet. Splitting the steps and helper responsibilities is easier to maintain.