Friday, February 15, 2008

Reading Flash (Swf) metadata from ColdFusion

Wouldn't it be cool if you could read the meta-data of Swf file in ColdFusion? The most common use case I can think of is to find the dimension of swf movie so that you can set the dimension for the same in object tag while you are embedding the flash movie in your page. And the good news is that you can do that currently in ColdFusion with just 2-3 lines of code!! All you need to do is to create a TagDecoder and decoder the header with it. Here is the complete code.

fis = createObject("java", "").init("C:\test.swf"); // Create the FileInputStream
decoder = createObject("java", "macromedia.swf.TagDecoder").init(fis); // create TagDecoder
header = decoder.decodeHeader(); // Decode the header.
rect = header.size;
Is Compressed : #header.compressed#<br>
Frame Count : #header.framecount#<br>
Frame rate : #header.rate#<br>
Version : #header.version#<br>
Height : #rect.getHeight()#<br>
Width : #rect.getWidth()#");

The height and width value that you see here would be in twips which is defined as 1/20 of a point or 1/1440 inch. This means, for a 72 dpi screeen, you will have to divide it by 20 to get the height and width in pixels.


Giancarlo Gomez said...

Thank you so much!!!!!!! This is great as I can use this in a current project to display movies!!!! Never knew you could do this. Great Post!

Steve Bryant said...

This is great! Where did you find this?

Rupesh Kumar said...

Thanks guys! It will be great if you could share where you need the meta-data for flash movies.

Steve, I am part of the ColdFusion engg team :)

Giancarlo Gomez said...

Hello Rupesh,

I am currently creating a site that will use this feature and once it is up I will post the link and explain how it is used. Thank you again!

Mike said...
This comment has been removed by the author.
Thomas said...

I'm trying to display the movie duration.. how can I do that?

Rupesh Kumar said...

Thomas, that will be framecount/framerate.

Thomas said...

Is Compressed : NO
Frame Count : 4608
Frame rate : 0
Version : 1
Height : 1
Width : 0

this is what I getting pulling from my file. Is it because i'm reading a .flv file.. I just want to get the duration of a .flv video to put in a database... not sure how..??