Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gka
Last active January 16, 2024 22:03
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gka/148bbad67871fa6ca8d0b97e4eee94b5 to your computer and use it in GitHub Desktop.
Save gka/148bbad67871fa6ca8d0b97e4eee94b5 to your computer and use it in GitHub Desktop.
how to make a nice GIF from png frames

Make sure ffmpeg is up-to-date:

brew update
brew upgrade ffmpeg

Convert a MOV into frames. Tweak the 2/1 if you want more or fewer frames.

mkdir frames
ffmpeg -i screen-recording.mov -r 2/1 frames/%03d.png

Optional: crop the frames to 600x400px using 824,690 as top/left

mkdir cropped
convert ??.png -crop 600x400+824+690 cropped/

Create the pallete.gif

ffmpeg -i cropped/%02d.png -vf palettegen palette.png

Aaaand create the GIF. The 6*PTS is added to slow down the GIF frames.

ffmpeg -v warning -i cropped/%02d.png -i palette.png  -lavfi "paletteuse,setpts=6*PTS" -y out.gif

Bonus tip: if you want the last frame to have a longer duration, just copy your last frame image a bunch of times and use new frame numbers as filename.

copy 20.png 21.png
copy 20.png 22.png
copy 20.png 23.png
copy 20.png 24.png

Sources: http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html

@varoon
Copy link

varoon commented Apr 5, 2018

Hello,
How to Add Transparent of the GIF ?
The PNG images have transparency and the output GIF does not have transparent. Any fix ?

@kkevlar
Copy link

kkevlar commented Aug 22, 2018

@samdivaio
Copy link

They do.

@doczoidberg
Copy link

my parameters:

-framerate 10 -i _frame%d.png -i palette.png -lavfi paletteuse=alpha_threshold=128 -gifflags -offsetting result.gif

@MichaelMacha
Copy link

@samdivaio I think what kkevlar means is that GIFs don't support true, alpha transparency; which is true. They have two methods built-in for handling transparency—do-not-replace, which means that they maintain the same color as the previous frame; and draw-background, which means they draw what's provided as a background color. More modern formats like PNGs do support a full alpha channel, and can be said to support true transparency; but GIFs use something of a hack.

Apologies if this counts as a necro, but I feel this data could be pertinent.

@monkeykim111
Copy link

@varoon GIF format does not support alpha channel. So, in order to make a png file into a gif, you need to use the palette.
and I have a question,
When creating a gif from an image, do you know how to give a different frame rate for each frame?

@iTouchU
Copy link

iTouchU commented May 24, 2023

@monkeykim111

You can use -r which stands for “rate” to change the frame rate. It will generate the same result as using -framerate

From:
https://www.bannerbear.com/blog/how-to-make-gifs-from-images-using-ffmpeg/

As for the - extremely old - comment about transparency, and, granted there have been major advancements of which I don't know all of. Programs such as Photoshop or Gimp can be used to add effects, just make a gif from a series of images (which Photoshop and Gimp can also do) and add the effect(s) to each individual image.

@VivekThazhathattil
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment