5

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

9

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
  • 57,139
  • 15
  • 118
  • 145
  • I don't know if I'm doing something wrong, but none of your examples works to me... – Marco A. Braghim Dec 09 '20 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 Dec 09 '20 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. – Marco A. Braghim Dec 09 '20 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 Mar 05 '21 at 11:33
  • @elmimmo See last example in updated answer. – llogan Mar 05 '21 at 17:19
  • I'm trying to capture just the first second from a very long video. This command seems to run the filters on the entire clip. I tried -ss 0 and -t 1, but it still seems that the entire filter applies. Is there a way to just take a short portion of the source video? – tslater Apr 22 '21 at 22:08
  • @tslater `ffmpeg -t 1 -i input.mp4 -filter_complex "[0]reverse[r];[0][r]concat=n=2:v=1:a=0" output.gif` – llogan Apr 22 '21 at 22:15
  • @llogan thanks! Questions, does that still do the first/last duplicate frame avoidance? Is that also possible with extracting just 1 second? – tslater Apr 23 '21 at 23:17
  • @tslater 1) No. 2) Yes. Example assuming an input frame rate of 30. `ffmpeg -t 1 -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` – llogan Apr 23 '21 at 23:24
  • Thanks! @llogan – tslater Apr 23 '21 at 23:31
  • @llogan Doesn't “Avoid duplicating first and last frames” still duplicate the last original frame (i.e. the one corresponding to 5-5)? – elmimmo Jun 22 '21 at 12:15
  • @elmimmo Works for me without duplicating the last original frame. – llogan Jun 24 '21 at 02:23
  • 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)` – Juan Martin Zabala Jul 05 '21 at 17:12
  • @JuanMartinZabala Need your command and complete log to provide help. – llogan Jul 06 '21 at 17:17
  • Yeah, unfortunately the downscaling does not seem to work: `ffmpeg -i video.mp4 -filter_complex "[0]scale=480:-1,reverse[r];[0][r]concat=n=2:v=1:a=0,fps=10,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" ~/Downloads/output.gif` – oarfish Apr 06 '22 at 09:15