使用 FFmpeg 制作高质量 GIF
About two years ago, I tried to improve the support of the GIF encoding in FFmpeg to make it at least decent. This notably led to the addition of the transparency mechanism in the GIF encoder. While this is not always optimal depending on your source, it is in the most common cases. Still, this was merely an attempt to prevent shaming the encoder too much.
大约两年前,我尝试改进 FFmpeg 对 GIF 编码的支持,使其至少达到“尚可”的水平。这尤其促成了在 GIF 编码器中加入透明机制。虽然这并非在所有情况下都是最优,但在最常见的情形下效果不错。尽管如此,这只是一次防止让编码器过于丢脸的尝试。
But recently at Stupeflix, we needed a way to generate high quality GIF for the Legend app, so I decided to work on this again.
但最近在 Stupeflix,我们需要为 Legend app 生成高质量 GIF,因此我决定再次着手这项工作。
All the features presented in this blog post are available in FFmpeg 2.6, and will be used in the next version of Legend app (probably around March 26th).
本文展示的所有功能都已包含在 FFmpeg 2.6 中,并将用于下一版 Legend app(大概 3 月 26 日左右)。
TL;DR: go to the Usage section to see how to use it.
TL;DR:前往 Usage 部分查看如何使用。
Initial improvements (2013)
初始改进(2013)
Let's observe the effect of the transparency mechanism introduced in 2013 in the GIF encoder:
让我们观察一下 2013 年在 GIF 编码器中引入的透明度机制的效果:
ffmpeg -v warning -ss 45 -t 2 -i big_buck_bunny_1080p_h264.mov -vf scale=300:-1 -gifflags -transdiff -y bbb-notrans.gif
ffmpeg -v warning -ss 45 -t 2 -i big_buck_bunny_1080p_h264.mov -vf scale=300:-1 -gifflags +transdiff -y bbb-trans.gif
ls -l bbb-*.gif
-rw-r--r-- 1 ux ux 1.1M Mar 15 22:50 bbb-notrans.gif
-rw-r--r-- 1 ux ux 369K Mar 15 22:50 bbb-trans.gif
This option is enabled by default, so you will only want to disable it in case your image has very much motion or color changes.
此选项默认启用,因此只有在图像包含大量运动或颜色变化时才需要禁用它。
The other implemented compression mechanism was the cropping, which is basically a way of allowing to redraw only a sub rectangle of the GIF and keep the surrounding untouched. In case of a movie, it is rarely very useful. I will get back to this later on.
另一种已实现的压缩机制是裁剪,本质上就是允许只重绘 GIF 的一个子矩形,并保持周围区域不变。对于电影来说,这很少非常有用。稍后我会再谈这一点。
But anyway, since then, I didn't make any progress on it. And whil...