SWF Parser (AIR application)

Simple, but useful tool to copy image resources from your SWF files.

SWF Parser

Download: SWFParser.air (Adobe AIR is required to run this software).


Working on GUI for Flash Panorama Player I noticed there is no appropriate library for SWF authoring. I tried free libraries like sswf and swf tools, also I checked commercial ActiveX products for SWF authoring/transformation like SWF Generator and SWF Scout. They are not bad for simple manipulations on old versions of SWF files, but all of them have big problems with 9th version of Flash format, also practically they are not working with SWF library items (only with scene objects). What a pity…

Now there is a question: why not using Actionscript for SWF manipulations? I can’t find any info about this except some unanswered questions on different Flash forums. This strange because Actionscript 3 has all capabilities to work with SWF: ByteArray, zlib library, png/jpg decoders and loadBytes() function to convert the result into DisplayObject and test it on the fly. This gives a possibility to create online tools for SWF transformation. For example: there is a big problem to load and control Actionscript 1-2 files inside Actionscript 3 application (no direct communications between AVM1 and AVM2). SWF library would allow disjointing SWF resources (images, vector shapes, movieclips and so on) and control them separately. Another way: just insert some communication object like SWF Adapter to AS2 application on the fly (one additional SWF tag) and convert it into AVM1 movie.

Together with new Adobe AIR technology this will provide a powerful platform for offline SWF authoring (multiplatform: single application for Windows and Mac, and for Linux a little later).

So, SWF parser is a test of technology. Features:

  • Parse SWF format (all versions).
  • See SWF file info (version, size, FPS) and tags structure.
  • Extract all bitmap images.
  • Type path to any local or internet SWF file.
  • Drag any swf files to the application (one or more).
  • Drag direct URL to SWF file from browser’s address bar.
  • Drag extracted images to any folder to save them.
  • No image quality reduction: jpeg files are saving as is, without recompression.
  • It works for Windows and Mac.
  • It’s only 17 kb the whole application.
  • Free for use (and partially open source).

This is an example how to parse SWF file (I believe this will be useful if you want to continue this idea).
Place it to the first frame in Flash CS3 or insert it to your AS3 class.

/*
Simple SWF parser
Written by Denis V. Chumakov 
http://flashpanoramas.com/blog/
Use this code without any restrictions.
*/

// log string
var parseLog:String;

// handlers for SWF tags
// for example: 
// handlers[6] = parseJPEG; 
// to set your handler for DefineBitsJPEG tag.
// you can find SWF file specifications in Google.
var handlers:Array = [];

// parse SWF file
function parseSWF(data:ByteArray) {
	parseLog = "";
	data.endian = Endian.LITTLE_ENDIAN;
	var format:String = data.readUTFBytes(3);
	var compressed:Boolean = format=="CWS";
	if (format=="FWS" || format=="CWS") {
		parseLog += "SWF version "+data.readByte();
		parseLog += ", size: "+data.readUnsignedInt();
	} else {
		parseLog += "Not a Flash file.";
		return;
	}
	data.readBytes(data);
	data.length -= 8;
	if (compressed) {
		data.uncompress();
	}
	data.position = 0;
	var frame:Array = readBox(data);
	parseLog += "\n";
	parseLog += "Width: "+Math.round((frame[1]-frame[0])/20);
	parseLog += ", height: "+Math.round((frame[3]-frame[2])/20);
	var fps_f:uint = data.readUnsignedByte();
	var fps_i:uint = data.readUnsignedByte();
	parseLog += "\n";
	parseLog += "FPS: "+(fps_i+fps_f/256);
	var count:uint = data.readUnsignedShort();
	parseLog += "\n";
	parseLog += "Total frames: "+count;
	parseLog += "\n";
	while (data.bytesAvailable) {
		readSWFTag(data);
	}
	trace(parseLog);
}

