Sunday, April 24, 2011

Organizing my /gif

I have accumulated MANY gif files over the year. The problem is a gif can both be static or animated. I wanted all my animated gifs in one folder. After a little research on wikipedia I discovered that the original gif specification didnt allow for animation. Gifs that animate have a header containing the value "NETSCAPE2.0". Knowing this, it was trivial to sort animated vs static gifs. As shown in the following bash one liner:

mkdir animooted_gifs; for i in *; do if head $i | grep 'NETSCAPE2.0'; then mv $i animooted_gifs/$i; fi;done

Lol, i was creating this nice long script that would do checks and verifications and all that crap...meh...one liner ftw...

A little explanation:
A search through wikipedia taught me that animated GIFs should contain a certain header value: "NETSCAPE2.0". The bash line, searches for all files in a directory, greps the file for the header NETSCAPE2.0 and moves the file to animooted_gifs directory. If it doesnt detect NETSCAPE2.0, it moves on to the next file.

Running the unix util "file" on the gif doesnt yield much. As you can see below, a static gif and an animated gif report the same properties (obviously the size will be different since they are two different gifs):

gifs/1266641860129.gif: GIF image data, version 89a, 1003 x 1217
gifs/1245733506951.gif: GIF image data, version 89a, 375 x 375


BUT, if you run strings through the files you will see something different:

$ strings 1245733506951.gif
GIF89aw
33 84...

$ strings 1245733506951.gif
GIF89aw
33 84
...
NETSCAPE2.0
...


If you have been paying attention, you will know which file is the animated one (hint, its the 2nd one).

NOTE: Some files are constructed in a retarded way for some reason and don't have the NETSCAPE2.0 header even though they are animated. I have found this to be a rare exception to the rule. In any case, my one-liner should sort the vast majority of the animated GIFs into the correct location.

No comments:

Post a Comment