ffmpeg
example
183k
ffmpeg
can output high quality GIF. Before you start it is always recommended to use a recent version: download or compile.
ffmpeg -ss 30 -t 3 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
- This example will skip the first 30 seconds (
-ss 30
) of the input and create a 3 second output (-t 3
).
- fps filter sets the frame rate. A rate of 10 frames per second is used in the example.
- scale filter will resize the output to 320 pixels wide and automatically determine the height while preserving the aspect ratio. The lanczos scaling algorithm is used in this example.
- palettegen and paletteuse filters will generate and use a custom palette generated from your input. These filters have many options, so refer to the links for a list of all available options and values. Also see the Advanced options section below.
- split filter will allow everything to be done in one command and avoids having to create a temporary PNG file of the palette.
- Control looping with
-loop
output option but the values are confusing. A value of 0
is infinite looping, -1
is no looping, and 1
will loop once meaning it will play twice. So a value of 10 will cause the GIF to play 11 times.
Advanced options
The palettegen and paletteuse filters have many additional options. The most important are:
stats_mode
(palettegen). You can force the filters to focus the palette on the general picture (full
which is the default), only the moving parts (diff
), or each individual frame (single
). For example, to generate a palette for each individual frame use palettegen=stats_mode=single
& paletteuse=new=1
.
dither
(paletteuse). Choose the dithering algorithm. There are three main types: deterministic (bayer
), error diffusion (all the others including the default sierra2_4a
), and none. Your GIF may look better using a particular dithering algorithm, or no dithering at all. If you want to try bayer
be sure to test the bayer_scale
option too.
See High quality GIF with FFmpeg for explanations, example images, and more detailed info for advanced usage.
Also see the palettegen and paletteuse documentation for all available options and values.
ImageMagick convert
example
227k
Another command-line method is to pipe from ffmpeg
to convert
(or magick
) from ImageMagick.
ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v pam -f image2pipe - | convert -delay 10 - -loop 0 -layers optimize output.gif
ffmpeg
options:
-vf "fps=10,scale=320:-1:flags=lanczos"
a filtergraph using the fps and scale filters. fps sets frame rate to 10, and scale sets the size to 320 pixels wide and height is automatically determined and uses a value that preserves the aspect ratio. The lanczos scaling algorithm is used in this example.
-c:v pam
Chooses the pam image encoder. The example outputs the PAM (Portable AnyMap) image format which is a simple, lossless RGB format that supports transparency (alpha) and is supported by convert
. It is faster to encode than PNG.
-f image2pipe
chooses the image2pipe muxer because when outputting to a pipe ffmpeg
needs to be told which muxer to use.
convert
options:
-delay
See Setting frame rate section below.
-loop 0
makes infinite loop.
-layers optimize
Will enable the general purpose GIF optimizer. See ImageMagick Animation Optimization for more details. It is not guaranteed that it will produce a smaller output, so it is worth trying without -layers optimize
and comparing results.
Setting frame rate
Set frame rate with a combination of the fps filter in ffmpeg
and -delay
in convert
. This can get complicated because convert
just gets a raw stream of images so no fps is preserved. Secondly, the -delay
value in convert
is in ticks (there are 100 ticks per second), not in frames per second. For example, with fps=12.5
= 100/12.5 = 8 = -delay 8
.
convert
rounds the -delay
value to a whole number, so 8.4 results in 8 and 8.5 results in 9. This effectively means that only some frame rates are supported when setting a uniform delay over all frames (a specific delay can be set per frame but that is beyond this answer).
-delay
appears to be ignored if used as an output option, so it has to be used before -
as shown in the example.
Lastly, browsers and image viewers may implement a minimum delay, so your -delay
may get ignored anyway.
Video courtesy of U.S. Fish & Wildlife Service National Conservation Training Center.
@LordNeckbeard - the
convert
option worked brilliantly - also using an ancient version of ffmpeg directly got a file size of 300Mb with rubbish output, whilst the high quality one from convert was 13Mb... – Wilf – 2014-07-05T23:00:18.0006By the way, for the
convert
command for converting from the PNG frames I ended up usingconvert -delay 5 -loop 0 -dither None -colors 80 "frames/ffout*.png" -fuzz "40%" -layers OptimizeFrame "output.gif"
, which reduces the overall file size quite a bit – Wilf – 2014-07-24T13:58:49.1701I think the
convert
command should be using-delay 10
(0.1 seconds between frames) to match the-r 10
(10 fps) frame rate you're feeding toffmpeg
. When I use-delay 5
, I see the gif play at double speed. – Jack O'Connor – 2014-09-27T00:16:34.060Is it it possible to crop a few pixels from the resulting GIF? I love these funny GIF from real movies/cartoons scenes, but I've found one, with a bad quality, in which the top... 5cm, more or less, is damaged. If I need to open a new thread I'll gladly – Bruno Augusto – 2016-05-15T21:40:08.970
Bravo! I used the "ffmpeg example" to convert a CamStudio recording of my screen showing a RAID5 parity recovery of a flash drive RAID5 array. The recovery took about an hour. During that time, I teed the output to a text file. The video was actually the recording of my parsing the text. the AVI was 1.4 GB; the GIF was 2.5 MB. – user38537 – 2016-05-29T04:23:17.360
In the second approach, you can use ffmpeg instead of imagick. All you need to do, is to use the image2 demuxer's framrate option, so you can set the delay between individual frames:
ffmpeg -framerate -i frames/ffout*.png output.gif
– Gergely Lukacsy – 2016-06-02T09:35:08.340Your ffmpeg example has become a common enough workflow for me that I wrote a bash wrapper script to automate it. Sharing here: https://github.com/jordanh/ffmpeg2gif
– Jordan – 2016-09-20T16:22:49.607How if i don't want to set the scale, i need to make him the the video's scale automatically? – Mousa Alfhaily – 2017-09-16T12:07:35.687
2Okay, I've got it, i used
scale=0:-1
, so when you set the scale to0
, it will take the scale from the video. – Mousa Alfhaily – 2017-09-16T12:20:01.333Giphy Engineering has released a nice article recently explaining all the options: https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg
– Marcus Mangelsdorf – 2018-04-30T09:54:55.187@LordNeckbeard How to can I modify your code to take let's say from a particular interval of a movie of let's say between 00:24:45 to 00:25:52? I tried to use the seek command and it did not work!. Can you perhaps help me with this?. I would like to know how to make it work with the palette you say to make and use ffmpeg to build up from that the gif. Btw I used the code as you mention and it works but to take a sample from longer intervals such as I mentioned is not very practical as I have to translate the minutes or hours to seconds. – Chris Steinbeck Bell – 2018-07-16T08:25:00.260
@LordNeckbeard Also the framerate you use how did you obtained?. Does it come from the input movie or is it just a random value that you chosen? – Chris Steinbeck Bell – 2018-07-16T08:25:18.453
@ChrisSteinbeckBell I chose a value low enough to make the GIF smaller in file size, but high enough to look acceptable. You'll just have to experiment depending on your content. – llogan – 2018-07-17T19:38:12.043
@LordNeckbeard Thanks for replying back, but I'd appreciate you could help me with the part of the code regarding time as I'm not too saavy enough to do that modification by myself. As I mentioned I tried to use seek command as ffmpeg documentation suggests the way of -ss 00:24:25 to 00:25:52 – Chris Steinbeck Bell – 2018-07-18T02:04:06.853
@LordNeckbeard just after the input but I can't make ffmpeg to interpret the command the way I need, perhaps can you help me with that?. I'd be really grateful. – Chris Steinbeck Bell – 2018-07-18T02:04:25.737
@ChrisSteinbeckBell Might be worth asking this as a new question and I or someone else can help. Comments may be too limited. – llogan – 2018-07-18T17:20:42.510
Trying the very first command - to generate the pallette (with ffmpeg-4.0.1):
Option 'flags' not found
. ffmpeg would've been a lot more useful, if the developers knew the concept of backwards compatibility... – Mikhail T. – 2018-07-21T19:01:48.693@MikhailT. Works for me. Please show your actual command and the complete log. You can use a pastebin site and provide the link here. – llogan – 2018-07-21T19:32:03.993
Err, never mind. I tried removing the
scale=360
part and that didn't work. Don't know, why... – Mikhail T. – 2018-07-21T19:41:22.2771@MikhailT. The full scale part is
scale=320:-1:flags=lanczos
. Theflags
is an option for scale. I probably should not have included that in the example as it increased complexity of the example. – llogan – 2018-07-21T21:17:44.5002This Q&A must be permanently encoded in a tome (or maybe just "pinned" for now) because in a hundred years from now all communication will be done via memes. I think the activity on this post alone speaks to that. – Jonathan Neufeld – 2018-10-07T08:15:21.300
What is [x] ?? Why nobody on the whole earh mentioned this [x]? – user1308990 – 2019-01-29T13:39:31.827
1
@user1308990
– llogan – 2019-01-30T22:25:32.650[x]
is just the label of the output coming from thefps=10,scale=320:-1:flags=lanczos
filterchain. The label is then referred to by other, separate filterchains or the output file. The label can be almost any arbitrary name, so you could rename it to whatever makes most sense to you. See FFmpeg Filtering: Introduction for more info.1
For the ffmpeg-only solution, I encourage the use of this brilliant single call with filter graph solution from @alijandro
– Leonid Usov – 2019-07-27T10:22:21.443Please describe all options, what is
-c:v pam
? – Vitaly Zdanevich – 2019-12-16T21:02:47.4031@VitalyZdanevich Answer updated. – llogan – 2019-12-16T21:34:53.427
4Added some example results (just still frames though). Here, the first file is 4.1 MB, the second around 8 MB. – slhck – 2013-02-22T21:44:43.320
2@LordNeckbeard, you are awesome! much thanks for
-vf scale=320:-1,format=rgb8,format=rgb24
– Kamil Hismatullin – 2013-02-22T21:53:44.860