Getting the animation length of a 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
Shaun
Posts: 8
Joined: 2013-04-25T23:06:34-07:00
Authentication code: 6789

Getting the animation length of a GIF

Post by Shaun »

Hello,

I was searching for an answer all over the internet and couldn't find any. So I will ask the question by myself:

Is it possible to determine the animation length of a GIF?

I could not find a command for that. I know that I can't just determine a framerate and count the frames to get the animation length, because the times between the frames can be different.
So what is the right way to get to the goal? :)
I would be really thankful for constructive answers.

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

Re: Getting the animation length of a GIF

Post by snibgo »

Code: Select all

identify -verbose x.gif
For each frame, look for the "delay" entry, {ticks}x{ticks-per-seconds}, eg "7x100" means 7/100 seconds = 0.07s. Then add these up for the total duration.

Or more directly with ExifTool:

Code: Select all

exiftool -Duration x.gif
Duration                        : 6.10 s
snibgo's IM pages: im.snibgo.com
Shaun
Posts: 8
Joined: 2013-04-25T23:06:34-07:00
Authentication code: 6789

Re: Getting the animation length of a GIF

Post by Shaun »

Hello,

thanks for your answer. Sadly, exiftool doesn't consider the iteration of a gif.

I tried the following command to get at least the image time delay for each frame:

Code: Select all

identify -verbose -format "Frame %s: %Tcs\n" 31_de_bf_gif_200x200.gif
In -verbose, I also found the information "Iterations". But I couldn't figure out how to output this information (to finally do some math to get the final practical duration of the gif).

The following doesn't work:

Code: Select all

identify -verbose -format "Frame %s: %Tcs | Duration: %[Iterations]\n" 31_de_bf_gif_200x200.gif
It only outputs the following:

Code: Select all

Frame 0: 30cs | Iterations:
Frame 1: 30cs | Iterations:
Frame 2: 30cs | Iterations:
Frame 3: 30cs | Iterations:
Frame 4: 30cs | Iterations:
Frame 5: 30cs | Iterations:
Frame 6: 30cs | Iterations:
Frame 7: 30cs | Iterations:
I was searching again for hours for a solution to get the duration of a gif including the loops. I can't believe nobody ever needed that information before.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Getting the animation length of a GIF

Post by glennrp »

I haven't tried any of them, but googling for "gif duration" gives links to several applications that probably do what you want.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Getting the animation length of a GIF

Post by fmw42 »

I have no idea what iterations means. It only shows up for me in the first frame of the animation and is 0. Later frames only show the Scene number out of the total number of Scenes.

What do you think interations means? What were you looking for?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Getting the animation length of a GIF

Post by glennrp »

fmw42 wrote:What do you think interations means?
I believe it means "how many times to play the entire animation". If it is zero it means to replay the animation repeatedly forever.

Thanks Glenn. That explains why it only shows up in the first frame and for my -loop 0 animation, it shows zero.

Fred
Shaun
Posts: 8
Joined: 2013-04-25T23:06:34-07:00
Authentication code: 6789

Re: Getting the animation length of a GIF

Post by Shaun »

fmw42 wrote:What were you looking for?
For the time, a gif animation needs to be finished, inclusive all loops.
So the time can be 0 seconds, n seconds or infinite.
glennrp wrote:but googling for "gif duration" gives links to several applications that probably do what you want.
Well, can you give me an example? As I said, I googled for hours and could'nt find any solution. Including other programs.

But well... I can't believe, that nobody found yet such solution for the mighty Image Magick?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the animation length of a GIF

Post by snibgo »

Code: Select all

convert xc: xc:black -loop 99 x.gif
identify -verbose x.gif | cGrep /i- /sIterations
Or:

Code: Select all

F:\web\im>exiftool -GIF:AnimationIterations x.gif
Animation Iterations            : 99
snibgo's IM pages: im.snibgo.com
Shaun
Posts: 8
Joined: 2013-04-25T23:06:34-07:00
Authentication code: 6789

Re: Getting the animation length of a GIF

Post by Shaun »

snibgo wrote:

Code: Select all

convert xc: xc:black -loop 99 x.gif
identify -verbose x.gif | cGrep /i- /sIterations
This gives me only the number of iterations...which can't even be accessed via an variable. :/ I dont know how I could continue here to get the final duration.
snibgo wrote:

Code: Select all

