Commands for converting images or videos to AVIF/AVIFS。
Here is a avif animated AVIF. Only 700kb. It should playing well on Chromium.
Why AVIF? Because it has same size as JPEG but higher quality. Also AVIF supports animated pictures (the file extesnion is avif
or avifs
). The qaulity is better than GIF and the size is smaller than webp.
It is easy to convert jpeg, png, gif or whatever to avif with these commmand line tools!
1. Tools used#
OS:Arch Linux
All tools below are open-source and cross-platform. On Arch Linux they can be installed with pacman
.
Name | Description | Link |
---|---|---|
libavif | AVIF library | Github |
rav1e | AV1 encoder | Github |
aom | AV1 encoder | Google git |
imagemagick | Image processing tool | Official |
ffmpeg | Encoding tool | Official |
gpac | Encoding tool | Github |
av1an | Encoding tool | Github |
2. Convert GIF to AVIF#
Convert GIF to yuv and pass it to avifenc to transcode to avif.
ffmpeg -i input.gif -strict -1 -f yuv4mpegpipe -pix_fmt yuva444p - | avifenc --stdin output.avif
3. Convert JPEF/PNG to AVIF#
Convet jpeg to avif:
magick convert input.jpeg -quality 90% output.avif
Convet png to avif:
magick convert input.png -quality 50% output.avif
Batch convert png to avif:
magick mogrify -format avif -quality 90 -type truecolor -alpha on *.png
4. Convert WebP to AVIF#
After converting, the size is almost the same so it is a little bit meaningless.
magick convert input.webp -quality 50% output.avif
5. Convert WebP (animated) to AVIF#
There is no tool for directly convery animated webp to avif.
- Extract frames from webp:
magick convert input.webp -coalesce input%05d.png
- Create a mp4 file from extracted images and use libxaom for encoding.
ffmpeg -framerate 30 -pattern_type glob -i '*.png' -c:v libxaom -pix_fmt yuv420p output.mp4
- Convert mp4 file to avif:
MP4Box -add-image output.mp4:id=1:primary -new output.avifs
MP4Box -ab avis -ab msf1 -ab miaf -ab MA1B -rb mif1 -brand avis output.avifs
MP4Box -add output.mp4:hdlr=pict:ccst:name="GPAC avifs" output.avifs
6. Convert MP4/WebM videos to animated AVIF#
- Make sure the video coding format is AV1. If not, convert the coding format to av1 codec with av1an (increase
--cq-lelve
for a smaller video):
av1an -i "input.mp4" -v "--end-usage=q --cpu-used=6 --threads=8 --cq-level=30"
- Convert mkv to mp4 and delete audio tracks
ffmpeg -i input_aom.mkv -c copy -an output.mp4
- Convert mp4 file to avif:
MP4Box -add-image output.mp4:id=1:primary -new output.avifs
MP4Box -ab avis -ab msf1 -ab miaf -ab MA1B -rb mif1 -brand avis output.avifs
MP4Box -add output.mp4:hdlr=pict:ccst:name="GPAC avifs" output.avifs
7. Convert AVIF to JPEG/PNG#
AVIF to JPEG:
magick convert input.avif -quality 90% output.jpeg
AVIF to PNG:
magick convert input.avif -quality 90% output.png