// read SWF tag and call handler if present
function readSWFTag(data:ByteArray) {
	var tag:uint = data.readUnsignedShort();
	var id:int = tag>>6;
	var size:int = tag&0x3F;
	if (size == 0x3F) {
		size = data.readUnsignedInt();
	}
	parseLog += "\n";
	parseLog += "Tag "+id;
	if (handlers[id]!=null) {
		var dump:ByteArray = new ByteArray();
		if (size!=0) {
			data.readBytes(dump,0,size);
		}
		handlers[id](tag, id, size, dump);
	} else {
		data.position += size;
	}
	parseLog += "\tsize: "+size;
}

// read compressed box format
function readBox(data:ByteArray):Array {
	var c:Array = [];
	var current:uint = data.readUnsignedByte();
	var size:uint = current>>3;
	var off:int = 3;
	for (var i:int=0; i<4; i+=1) {
		c[i] = current<<(32-off)>>(32-size);
		off -= size;
		while (off<0) {
			current = data.readUnsignedByte();
			c[i] |= off<-8?current<<(-off-8):current>>(-off-8);
			off += 8;
		}
	}
	return c;
}

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loadComplete);
// load complete handler
function loadComplete(event:Event) {
	parseSWF(loader.data);
}
loader.load(new URLRequest("myfile.swf"));

This entry was posted on Monday, July 2nd, 2007 at 5:34 pm and is filed under AIR, Flash. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

23 Comments so far

  1. This is freaking AWESOME!!!!! Thanks!!!

  2. Hehehe…. I was expecting some panoramic related post! ;)
    But this one is great! Thanks for letting us learn from your experience! :)

  3. Hello i can’t understand the swf parser utility !
    Maybe because i’m not regular with Action Script and English (i’m from France).
    Is there a relation with panoramic ?
    If i only have Flash MX 2004, can I use it ?

    Thanks for help

  4. I am extremely disappointed in this product. The link to the “support” page is “file not found”, the file doesn’t know how to handle NORMAL STANDARD 360 images (one image, not six pieces — I NEVER heard of that in 10 years of doing pano). There was never any HINT of that in the initial selling page.

    Now what?

  5. Support forum was reinstalled because of spammers. Use http://flashpanoramas.com/forum/
    Wrote you a mail about format convertion. It’s not a big problem to get six cube faces, Apple Quicktime is using this format more then 10 years.

  6. 2Claude: No, it’s a new type of offline applications, you do not need anything except Adobe AIR to use it (the same for Win and Mac).
    Will be very explotable in year or two, believe me. :)

  7. Hi, This is a great utility and a great example of why AIR is such a powerful technology. I’m currently putting together a searchable index of AIR applications at http://www.theairbay.com/ (should be live within a week or two) and I’m including your app in the index. I have a few questions regarding the entry for the SWF parser. Feel free to reply via email if you want.

    1. What version is this?
    2. Is it being released under a particular license?
    3. Is there a better description than what I currently have? (“Simple, but useful tool to copy image resources from your SWF files.).
    4. Will you download link remain the same or will I need to update it in the future.

    As a fellow Flash developer…thanks for pushing the envelope; it’s always a pleasure to watch :) Keep up the great work.

    Patrick

  8. Thanks for sharing this code. There is one piece of code I simply can not get my head around, its the readBox method. Would you be willing to send me an email with explaination (if you can find the time), I would be very gratefull. The line that gives me most problems is

    current<>(32-size)

    At the start off is 3 and size is 15, so in simple terms it would be

    current <> 17

    What does this do… Hmm while typing this I think I got a clue. I was under the impression that you could only push of (or discard) bits on the right side, hehe. But integers can be only 32 bits in size, so you are pushing them out on the left aswell.

    Phew, what a discovery. Thanks for sharing your code, I am learning loads!

    Greetz Erik

  9. When i try to install it I get an error – this application needs an older version of air that is no longer supported. please contact the vendor and ask him to update his program.

    It looks really neat, but could you update the program?

  10. Are you developing this to save swf files? I would love to contribute to that effort somehow

  11. I’m also getting the message about need an older Air runtime. Are you planning an update? I can’t seem to find anything but the latest Air palyer…

    Thanks!

  12. Just wanted to say a quick thanks for posting your code; I literally spent the entire day looking for a swf bytecode manipulation library and am happy to say that this is a great foundation for building just that!

    Hope you continue to flesh out this class and look forward to seeing what else you come up with.

    Thanks again!

  13. Wow great site! Some really helpful information there.
    I??m sorry for little off-topic, but I want to ask you about design of this site.
    Did you make this template yourself or got from any templates website?
    Looks pretty cool for me. Wonderful well this reading.

  14. Hi Denis,

    I was going thru the code and description.Its pretty much neat.I really admire your knowledge on this.
    I am looking for something little diffferent.As of now I am new to this SWF and FLV thing.However,I am doing my homework to get acquainted to it.My issue is to find some programmable way to find if SWF has an embedded FLV in it.Can you guide me in this or point me to some related stuffs which helps me in finding out if the swf contains an embedded FLV?

    Thanks in advance,
    Puneet

  15. Hi Denis,

    I’ve been searching for a way to load in an external SWF with some user-editable input fields in it, and then save the SWF out again, with the string values they entered intact. (Like form fields in PDFs.).

    Haven’t found anything yet, but wonder if it’s possible with something based on your code? Do you have or know of a way of doing it? (I don’t have the time to decode and understand the Flash file spec unfortunately)

    Thanks for any help :)

  16. Great… Thanks a lot…

  17. great!!!
    extremely helpfull man!!! thanks just what I was looking for!!!
    I was looking for some info on manipulating swf filez with AIR… and its just here…

  18. For some reason I couldn’t get this to install. What version of Air did you compile it with? I wrote a small Air 1.0 app using this parsing code: http://www.janitor61.com/?page_id=3 I can post the source if anyone is interested.

  19. Hey there, thanks for putting this together. Couple questions for you. Where did you find the name’s of the tags? Foe example 0x4C – you have “DEFINECLASS”, I don’t see that documented anywhere in the SWF file format. Also, the air file you have for download is outdated, so I can’t install it. I was also wondering if you would post the source somewhere. I’m trying to figure out how to extract class definitions from the SWF bytecode – basically like a glorified exclude classes.

    Thanks
    Aaron

  20. Hello,

    i’m having the same problem like Pineappleclock, cannot install cause it tells me ‘i need an air update which is not available for my system’
    …very strange…running vista with air 1.5 installed

  21. Hello, Neat post. There is a problem with your web site in internet
    explorer, might check this? IE still is the marketplace
    chief and a huge portion of people will pass over your fantastic writing
    due to this problem.

    My web-site :: website :: Anton ::

  22. Downloading Cube planet typically costs funds. I will give you Dice
    World full variation which you can download listed here
    for totally free. Of course you will be in a position to perform Cube Globe in a couple of minutes right here for free,
    all you have to do is down load Cube World with the website link
    provided right here. We integrated Cube Planet crack so soon after
    you install Cube Entire world Alpha, you can perform it instantly.

    Cube Entire world is a 3D action RPG kind match that is created/designed by Wollay and his spouse.
    Cube Globe was started out in June 2011
    and now, the Alpha edition is out previously.

  23. Downloading Cube world normally charges money. I will give
    you Cube Globe total version which you can download here for
    totally free. Of course you will be able to engage in Cube Planet in a handful of minutes
    here for free, all you have to do is obtain Cube Globe with the hyperlink presented below.
    We included Dice Globe crack so after you put in Dice
    Globe Alpha, you can perform it immediately. Cube Entire world
    is a 3D action RPG type recreation that is created/developed by Wollay and his wife.
    Dice World was started out in June 2011 and now, the Alpha version is out presently.

Have your say

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URIs must be fully qualified (eg: http://www.domainname.com) and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.