#1 2009-08-27 14:09

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Number of frames in a GIF

I wanted to know the number of frames on a gif, so I searched for information and I found this on the wikipedia:
"21 F9 04      Graphic Control Extension frame"
I checked it with an HEX editor and its correct

So I made this script but doesn't work

var
  GifFrames: TStringsArray;
 
begin
  GifFrames := MatchesRegEx(FileReadContent(FilePath), '\x21\xF9\x04', False);
  FileName := IntToStr(Length(GifFrames));
end.

Someone knows where is the problem?

Last edited by SafetyCar (2009-08-27 14:11)


If this software has helped you, consider getting your pro version. :)

Offline

#2 2009-08-28 06:36

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Number of frames in a GIF

A few quick tests seem to indicate that MatchesRegEx() doesn't read the string past the first NULL (\x00) character in the file. Which seems a bit strange to me (perhaps a bug?), though it might also be a limitation of the RegEx engine, since it might not be built to handle binary data. At the very least, although your code seems like it "should be correct" in most cases, it's certainly not robust by any means... a proper thing to do would be to parse the GIF file structure, kinda like what I was doing with digging out the AVI video codec handler.

As far as to complete your original train-of-thought with your program, I rewrote it in a brute-force style... this, at least, outputs the expected result.

const
  GIF_FRAME_MARKER = #33 #249 #4;
var
  GifData: String;
  Count: Integer;
  GifPos: Integer;
begin
  Count := 0
  GifData := FileReadContent(FilePath);
  for GifPos := 1 to (Length(GifData)-2) do
  begin
    if (GifData[GifPos] = GIF_FRAME_MARKER[1]) then
      if (GifData[GifPos + 1] = GIF_FRAME_MARKER[2]) then
        if (GifData[GifPos + 2] = GIF_FRAME_MARKER[3]) then
          count := count + 1;
  end;
  FileName := IntToStr(Count);
end.

Last edited by prologician (2009-08-28 06:38)

Offline

#3 2009-08-28 07:22

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Number of frames in a GIF

Thanks for the help, I will use it unless Denis confirms the bug.


But I'd like to know a little more about how it works.

Well as I understand searchs the first character, and when it finds it, looks if the next is also what it should be, an the same for the 3rd.

But I'm missing something about the const GIF_FRAME_MARKER:
Is a pascal function to recognize a number as character if there is a # in front on it?
or... its an array?
or... I don't know... I'm lost hmm


If this software has helped you, consider getting your pro version. :)

Offline

#4 2009-08-28 11:38

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Number of frames in a GIF

21 F9 04 (Hex) = #33 #249 #4 (Decimal representation in PascalScript)

The way it's used, it certainly seems like a const array of characters.

Offline

#5 2009-08-29 01:37

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Number of frames in a GIF

Andrew is indeed correct that I stated the "graphic control extension frame" explicitly as the constant GIF_FRAME_MARKER, using a series of characters. Since we were looking through the raw bytes of the file, I figured that this would be suitable.

However, GIF_FRAME_MARKER is actually a String. One can index into strings in a manner similar to arrays... the difference being that strings are 1-indexed (that is, the first character of the string is at position 1). Arrays are generally 0-indexed, unless you explicitly override it (or something.... DelphiBasics goes into more details about it).

You can also double-check this by trying to assign GIF_FRAME_MARKER to a variable.... it works for a String, but spits a "Type Mismatch" error for an Array of Char.

Offline

#6 2009-12-29 17:07

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Number of frames in a GIF

I don't know if someone else is interested in this script but I found a way that is much faster...  big_smile  big_smile  big_smile

(The technique is the same that I tried at first, a TStringsArray, but now without RegEx function)

var
  GifFrames: TStringsArray;
 
begin
  If WideExtractFileExt(FileName)='.gif' then
  begin
    GifFrames := WideSplitString(FileReadContent(FilePath), Chr(33)+Chr(249)+Chr(4));
    FileName := WideExtractBaseName(FileName) + ' (frames=' + IntToStr(Length(GifFrames)-1) + ').gif';
  end;
end.


If this software has helped you, consider getting your pro version. :)

Offline

#7 2010-01-06 22:22

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,376

Re: Number of frames in a GIF

I have added a new meta tag: GIF_Frames, in the latest beta version! smile

Offline

#8 2010-01-08 13:42

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Number of frames in a GIF

Oh, thanks for adding that feature.

I'd like to ask you a little bit more, because my script had some problems:
- some times appeared 0 frames and other 1 (minor problem, easy to solve)
- in some cases it counted 1 frame more than it should (some-how important)

But your code in a few cases times, also counts 1 frame more than it should... but it's not failing in the same files that mine...


Could you comment something  roll about your code? I'm intrigued why is this difference, and may be we can find something even better...


By the way, revising the info about GIFs
http://en.wikipedia.org/wiki/Graphics_I … nge_Format

I realized that the GCE (21 F9 04) should be preceded by null (00) this makes my code more efficient in almost every cases, but, there are a very few where before was counting ok but now counts 1 less than it should. (Even though, is better because its failing in less files that yours, and mine codes where failing)

Last edited by SafetyCar (2010-01-08 13:49)


If this software has helped you, consider getting your pro version. :)

Offline

#9 2010-01-08 17:23

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Number of frames in a GIF

There is a chance those GIFs have not been encoded in a standard way somehow... SafetyCar, can you take the GIFs that are failing with the 2nd version of your script, open them in any graphics program, just save them as another GIF with the same settings (i.e. no. of bits, color depth etc.) with no editing, and then try with these new files? If they work fine, then that might point to non-standard GIFs after all.

Offline

#10 2010-01-08 18:30

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Number of frames in a GIF

I think you are right, must be a standards problem

I opened some of those files and saving again without changing anything, the new file is counted correctly  roll

Last edited by SafetyCar (2010-01-08 18:31)


If this software has helped you, consider getting your pro version. :)

Offline

Board footer

Powered by FluxBB