How to remove every second frame from an animated gif?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
hellocatfood
Posts: 44
Joined: 2010-01-01T13:29:41-07:00
Authentication code: 8675309

How to remove every second frame from an animated gif?

Post by hellocatfood »

I have a video (avi) that I want to convert to an animated gif. ffmpeg/avconv does a bad job of doing it directly, so I instead convert it to a gif by first outputting each frame as a png and then converting back to gif using iamgemagick. 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove every second frame from an animated gif?

Post by fmw42 »

As far as I know, IM has no special command to skip frames. It would be a nice addition for an enhancement.

You would most likely have to write a script loop to select the list of all the png images you wanted and then use the list as input for convert to make the gif animation.

However, I am surprised to hear that ffmpeg won't do that, simply by specifying some argument like frames per second to export from the video. However, I am not a expert using ffmpeg.

Perhaps one of the user's who has more experience with ffmpeg can help further.
Last edited by fmw42 on 2013-09-02T16:01:41-07:00, edited 1 time in total.
User avatar
hellocatfood
Posts: 44
Joined: 2010-01-01T13:29:41-07:00
Authentication code: 8675309

Re: How to remove every second frame from an animated gif?

Post by hellocatfood »

fmw42 wrote:As far as I know, IM has no special command to skip frames. You would most likely have to write a script loop to select the list of all the png images you wanted and then use the list as input for convert to make the gif animation.

However, I am surprised to hear that ffmpeg won't do that, simply by specifying some argument like frames per second to export from the video. However, I am not a expert using ffmpeg.

Perhaps one of the user's who has more experience with ffmpeg can help further.
ffmpeg/avconv can specify frame rate but it seems like it's not working when the output is pictures.

In the end I used a modified version of this script

Code: Select all

#!/bin/bash

for file in *.avi
do

avconv -i $file	image_%05d.png

cnt=0; 
for file in image_*.png
do 
  let cnt=cnt+1; 
  if [ $cnt -eq 3 ]; //change this number to specify every nth file to delete 
    then rm $file;
    cnt=0;
fi
done

convert image_*.png output.gif

done
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to remove every second frame from an animated gif?

Post by snibgo »

I think the usual way to skip frames with ffmpeg is by messing with framerates. Eg if the input is 60 fps and the output is 30 fps, every second frame will be dropped.

I've never used ffmpeg to create gif annimations, and don't know how well it works.
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to remove every second frame from an animated gif?

Post by glennrp »

Presuming you have included frame numbers in the names of the PNG files, just remove the odd ones before converting them
to GIF:

Code: Select all

 rm image_*[13579].png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to remove every second frame from an animated gif?

Post by anthony »

Now that is a neat and simple solution.

Afterwards you can also use some helper scripts "mv_reseq" (renamed "mv_renum" script) and "mv_perl" scripts to renumber and rename files. That will remove the 'gaps' in the counts you just removed.

The Scripts are in my personal software export
http://www.ict.griffith.edu.au/anthony/ ... /#mv_renum
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply