How to remove every second frame from an animated gif?

I have a folder full of videos that I want to convert to an animated gifs. ffmpeg/avconv does a bad job of doing it directly, so I instead convert the video to a gif by first outputting each frame as a png and then converting back to gif using imagemagick. The problem is that this results in a large gif in terms of file size. To solve this I want to “drop” every second or nth frame from the gif, either by skipping every image file when converting to a gif or by removing frames from a gif. How can I do this on Ubuntu (13.04) using imagemagick or some other command-line utility?

Answer

Using a bash script

To do this from the command line, you could use a utility called Gifsicle. There is no built in method to delete every other frame, so you’ll need to get your hands dirty with some scripting.

Here is a quick script I made to do just a single GIF:

#!/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

# Get the number of frames
numframes=`gifsicle $1 -I | grep -P "\d+ images" --only-matching | grep -P "\d+" --only-matching`

# Deletion
let i=0
while [[ $i -lt $numframes  ]]; do
    rem=$(( $i % 2 ))
     
    if [ $rem -eq 0 ]
    then
        gifsicle $2 --delete "#"$(($i/2)) -o $2 
    fi

    let i=i+1 
done

I tested it out with a simple countdown GIF:

enter image description here

And here is the result after running it through the script:

enter image description here

This script is of course not bulletproof, but it should lead you in the right direction.

Attribution
Source : Link , Question Author : hellocatfood , Answer Author : Community

Leave a Comment