One of the cool things about working for TechSmith is I’ve become interested in some of the *lower* level aspects of the Flash platform—namely the swf file format spec. So with the customary confidence of a programmer with a Political Science degree I set off to see if I could parse the swf header to extract the following information:
- file signature
- version
- size
- width
- height
- frame rate
- frame count
After much gnashing of teeth and a quick lesson in bit shifting from Tom Sirgedas, a C++ dev on the Camtasia Studio team, I developed a utility that parses uncompressed swf files. The big caveat is that it only parses uncompressed swf files—being a java rookie, I’m still working on how to “inflate” a compressed swf file.
However, rather than just sitting on this until I find the time and inclination to inflate compressed swf’s I’m going to release this utility / code to the community under the MIT license. If you are a swf file format or java master and want to post back thoughts or ideas I’d certainly appreciate it.
Jar file usage: java -jar swfheader.jar [swf file]
Download swf header parser jar file.
Download java source (code was condensed into a single file for web download).
I’m not sure how useful this is, but it could have some application in server-side code. For instance, perhaps you allow users to upload swf files to a CMS system. When a request to play back the file comes, the server can quickly parse the header for the width and height to be used when generating the embed tags.
The real value here is that of a lightweight example of parsing the header in Java. Reading the file format specification isn’t a whole lot of fun if you’re coming at it from the non-computer science background that many of us in the Flash community have. If you’re interested in a full blown framework for reading and writing swf files with Java checkout Flagstone’s Transform SWF library (and source code).
7 Comments
email me and I am happy to fill you in on the whole inflate side of it, I already have a coldfusion based info extractor for the flash format…
Marcel, I’d love to hear more about your Coldfusion-based extractor!
As the implementation of unsigned right shift in Java is not as expected, your parseHeader() does not work for all flash files. To fix the problem, simply change the following line
nbits = ( rect[0] >>> 3 );
to
nbits = (rect[0] & 0xff) >> 3;
For more details:
http://www.cs.utsa.edu/~wagner/laws/Abytes.html
Great catch Anson! I’ll get the files updated.
Thank you for this very simple yet useful tool.
I notice some of these files you have offered inherit from SWFCompression
and or require an object type PackedBitObj but I don’t see these source files offered, are they available as well?
Thanks!
I found PackedBitObj but still don’t see SWFCompression
Thanks!
One Trackback
[...] parse the … in a full blown framework for reading and writing swf files with java checkout …http://www.brooksandrus.com/blog/2006/08/01/lightweight-swf-header-reader-java/Java Media Framework - PLAY SWF FILE IN FRAMEPLAY swf FILE IN FRAME Nov 29, 2006 3:04 PM … Re: [...]