Skip to content

Instantly share code, notes, and snippets.

@grocky
Created November 25, 2020 19:45
Show Gist options
  • Save grocky/7c5638e1b910969d82ccf51c886164d6 to your computer and use it in GitHub Desktop.
Save grocky/7c5638e1b910969d82ccf51c886164d6 to your computer and use it in GitHub Desktop.
Reduce a gif down to a desired number of frames
#!/usr/bin/env bash
# Reduce a gif down to a desired number of frames.
# Useful for really large gifs that you'd like to truncate.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJ_DIR=$(realpath "${DIR}/..")
TEMP_DIR=$(mktemp -d )
REDUCED_DIR=$(mktemp -d)
function cleanup {
rm -rf "${TEMP_DIR}"
rm -rf "${REDUCED_DIR}"
}
trap cleanup EXIT
full_gif="${1}"
num_images=${2}
target_name="${3:-${full_gif%.*}-reduced.gif}"
convert "${PROJ_DIR}/${full_gif}" +adjoin ${TEMP_DIR}/temp_%03d.gif
for i in $(seq -w 0 ${num_images}); do
cp ${TEMP_DIR}/temp_${i}.gif ${REDUCED_DIR}/
done
convert -delay 7 $(ls ${REDUCED_DIR}/*.gif) ${PROJ_DIR}/${target_name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment