29

Is there a command to get the number of frames in a gif? I have some gifs, and I want to convert them into arrays, but I want to limit the array, and structure of the array based on the number of frames in any random gif I happen to feed my program. I was thinking imagemagick might have such an option, but i haven't found it yet.

5 Answers 5

32

If you have ImageMagick installed, you can use its identify command as follows

identify /path/to/image.gif

You'll get details of each frame.

As suggested by steeldriver, you can get the number of images in the sequence directly using the %n output format specifier, however rather annoyingly it appears to print it once for each image. To get a single number you could do

identify -format "%n\n" path/to/file.gif | head -1
1
  • 2
    Oopsie. I was the first to post an answer and then updated it 3 times without seeing your answer which is quite similar to my last revision... Apr 23, 2018 at 1:12
21

Exiftool will list the Frame Count as part of its extensive output.

This a command and output of an Animated Gif file:

$ exiftool -b -FrameCount giphy.gif
33

Full exiftool output on the gif:

$ exiftool giphy.gif
ExifTool Version Number         : 10.10
File Name                       : giphy.gif
Directory                       : .
File Size                       : 2.1 MB
File Modification Date/Time     : 2018:04:22 21:00:42-04:00
File Access Date/Time           : 2018:04:22 21:01:03-04:00
File Inode Change Date/Time     : 2018:04:22 21:00:52-04:00
File Permissions                : rw-rw-r--
File Type                       : GIF
File Type Extension             : gif
MIME Type                       : image/gif
GIF Version                     : 89a
Image Width                     : 500
Image Height                    : 281
Has Color Map                   : Yes
Color Resolution Depth          : 7
Bits Per Pixel                  : 7
Background Color                : 127
Animation Iterations            : Infinite
XMP Toolkit                     : Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27
Creator Tool                    : Adobe Photoshop CS6 (Macintosh)
Instance ID                     : xmp.iid:C312DB1DA6A311E5B8F5D2BA262E999D
Document ID                     : xmp.did:C312DB1EA6A311E5B8F5D2BA262E999D
Derived From Instance ID        : xmp.iid:C312DB1BA6A311E5B8F5D2BA262E999D
Derived From Document ID        : xmp.did:C312DB1CA6A311E5B8F5D2BA262E999D
Frame Count                     : 33
Duration                        : 2.31 s
Image Size                      : 500x281
Megapixels                      : 0.141
3
  • 3
    You don't need to grep exiftool's output: every field can be printed independently: exiftool dev/slack-emojinator/gifs/yj_batsu.gif -FrameCount and exiftool dev/slack-emojinator/gifs/yj_batsu.gif -b -FrameCount for just the value
    – muru
    Apr 23, 2018 at 1:55
  • 1
    @muru Thanks for the information. I'll fix the answer. Apr 23, 2018 at 2:01
  • Here's some useful information: On Ubuntu (21.10), the exiftool program is found on the libimage-exiftool-perl package and can be installed with apt install libimage-exiftool-perl. On Arch Linux (2021), it is found on the perl-image-exiftool package. Nov 16, 2021 at 21:23
10

One-liner command

Inspired by this article, use this one-liner command:

$ identify drop_caches.gif | wc -l
128

For more details use:

identify -verbose filename.gif

This website lets you upload your .gif and analyzes it many ways including the number of frames in it:

ezgif.png


The convert provided by Image Magic will convert your .gif into individual .png images one for each frame:

$ convert -verbose -coalesce drop_caches.gif drop_caches.png
drop_caches.gif[0] GIF 470x940 470x940+0+0 8-bit sRGB 256c 177KB 0.090u 0:00.099
drop_caches.gif[1] GIF 13x1 470x940+398+704 8-bit sRGB 256c 177KB 0.080u 0:00.089
drop_caches.gif[2] GIF 306x620 470x940+144+130 8-bit sRGB 256c 177KB 0.080u 0:00.089
    (... SNIP ...)
drop_caches.gif=>drop_caches-125.png[125] GIF 470x940 470x940+0+0 8-bit sRGB 255c 50.3KB 27.100u 0:24.890
drop_caches.gif=>drop_caches-126.png[126] GIF 470x940 470x940+0+0 8-bit sRGB 254c 48.9KB 27.320u 0:25.089
drop_caches.gif=>drop_caches-127.png[127] GIF 470x940 470x940+0+0 8-bit sRGB 254c 48.9KB 27.480u 0:25.269
7

FFmpeg way:

ffprobe -v warning -show_frames qqq.gif | grep -c '\[/FRAME\]'

Gif video is a video, so video analysis tools may also help.

2
  • My experience is that compiling FFMPEG to meet my needs is a bitch, but I do appreciate the time it took to make this answer.I hope someone finds it useful
    – j0h
    Nov 25, 2019 at 18:37
  • @j0h, Doesn't distro-provided FFmpeg just work for this?
    – Vi.
    Nov 27, 2019 at 21:53
5

You can use less command too if you have ImageMagick installed.

less +G image.gif

Total frames is the last # in brackets in image.gif[#]... (starting from 0); Or count with:

less image.gif | wc -l
1
  • intriguing.......
    – j0h
    Apr 26, 2018 at 0:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .