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 Answer 1


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.

  • I don't know if I'm doing something wrong, but none of your examples works to me... Commented Dec 9, 2020 at 15:27
  • @MarcoA.Braghim Without any additional info I can only guess that your ffmpeg is probably too old. Or show your ffmpeg command and the complete log. Use a pastebin site and provide the link in a comment.
    – llogan
    Commented Dec 9, 2020 at 18:08
  • I'll mark it as right answer because directly on terminal it works as expected, actually I'm trying to run this command at Flutter ffmpeg plugin package and at Android it works too. Probably my issue stands at iOS... Thank you. Commented Dec 9, 2020 at 18:14
  • This produces a repetition of the last and first frames (i.e., with 5 looping frames, the output is 1-2-3-4-5-5-4-3-2-1 while it ideally would be 1-2-3-4-5-4-3-2). Is there a way to drop the first and last frames in the reversed half?
    – elmimmo
    Commented Mar 5, 2021 at 11:33
  • 2
    @llogan I am getting the following error when downscaling input link in0:v0 parameters (size 320x180, SAR 0:1) do not match the corresponding output link in0:v0 parameters (3840x2160, SAR 0:1) Commented Jul 5, 2021 at 17:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .