SWF info and Frame Rate extraction

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 analflash.php on line 15

6. My Solution

I finally found out the solution I wanted : a ready-made binary-level analyser written in PHP. All we're left to is write a correct instanciation 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 : AnalFlash.php.txt (remove .txt to use the File ; do Save Link As or Click and Save As the displayed text file)


<?php
//
// AnalFlash.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 <a href=mailto:vgr@europeanexperts.org?Subject=AnalFlash>VGR</a> for <a href='http://www.edainworks.com/'>Edaěn Works Ltd</a> - uses swfheader class
© 2004 Carlos Falo Hervás<hr>file {$_GET['file']} <br>dimensions $thedim px<br><pre>";
  print_r($video);
  echo '</pre>';
}
?>

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

Result:


AnalFlasp.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
Best regards,
Vincent Graux (VGR) for European Experts Exchange and Edaěn Works  back to list of test scripts
Last update 2021-03-15 16:05:50