How to determine the length of a .gif animation in milliseconds

我只是一个虾纸丫 提交于 2019-11-30 23:10:28

The identify command from ImageMagick can give this information:

$ identify -verbose file.gif | grep 'Elapsed time'

  Elapsed time: 0:01.080
  Elapsed time: 0:01.150
  Elapsed time: 0:01.230

...

  Elapsed time: 0:04.250
  Elapsed time: 0:04.330
  Elapsed time: 0:04.399
  Elapsed time: 0:04.480

The last line printed should be the total length of the animation.

Prinzhorn

The accepted answer doesn't give the exact result. Elapsed time is like a real world clock while ImageMagick runs the animation. What you want is the Delay field for each frame and sum them up.

$ identify -verbose fail.gif  | grep Delay

Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 15x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 10x100
Delay: 33x100
Delay: 10x100
Delay: 10x100
Delay: 10x100

Where 33x100 is a delay of 330ms.

Edited by Mark Setchell

You can actually extract the delay parameter somewhat more surgically (than by using grep) with the %T escape:

identify -format "%T\n" animation.gif 

8
8
8
8
8
8
8
8
8
8
11
11
11
11
11
11
11
26

And get the total with awk like this:

identify -format "%T\n" anomation.gif | awk '{t+=$0} END{print t " centiseconds"}'
183 centiseconds

I tried ImageMagick identify but it didn't give me the correct duration.

I found another reliable way using ExifTool

exiftool -Duration image.gif

It will print out the duration in seconds:

Duration : 0.48 s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!