Skip to content

Instantly share code, notes, and snippets.

@lmmx
Created March 23, 2025 13:48
Show Gist options
  • Save lmmx/77a7ca335a9fb83ab51c9c667b13e7fc to your computer and use it in GitHub Desktop.
Save lmmx/77a7ca335a9fb83ab51c9c667b13e7fc to your computer and use it in GitHub Desktop.
Gif optimisation using gifski, a fast Rust GIF encoder https://github.com/ImageOptim/gifski/
gifopt() {
local inputgif="${1:?Missing input GIF filename}"
local quality="${2:-80}"
if ! [[ "$quality" =~ ^[0-9]+$ ]] || [ "$quality" -lt 1 ] || [ "$quality" -gt 100 ]; then
echo "Quality parameter must be an integer from 1 to 100" >&2
return 1
fi
local tmpdir="$(mktemp -d)"
local outputgif="${inputgif%.gif}-opt.gif"
# Extract frames
ffmpeg -loglevel error -i "$inputgif" "$tmpdir/frame%04d.png"
# Recreate GIF using gifski
gifski -o "$outputgif" --quality "$quality" "$tmpdir"/*.png
# Clean up temporary files
rm -rf "$tmpdir"
# Report file size difference
local oldfilesize=$(du -h "$inputgif" | cut -f1)
local newfilesize=$(du -h "$outputgif" | cut -f1)
echo "File reduced from $oldfilesize to $newfilesize as $outputgif"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment