Skip to content

Instantly share code, notes, and snippets.

@ohiosveryown
Forked from jackrusher/twitter-gifs.md
Created December 14, 2021 05:31
Show Gist options
  • Save ohiosveryown/eb4c05f165f9c94571f6a03752b3582d to your computer and use it in GitHub Desktop.
Save ohiosveryown/eb4c05f165f9c94571f6a03752b3582d to your computer and use it in GitHub Desktop.

Twitter abuses all media file uploads, each type in its own way. If we want to upload a good looking animation loop from some low-color, high-detail generative art, we have to game their system's mechanisms.

  • don't upload a video file, they will re-encode it into absolute 💩

  • create a GIF, which they will auto-convert into a video file 😱

  • The frames of the GIF will be resized to an even-sized width using an extremely naive algorithm. Your GIF should be an even size (1000, 2000, &c) so the multiple of this size to avoid janky artifacts caused by floating point division.

  • The GIF format is also terrible, so you have to game that too. In particular, if we create a GIF using ffmpeg's defaults we end up with one small divergent color palette per frame. This leads to weird inconsistencies and artifacts between frames and blows up the file size. To avoid this, choose a representative frame from your animation and generate a palette from that:

ffmpeg -i frame-030.png -vf palettegen -y palette.png

And then generate your GIF using that palette:

ffmpeg -framerate 20 -pattern_type glob -i 'frame-*.png' -i palette.png -filter_complex "paletteuse" art.gif

If the GIF looks right, but is slightly too large to upload, you can give gifsicle a try:

gifsicle -O art.gif > art-optimized.gif

... et Voilà! Your beautiful art is as Twitter ready as it can be. Vaya con dios!

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