4 FFmpeg Tips: convert animated GIF to MP4 and back to animated GIF

Reducing the file size, keep quality & side-by-side collated gifs

·

3 min read

4 FFmpeg Tips: convert animated GIF to MP4 and back to animated GIF

Abstract

This tutorial provides a step-by-step guide on how to use FFmpeg, a free and open-source command-line tool, to convert an animated GIF to MP4 and back to animated GIF. The goal is to reduce the file size of the animated GIF while maintaining the same quality. The tutorial covers how to use FFmpeg to convert the GIF to MP4, reduce the file size of the MP4 by increasing the frame rate (video speed), and convert the MP4 back to an animated GIF. The tutorial also covers how to create an animated GIF by combining two side-by-side input GIFs using FFmpeg. The tutorial concludes by discussing potential reasons for the loss of quality when converting an animated GIF to MP4 and back to GIF.

Learn to convert animated GIF to MP4 and back to animated GIF. The goal is to reduce the file size of the animated GIF while maintaining the same quality.

1. Animated GIF ⟶ MP4

source: original gif, input: rt1_vs_baseline.gif

```
ffmpeg -i rt1_vs_baseline.gif -pix_fmt yuv420p -vf scale=640:-1 -r 10 -movflags +faststart rt1_vs_baseline.mp4
```

shows the original GIF (rt1_vs_baseline.gif, 11.5 MB).

2. Convert MP4 ⟶ animated GIF

- mp4 video ⟶ palette

ffmpeg -i rt1_vs_baseline.mp4 -vf fps=10,scale=640:-1:flags=lanczos,palettegen palette.png

- palette ⟶ animated GIF:

ffmpeg -i rt1_vs_baseline.mp4 -i palette.png -filter_complex "fps=10,scale=640:-1:flags=lanczos[x];[x][1:v]paletteuse" new_rt1_vs_baseline.gif

comparison of file sizes of the original GIF (rt1_vs_baseline.gif, 11.5 MB) and the new GIF (new_rt1_vs_baseline.gif, 2.3 MB).

3. Create an even smaller GIF

Convert animated GIF ⟶ MP4

ffmpeg -i rt1_vs_baseline.gif -pix_fmt yuv420p -vf scale=640:-1 -r 10 -movflags +faststart rt1_vs_baseline2.mp4

Reduce the file size of mp4 video by increasing the frame rate (video speed)

ffmpeg -i rt1_vs_baseline2.mp4 -filter:v "setpts=0.5*PTS" reduced_rt1_vs_baseline.mp4

Convert MP4 ⟶ animated GIF

- mp4 video ⟶ palette

ffmpeg -i reduced_rt1_vs_baseline.mp4 -vf fps=10,scale=640:-1:flags=lanczos,palettegen palette2.png

- palette ⟶ animated GIF

ffmpeg -i reduced_rt1_vs_baseline.mp4 -i palette2.png -filter_complex "fps=10,scale=640:-1:flags=lanczos[x];[x][1:v]paletteuse" new_rt1_vs_baseline2.gif

shows the new GIF (new_rt1_vs_baseline2.gif, 1.4 MB). shows the new GIF (new_rt1_vs_baseline2.gif, 1.4 MB).

4. Create a GIF with two side-by-side input animated GIFs

```bash
ffmpeg -i new_rt1_vs_baseline.gif -i new_rt1_vs_baseline2.gif -filter_complex hstack side_by_side_animated_gifs.gif
```

showing the new GIF (side_by_side_animated_gifs.gif, 6.4 MB).

Conclusion

The goal was to reduce the file size of the animated GIF while maintaining the same quality. The new GIF (new_rt1_vs_baseline2.gif, 1.4 MB) is 87.5% smaller than the original GIF (rt1_vs_baseline.gif, 11.5 MB). In the end, the new GIF (side_by_side_animated_gifs.gif, 6.4 MB) lost some quality. Some possible reasons are:

  • the original GIF (rt1_vs_baseline.gif, 11.5 MB) was already compressed

  • the new GIF (new_rt1_vs_baseline2.gif, 1.4 MB) was shortened by reducing the frame rate (video speed)

  • the new GIF (side_by_side_animated_gifs.gif, 6.4 MB) was shortened by reducing the frame rate (video speed) and by reducing the size of the GIF

Thanks for reading! If you have any questions or improvements, please let me know in the comments below.