HOME FORUM ARTICLES TUTORIALS SCRIPTS LINKS NEWS MENTORS TOOLS REGISTER

SWF Frame Rate Extraction

An article by ERT Mentor VGR

1. Purpose

To extract ***from PHP*** various SWF file information, in particular the difficult-to-obtain "Frame Rate":

2. Introduction

The first idea is of course not to reinvent the wheel and search for an already-made solution:

3. Third-party Tools

The tools I found and tried are usually:

The conclusion of this tools analysis is that I found them:

So I had to find a really good way to extract information from a SWF file. I had two trivial ways to go : (a) find an already-made SWF Header Extractor, or (b) work at the binary level.

At first, of course, I turned to the PHP documentation (http://www.php.net/manual/en/) and found out the LibSWF and Ming libraries as candidates.

4. PHP common libraries - LibSWF

PHP has since long (PHP 4 RC2) "Shockwave Flash Functions" (see http://www.php.net/manual/en/ref.swf.php)
The two problems are that (a) it is not available on Windows, and I don't want to use a custom port or emulator (Cygwin...), (b) it can only CREATE SWF files. There is no GetFrameRate() function and no Getter at all.

5. PHP common libraries - Ming

Ming is now a standard library and was added in PHP 4.0.5. See http://www.php.net/manual/en/ref.ming.php.
The "only" problem is that the necessary SWFVideoStream object is only available in the CVS version of the current PHP ;-)

PHP code:


$video= new SWFVideoStream ($_GET['file']);
echo "number of frames is ".$video->getNumFrames().'<br>';

Results:


Fatal error: Cannot instantiate non-existent 
class: swfvideostream in FlashGetFrameRate.php on line 15

6. My Solution

I finally found out the solution I wanted : a ready-made binary-level analyzer written in PHP. All we're left to is write a correct instantiation of the new class and use the defined methods to get the frame rate. Or I could have extended the class to add a GetFrameRate() getter.

The Library Header:

This a well-written little-known PHP class of 6877 bytes and which deserves better visibility on the Web, even if it is known since 2004 ;-)
You can already find it at :


<?php
//---------------------------------------------------
// SWF HEADER - version 1.0
// Small utility class to determine basic data from a 
// SWF file header Does not need any php-flash extension, 
// based on raw binary data reading
//----------------------------------------------------
//   SWFHEADER CLASS - PHP SWF header parser
//   Copyright (C) 2004  Carlos Falo Hervás
//   This library is free software; you can redistribute 
//   it and/or modify it under the terms of the GNU Lesser 
//   General Public License as published by the Free 
//   Software Foundation; either version 2.1 of the License, 
//   or (at your option) any later version.
//-----------------------------------------------------

class swfheader {
  var $debug ;          // Output DEBUG info
  var $fname ;          // SWF file analyzed
  var $magic ;          // Magic in a SWF file (FWS or CWS)
  var $compressed ;     // Flag to indicate a compressed file (CWS)
  var $version ;        // Flash version
  var $size ;           // Uncompressed file size (in bytes)
  var $width ;          // Flash movie native width
  var $height ;         // Flash movie native height
  var $valid ;          // Valid SWF file
  var $fps ;            // Flash movie native frame-rate
  var $frames ;         // Flash movie total frames
  //----------------------------------------------------    
  // swfheader($debug) :    Constructor, basically does 
  //                        nothing but initilize
  //                        debug and data fields
  //----------------------------------------------------
  function swfheader($debug = false) {}
  //----------------------------------------------------    
  // init() : initialize the data fields to "empty" values
  //----------------------------------------------------
  function init() {}
  //----------------------------------------------------
  // loadswf($filename) : loads $filename and stores data 
  // from it's header
  //----------------------------------------------------
  function getDimensions($filename) {} 
  // returns String ; object has been updated
}
?>

My PHP code:

Local Copy on this Server : FlashGetFrameRate.txt


<?php
//
// FlashGetFrameRate.php : analyze of a Flash (SWF) file
//
//VGR09062006 Creation with Ming to get the fps of an SWF "video"
//
// TODO : Nil
//

if (!isset($_GET['file'])) { 
   die("Please feed me a filename in 'file' parameter"); 
   exit; }

if (strlen(trim($_GET['file']))==0) die("Empty filename");
else if (!file_exists($_GET['file'])) 
     die("Could not open file '{$_GET['file']}'");
else { // let's go
  //$video= new SWFVideoStream ($_GET['file']); 
  // type : SWFVideoStream (VGR REM : unavailable, CVS only)
  //echo "number of frames is ".$video->getNumFrames().'<br>';
  require('swfheader.class.php');
  $video=new swfheader();
  $thedim=str_replace('|','x',$video->getDimensions($_GET['file']));
echo "AnalFlasp.php v 1.0 ©2006 VGR for ";
echo "Edaěn Works Ltd - uses swfheader class ";
echo "© 2004 Carlos Falo Hervás<hr>file {$_GET['file']} <br>";
echo "dimensions $thedim px<br><pre>";
  print_r($video);
  echo '</pre>';
}
?>

You may see it in action at http://www.fecj.org/extra/AnalFlash/FlashGetFrameRate.php and one file available locally is monster_NL.swf.

Result:


FlashGetFrameRate.php v 1.0 ©2006 
VGR for 

Edaěn Works Ltd - 
uses swfheader class © 2004 Carlos Falo Hervás
file monster_NL.swf
dimensions 425x600 px

swfheader Object
(
    [debug] => 
    [fname] => monster_NL.swf
    [magic] => CWS
    [compressed] => 1
    [version] => 6
    [size] => 46871
    [width] => 425
    [height] => 600
    [valid] => 1
    [fps] => Array
        (
            [0] => 0
            [1] => 18
        )

    [frames] => 1
)

And the Frame Rate is ... 18 fps. CQFD.

8. Extending the technique

You can extract any kind of header field using the same script and looking at the print_r() results.

9. Conclusion

This technique is extremely simple and efficient and does the job well.
Many many thanks to the Author of the Library : Carlos Falo Hervás (SP) CarlosFaloHervas

© Copyright VGR 2006-2007

post to Dzone Digg this! Add to del.icio.us Googleize this Add to reddit Save to myYahoo Add to furl Add to Netvouz! Spurl this! Add to Linkroll! Save to Simpy Give if thumbs up on StumbleUpon Save to Blinklist Add to Tektag Save to Bibsonomy Submit to Tweako
Search ERT on the Tools Page

Did you know? You can discuss this article with the mentor who wrote it and others interested in the topic? You are invited to join the discussion with Go to the forum

Got a technical article or tutorial you want to publish on the Internet? Join Go to the forum in the Round Table Forum and let the Mentors know what you have. If it meets ERT standards, is factual and can help ERT visitors, then ERT Mentors and Editors can help you (without charge) polish your offering so it can be published and promoted by ERT. An article published on ERT may be read by as many as 10,000 visitors a week; promoting you, your site, and your ideas. Please note ERT does not publish re-prints; promotional handouts, or pieces consisting mainly of links. So original technical content only please. If you prefer you can email the Editor