Skip to content

Instantly share code, notes, and snippets.

@aug2uag
Created January 28, 2016 17:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aug2uag/0906c80efb9fd6067b54 to your computer and use it in GitHub Desktop.
Save aug2uag/0906c80efb9fd6067b54 to your computer and use it in GitHub Desktop.
remove every second frame of GIF with gifsicle
#!/bin/bash
# This script will take an animated GIF and delete every other frame
# Accepts two parameters: input file and output file
# Usage: ./<scriptfilename> input.gif output.gif
# Make a copy of the file
cp $1 $2
echo "copy completed"
# Get the number of frames
numframes=`gifsicle $1 -I | grep -P "\d+ images" --only-matching | grep -P "\d+" --only-matching`
echo "numframes completed"
# Deletion
gifsicle "$1" --unoptimize $(seq -f "#%g" 0 2 $numframes) -O2 -o "$2"
echo "script completed"
@ulf1
Copy link

ulf1 commented Jun 23, 2016

thank you. i found it useful

@rjcorwin
Copy link

Failed on macOS for me. Ended up with a Gif with probably about three frames at the beginning.

screen shot 2017-07-26 at 5 02 23 pm

@h3xx
Copy link

h3xx commented Jul 28, 2017

@rjsteinert Try this version on mac.

@zyc945
Copy link

zyc945 commented Feb 26, 2021

//on macos, get numframes use this command:
numframes=gifsicle $1 -I | head -n 1 | awk '{print $3}'

@se7enack
Copy link

To work with MacOS and Linux...

Replace:

numframes=`gifsicle $1 -I | grep -P "\d+ images" --only-matching | grep -P "\d+" --only-matching`

With:

os=$(uname -s)
if [[os == 'Darwin']]; then
    numframes=$(gifsicle $1 -I | head -n 1 | awk '{print $3}')
else
    numframes=`gifsicle $1 -I | grep -P "\d+ images" --only-matching | grep -P "\d+" --only-matching`
fi

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