7

I have an input video mp4. I need to create a 2sec output media where 1sec goes forward and 1sec backward (boomerang effect), ok it's working well if I set the output as mp4 too, but I need to set its output as GIF. How can I do that?

ffmpeg -t 1 -i input.mp4 -filter_complex "[0]reverse[r];[0][r]concat,loop=1:2,setpts=N/25/TB" output.mp4

1 Answers1

13

Basic GIF example

Using the reverse and concat filters:

ffmpeg -i input.mp4 -filter_complex "[0]reverse[r];[0][r]concat=n=2:v=1:a=0" output.gif

GIF from ffmpeg will loop forever by default, so you don't need to add any loop options.

High quality GIF example

Using the reverse, concat, split, palettegen, and paletteuse filters:

ffmpeg -i input.mp4 -filter_complex "[0]reverse[r];[0][r]concat=n=2:v=1:a=0,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif

Adapted from How do I convert a video to GIF using ffmpeg, with reasonable quality?

With downscaling and lower frame rate for smaller file size

Same as above but with the scale and fps filters added:

ffmpeg -i input.mp4 -filter_complex "[0]scale=320:-1,reverse[r];[0][r]concat=n=2:v=1:a=0,fps=10,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif

Avoid duplicating first and last frames

By adding the trim and setpts filters. In this example input.mp4 has a frame rate of 10 and is 3 seconds long. Note that in the trim filter frame #0 is the first frame.

ffmpeg -i input.mp4 -filter_complex "[0]trim=start_frame=1:end_frame=29,setpts=PTS-STARTPTS,reverse[r];[0][r]concat=n=2:v=1:a=0,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif

Also see Fetch frame count with ffmpeg.

llogan
  • 63,280