replacing gif animations with avif
GIF really needs to die, because AVIF just destroys it in every way once you know how to do it.
The only reason anybody uses GIF at all is because you can create little animations with it. And by little, I mean little.
Above is a 3 second AVIF animation from the movie The French Connection. It's a good cop drama/thriller from 1971, and some guys like it because you get to see a Pontiac LeMans being thrashed around.
That image above in the way I did it has dimensions of 400x218, runs at 15 frames per second, and the total size is 72K.
If 72K sounds "heavy" to you, I can assure you it's not. I created the exact same image as a 1-bit black-and-white GIF, knocked down the frame rate to 5fps, and the file size was 186K.
Here is another clip, and this one is 9 seconds long:
This one is also 400x218 @ 15fps, and the total size is 189K.
And if you think that is heavy for size, I did some GIF tests with this one too.
At the same resolution of 400x218 with the frame rate knocked down to 10fps, an unoptimized 256 color GIF with "grain"/dithering is 3MB, and one also with 256 colors but optimized with pallette generation and all that is 5MB. Again, that's with the frame rate knocked down from 15 to 10.
GIF is such a pig. It really is.
How to create an AVIF animation
There's the 100% FFmpeg way which is slow to generate, then there's the HandBrakeCLI + FFmpeg + avifenc way that's much quicker to get the same thing.
FFmpeg way
This can fortunately but done as a one-liner, but you'll have to wait a while until it's done, even for really short clips.
This one can be done on Linux, Windows or Mac.
ffmpeg -i video.mp4 -ss 00:05 -to 00:10 -vf scale=400:-2,fps=15 output.avif
ss is the start time of the video, to is the end time, scale resizes the video to 400 pixels wide, -2 after that auto-calculates the height, fps sets the frame rate.
The waiting has nothing to do with processing power or lack thereof, but rather FFmpeg itself. It will eventually spit out that AVIF.
HandBrakeCLI + FFmpeg + avifenc way
This one as far as I know is Linux-only. Getting HandBrakeCLI and avifenc is easy, such as from Synaptic Package Manager. As for doing the same in Mac or Windows, probably not.
HandBrakeCLI is case sensitive. If you try handbrakecli, it won't work.
HandBrakeCLI -i video.mp4 -o handbrake.mp4 --start-at duration:35 --stop-at duration:5
This one gets slightly confusing. The --start-at part indicates where in the video file to start the clip. The --stop-at part is how long of a video clip you want from that starting point. This means that in the above example, HandBrakeCLI will read video.mp4, output as handbrake.mp4 with the clip beginning at 0:35 and ending at 0:40 since it's 5 seconds from 0:35 to 0:40. If you're confused, read it again until you understand because I can't explain it any better nor easier than that.
After that's completed, now we use FFmpeg:
ffmpeg -i handbrake.mp4 -vf scale=400:-2,fps=15 output.y4m
FFmpeg scales down the video to 400 pixels wide, auto-calculates height, sets frame rate to 15, and converts the MP4 to Y4M. Y4M is a format avifenc can read.
avifenc -q 30 output.y4m output.avif
avifenc reads output.y4m and converts to output.avif.
I use a quality of 30, as seen above. If you don't set quality, the default is 50, but if you're looking to compress this as much as possible before things start looking bad, 30 works. Sometimes you can get away with 20 or even 10 to shave off a few more bytes.
While true doing it this way takes more commands, you save a lot of wait time. FFmpeg takes a long time to seek to a certain part of a video, then takes even longer to render to AVIF after that. HandBrakeCLI on the other hand gets to the seek point fast, and avifenc also renders AVIF far quicker than FFmpeg does.
I would only use FFmpeg alone if I had no other choice. It's doable, but again, lots of wait time just to get your AVIF.
Converting a GIF to AVIF
This is doable with FFmpeg, but the conversion time is long.
The alternative is WebP. FFmpeg when converting GIF to WebP will crunch through the data very quick and output a WebP with good file reduction. As good as AVIF? No, but it's still pretty darned good.
Mass convert a bunch of GIF files to WebP
In Linux using FFmpeg:
for i in *.gif;do ffmpeg -i "$i" -loop 0 "${i%%.*}.webp";done
It is really important you remember that -loop 0 part, else the WebP animation will play and then stop.
AVIF is great, but you have to pick which camp to go with
AVIF is best, no question. But if you're in the situation where you have to convert a whole bunch of GIFs in the fastest way possible, then WebP is better.
You could use this for mass conversion from GIF to AVIF if you wanted to:
for i in *.gif;do ffmpeg -i "$i" "${i%%.*}.avif";done
...but you're going to be waiting a while. A long while.
In the end, it all depends on how long you're willing to wait if solely using FFmpeg.
In my situation, I didn't have too many animated GIFs to convert, so I just dealt with the wait time went with AVIF 100%.
Lastly, two questions answered that you may have.
Why use FFmpeg for resizing and setting frame rate when HandBrakeCLI can do that? Answer: HandBrakeCLI doesn't do dynamic resizing, and using --rate only sometimes works. When you tell FFmpeg to use a specific frame rate, it does it. When you tell HandBrakeCLI the same thing, it'll think about it, and if it determines for whatever reason it can't do that frame rate, the output will be a variable frame rate and jack everything up. In other words, FFmpeg does what it's told and HandBrakeCLI doesn't where frame rate is concerned.
What's the deal with Y4M? Answer: avifenc recognizes jpg, jpeg, png and y4m. That's it. Giving avifenc Y4M files is the easiest to deal with when doing this stuff.
Published 2025 Aug 21