I have a program that generates a 10-frame GIF slideshow from a video, with the slideshow frames always being 0.5s seconds long, regardless of the video length.*

It works by first getting the duration of the video, then running the following snippet, which pipes the output of ffmpeg through imagemagick and gifsicle:

ffmpeg -i video.webm -filter:v "fps=1/#{duration / 10.0}" -frames:v 10 -f image2pipe -vcodec ppm - | convert -delay 50 -loop 0 -gif:- | gifsicle -O2 > preview.gif

I would like to convert this code snippet to use a single ffmpeg command.

First, a basic sanity check. input.webm duration = 100s:

ffmpeg -y -i input.webm -vf "fps=1/10" 

As expected, this works. It creates an output with the right number of frames, but the wrong frame timings.

I expected that the solution to get a constant 2 FPS would be to simply use the -r option to set the output FPS:

ffmpeg -i input.webm -vf "fps=1/10" -r 2 preview.gif

This didn't work. It generated a file with the same frame timings as before.

Given that a video with this constant framerate setup could have the time base be at 0.5s and the PTS be at the frame number, I tried that too:

ffmpeg -i input.webm -vf "fps=1/10,settb=1/2,setpts=N" preview.gif

But that didn't work either. ffmpeg dropped all but 3 of the frames and they were still shown for 10 seconds instead of half a second.

What am I doing wrong here?

* It will generate an output GIF with fewer than 10 frames if the input video has fewer than 10 frames. However, the frame timing will be the same.


You need to also set the -r option when you use settb and setpts:

ffmpeg -y -i input.webm -vf "fps=1/10,settb=1/2,setpts=N" -r 2 preview.gif
  • You can skip the fps and settb. ffmpeg -y -i input.webm -vf "setpts=N/TB/2" -r 2 preview.gif – Gyan Jan 1 '20 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.