An article by ERT Mentor VGR
To extract ***from PHP*** various SWF file information, in particular the difficult-to-obtain "Frame Rate":
The first idea is of course not to reinvent the wheel and search for an already-made solution:
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.
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.
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 ;-)
$video= new SWFVideoStream ($_GET['file']);
echo "number of frames is ".$video->getNumFrames().'<br>';
Fatal error: Cannot instantiate non-existent
class: swfvideostream in FlashGetFrameRate.php on line 15
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.
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
}
?>
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.
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.
You can extract any kind of header field using the same script and looking at the print_r() results.
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)