F:\web\im>exiftool -GIF:AnimationIterations x.gif
Animation Iterations            : 99
Hm, via exiftool, I get at least also the Duration for one iteration. But how can I multiply those two values? I need a solution where I can output the final duration. So at least, I need to be able to do some math in the command line.

Thanks for your answer. :)
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Getting the animation length of a GIF

Post by glennrp »

I've pushed an update to the SVN repostory, IM6, version 14582, to add image->duration to image.h, calculate it in gif.c, and report it (if non-zero) in identify.c.
To do: calculate duration in other animation-supporting formats including MIFF, MPC, and MNG, and port to IM7.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Getting the animation length of a GIF

Post by fmw42 »

This should do it. Unix syntax

# create animation with 4 frames, delay 50 and iterations 8
convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif

# get duration
convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif
iterations=`convert rose.gif[0] -verbose info: | sed -n 's/^[ ]*Iterations: \([0-9]*\)*$/\1/p'`
delayArr=(`convert rose.gif -format "%T\n" info:`)
numframes=${#delayArr[*]}
duration=0
for ((i=0; i<numframes; i++)); do
duration=`convert xc: -format "%[fx:$duration + $iterations*${delayArr[$i]}]" info:`
done
echo $duration
1600
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Getting the animation length of a GIF

Post by glennrp »

glennrp wrote:
fmw42 wrote:This should do it. Unix syntax

# create animation with 4 frames, delay 50 and iterations 8
convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif

# get duration
convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif
iterations=`convert rose.gif[0] -verbose info: | sed -n 's/^[ ]*Iterations: \([0-9]*\)*$/\1/p'`
delayArr=(`convert rose.gif -format "%T\n" info:`)
numframes=${#delayArr[*]}
duration=0
for ((i=0; i<numframes; i++)); do
duration=`convert xc: -format "%[fx:$duration + $iterations*${delayArr[$i]}]" info:`
done
echo $duration
1600
More generally, we must account for the possibility that the delay is not the same for
each frame. The code that I added to gif.c does that by keeping a running
tally.
  • glenn.rp> q16convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif
    glenn.rp> q16identify -verbose rose.gif | grep urat
    Duration: 1600
  • glenn.rp> q16convert -delay 50 rose: rose: -delay 10 rose: rose: -loop 8 rose.gif
    glenn.rp> q16identify -verbose rose.gif | grep Duration
    Duration: 960
Glenn,

Mine does to by getting the delays from every frame (as delayArr) and looping to add them together. But a more automatic and direct way is always better. My solution was temporary until you implement yours.

Fred
Shaun
Posts: 8
Joined: 2013-04-25T23:06:34-07:00
Authentication code: 6789

Re: Getting the animation length of a GIF

Post by Shaun »

fmw42 wrote:This should do it. Unix syntax

# create animation with 4 frames, delay 50 and iterations 8
convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif

# get duration
convert -delay 50 rose: rose: rose: rose: -loop 8 rose.gif
iterations=`convert rose.gif[0] -verbose info: | sed -n 's/^[ ]*Iterations: \([0-9]*\)*$/\1/p'`
delayArr=(`convert rose.gif -format "%T\n" info:`)
numframes=${#delayArr[*]}
duration=0
for ((i=0; i<numframes; i++)); do
duration=`convert xc: -format "%[fx:$duration + $iterations*${delayArr[$i]}]" info:`
done
echo $duration
1600
Thanks! Just one little bug: The number of iterations needs to be increased by 1 do get the correct duration.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Getting the animation length of a GIF

Post by glennrp »

Shaun wrote: Thanks! Just one little bug: The number of iterations needs to be increased by 1 do get the correct duration.
Unfortunately the word "iterations" is ambiguous. Netscape, Firefox, and ImageMagick interpret it (as commonly used
by computer programmers) to mean "number of times to play" while Chrome interprets it (as commonly used by
mathematicians) as "number of times to repeat".
Shaun
Posts: 8
Joined: 2013-04-25T23:06:34-07:00
Authentication code: 6789

Re: Getting the animation length of a GIF

Post by Shaun »

glennrp wrote:
Shaun wrote: Thanks! Just one little bug: The number of iterations needs to be increased by 1 do get the correct duration.
Unfortunately the word "iterations" is ambiguous. Netscape, Firefox, and ImageMagick interpret it (as commonly used
by computer programmers) to mean "number of times to play" while Chrome interprets it (as commonly used by
mathematicians) as "number of times to repeat".
Very interesting information, thank you. IE behaves like Chrome in this case.
Post Reply