Last active
July 1, 2025 17:47
-
-
Save troutcolor/d6f06f9fd44746a4e4aa41048c474e9b to your computer and use it in GitHub Desktop.
takes an input gif, deletes every second frame, sets the delay to each frame plus the delay of the frame after it. Good for shrinking the size of gifs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: ./reduce_gif_frames.sh input.gif output.gif | |
if [ $# -ne 2 ]; then | |
echo "Usage: $0 input.gif output.gif" | |
exit 1 | |
fi | |
INPUT_GIF="$1" | |
OUTPUT_GIF="$2" | |
echo "Processing $INPUT_GIF..." | |
# Get frame count | |
FRAME_COUNT=$(gifsicle --info "$INPUT_GIF" | grep -c "image #") | |
echo "Original frame count: $FRAME_COUNT" | |
# Get delays for each frame | |
echo "Analyzing frame delays..." | |
DELAYS=() | |
for ((i=0; i<FRAME_COUNT; i++)); do | |
DELAY=$(gifsicle --info "$INPUT_GIF" "#$i" | grep "delay" | sed 's/.*delay \([0-9]*\).*/\1/') | |
if [ -z "$DELAY" ]; then | |
DELAY=10 # Default delay if none specified | |
fi | |
DELAYS[i]=$DELAY | |
done | |
# Calculate new frame count | |
NEW_FRAME_COUNT=$(((FRAME_COUNT + 1) / 2)) | |
echo "New frame count: $NEW_FRAME_COUNT" | |
# Build gifsicle command to create new GIF | |
GIFSICLE_CMD="gifsicle -U '$INPUT_GIF'" | |
for ((i=0; i<FRAME_COUNT; i+=2)); do | |
# Calculate combined delay | |
DELAY1=${DELAYS[i]} | |
if [ $((i+1)) -lt $FRAME_COUNT ]; then | |
DELAY2=${DELAYS[$((i+1))]} | |
else | |
DELAY2=0 # If odd number of frames, no second frame to add | |
fi | |
COMBINED_DELAY=$((DELAY1 + DELAY2)) | |
echo "Keeping frame $i with combined delay $DELAY1+$DELAY2=$COMBINED_DELAY" | |
# Add frame with new delay to command | |
GIFSICLE_CMD="$GIFSICLE_CMD --delay $COMBINED_DELAY '#$i'" | |
done | |
GIFSICLE_CMD="$GIFSICLE_CMD --colors 256 --optimize=3 --output '$OUTPUT_GIF'" | |
echo "Creating new GIF..." | |
eval $GIFSICLE_CMD | |
echo "Done! Created $OUTPUT_GIF with $NEW_FRAME_COUNT frames" | |
echo "Original duration preserved by combining frame delays" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment