Shell script
Convert to AVIF Programmatically
This shell script converts JPG and PNG images into AVIF and WebP using ffmpeg
and
Google’s WebP encoder.
# macOS brew install ffmpeg webp # Arch Linux sudo pacman -S ffmpeg webp
AVIF, besides producing smaller file sizes, they make optically better
images than cwebp
. No artifacts, sharper, and more accurate colors.
png | 250 kB |
webp | 112 kB |
avif | 72 kB |
#!/bin/sh set -o noglob IFS=$'\n' images=$(find ~/imgs -type f -name *.png -o -name *.jpg) for img in $images; do avif_out=$img.avif webp_out=$img.webp if [ $img -nt $avif_out ]; then ffmpeg -i "$img" -c:v libsvtav1 -svtav1-params avif=1 "$img" "$avif_out" fi if [ $img -nt $webp_out ]; then cwebp $img -o $webp_out fi done
The images end up with the original extension plus the modern one (e.g. foo.png.avif) because I need those file names to Conditionally Serve the Optimal Image Format with Nginx.