Skip to content

Instantly share code, notes, and snippets.

@x86-39
Last active October 2, 2021 23:38
Show Gist options
  • Save x86-39/404c17bd80f46d40c213d6daf610f927 to your computer and use it in GitHub Desktop.
Save x86-39/404c17bd80f46d40c213d6daf610f927 to your computer and use it in GitHub Desktop.
Stitch together PNG files to an animated GIF with ffmpeg

Create GIF

This takes files in the current directory named like 1.png 2.png 3.png ... and creates a gif out.gif.

ffmpeg -i ./%001d.png -vf palettegen=reserve_transparent=1 -f apng pipe:1 \
| ffmpeg -framerate 3 -i ./%001d.png -i - -lavfi paletteuse=alpha_threshold=128 -gifflags -offsetting out.gif

Change the framerate by changing -framerate 3 to your desired value.
Change the directory by changing the ./%001d.png in both parts of the command.
Change the output location by changing out.gif at the end of the command.

Upscale GIF without algorithm (neighbor)

This doubles the size of the gif while keeping transparency and not using algorithms (Good for pixel art).
Change how much it upscales by changing scale=iw*2:ih*2, the numbers is the multiplier.

ffmpeg -i out.gif -filter_complex \
"[0:v] scale=iw*4:ih*4:flags=neighbor,split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse" out2.gif

To change the input, change out.gif in the first part of the command.
To change the output, change out2.gif at the end of the command.

With padding

ffmpeg -i out.gif -filter_complex \
"[0:v] scale=iw*4:ih*4:flags=neighbor,pad=width=200:height=200:x=24:y=16:color=black, split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse" out2.gif

Change the params after pad= to customise. Width and height is the end resolution, x and y are the position of the original video. Color is the padding colour.

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