Creating High Quality GIFs in 2023

Since this always takes a little while to figure out, and as a reference for myself, here is how we are recording GIFs to share on Kickstarter, Discord, Mastodon and Twitter in 2023!

Recording Setup

Most importantly, your initial recordings need to be near lossless quality, and for this we are using OBS studio.  Here's a screenshot of the settings we went with:

A few notes here:

  • Due to how MP4 works, do not use it for recording, instead convert to it later.
  • h264 + ACC allow you to do pass through with videos on Mastodon and Youtube
  • qp=0 enables lossless encoding for near perfect quality

You must also change your color format from the default NV12 to I444 in the Advanced tab, otherwise your quality will appear much lower due to missing colors.

Then try recording a few videos to ensure the quality appears lossless.

Creating Clips

With OBS Studio setup, after you record some gameplay you need to cut them up into clips you want to share on social media.  We do this by creating a clip.sh bash script that uses FFMPEG:

#!/bin/bash

# usage:
#   ./clip.sh filename start time

# ex. go to 2min into the video and grab a 30 sec clip
#  ./clip.sh recording.mkv 2:00 30.5

file=$1
time=$3
num=0
params="-c:v libx264rgb -c:a copy -max_muxing_queue_size 1024 -crf 0 -preset veryslow -y"

# allow entering time like 0:30.5 which is 30.5 seconds
SavedIFS="$IFS"
IFS=":."
t=($2)
start=$((10#${t[0]}*60 + 10#${t[1]})).${t[2]}
IFS="$SavedIFS"

# autocreate vids/num folders for each clip we try to create
while [ -d "vids/${num}" ]; do
  num=$(( $num + 1 ))
done
mkdir "vids/${num}"

# create actual clip
ffmpeg -i $file -ss $start -t $time $params vids/$num/full.mkv

echo "input video: ${1}"
echo "output to vids/${num}"

Creating GIFs

Once you have your clips, you want to create your gifs - but first you need to resize them, as 1080p gifs would be massive in filesize!  We typically use 720x405 for this, as its the width of posts on Kickstarter and ManaKeep, and works well for Discord, Mastodon and Twitter.  Here's how we do this with a reforge.sh bash script:

#!/bin/bash

# reforge existing videos
# usage:
#   ./reforge.sh folder
#   ./reforge.sh vids/2

folder=$1
params="-c:v libx264rgb -an -r 60 -max_muxing_queue_size 1024 -crf 0 -preset veryslow -y"

# create tmp folder to hold pngs
mkdir "${folder}/pngs"

# create social clip to share
ffmpeg -i $folder/full.mkv -vf "scale=1440:810:flags=neighbor,crop=720:405" $params $folder/social-720x405.mkv

# create gif of this clip
ffmpeg -i $folder/social-720x405.mkv -vf "fps=40" $folder/pngs/%04d.png
gifski -r 40 -Q 100 -W 720 -H 405 $folder/pngs/*.png -o $folder/social-720x405.gif

# clear temporary pngs
rm -rf "${folder}/pngs"

We are making use of the gifski tool to convert the mkv to a gif, which we found worked the best for this and created great quality, high fps gifs.  You might notice we record our videos at 60fps but are switching to 40fps here before creating the gifs, as browsers do not support over 50fps gifs.  They still look really good at this speed.

And that's it!  Hopefully some other gamedevs may find this useful =)