# When importing GIF animation, how to find the correct list of “DisplayDurations”?

When importing GIF animations with variable frame durations, the settings for "DisplayDurations" are not recovered correctly. As an example, I'm using the movie from this earlier question:

Here I'm repeating the code to generate it, putting the gradually increasing frame durations in a variable durations:

frames =
Table[Graphics[
Text[Style["Slow Down", FontFamily -> "Futura", FontColor -> Blue,
FontSize -> 48], {0, y}], Background -> Cyan,
PlotRange -> {{-1, 1}, {.1, 1.5}}], {y, 1.2, .2, -.1}];
durations = Append[Range[Length[frames] - 1]/20, 2];
Export["slowDownMovie.gif", frames, "DisplayDurations" -> durations,
"AnimationRepetitions" -> Infinity];

durations//N


{0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 2.}

Now compare to the durations that we get when re-importing the same animated GIF:

newDurations = Import["slowDownMovie.gif", "DisplayDurations"]


{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.}

newDurations == durations


False

As expected from this negative result, ListAnimate also plays the animation at an incorrect, constant frame delay. It seems clear that this is a bug.

Before I start looking for a platform-specific hack to fix this: is there any way to get the correct display durations from a GIF with the Import function? I looked into the "Frames" element of the GIF import, but that also seems to have the wrong display duration in it.

I haven't looked at the raw data, but if one has to do that it may be better to find a non-Mathematica hack instead.

Edit

Wolfram support has confirmed it's a bug [TS 19773].

-
Well, I would mark that as a bug. –  Ajasja May 9 '12 at 9:08
@Ajasja I agree, but IMHO it is best if the person posting the question doesn't make that call. –  Mr.Wizard May 9 '12 at 9:11
Try to get the durations of this one. I get a set of infinities. I'd say, a bug. –  Sjoerd C. de Vries May 9 '12 at 18:35

I don't know if you can persuade Import to return the correct durations, but luckily the format of .gif files is pretty straight forward so it's not that hard to extract the correct durations manually from the raw data.

In an animated gif the frame durations are stored in a so called Graphic Control Extension or GCE preceding each frame. A GCE starts with the byte sequence 21 F9 04 followed by 4 data bytes and is closed by 00. Of these data bytes, the second and third byte are the frame duration in hundreds of seconds ordered least significant byte first.

So a crude way to extract the frame durations is to read in the file, find the positions of the byte sequence 21 F9 04 and extract the fourth and fifth byte after each of these positions, e.g.

lst = BinaryReadList["~/test.gif"];
seq = FromDigits[#, 16] & /@ StringSplit["21 f9 04"];
pos = Position[Partition[lst, 3, 1], seq];
durations2 = (Extract[lst, pos + 4] + 256 Extract[lst, pos + 5])/100

(* ==> {1/20, 1/10, 3/20, 1/5, 1/4, 3/10, 7/20, 2/5, 9/20, 1/2, 2} *)

-