Update (Dec 20, 2008): The AMF0Parser library has been updated to better handle broken AMF0 packets. The FLVInfo2 library PHPDoc has been updated.
Update (Aug 15, 2008): The AMF0Parser library has been updated to support the Date type in the metas, added among others by the “inlet media FLVTool2″ tool.
If you’ve done Flash banners or micro-sites that embed video with cue points for synchronization before, you know that it’s a pain in the arse. There doesn’t seem to be any tool around to modify the damn points once the file is encoded, so you’ve got to re-encode the file over and over to have your things synced correctly.
If you’ve tried to reverse engineer the FLV format before, you know that it’s a pain in the arse as well. The AMF0 format is anything but intuitive, and the documentation has been lacking for a long long time — although there’s apparently an SDK now. Luckily, there was OSFlash, and SabreAMF and AMFPHP that could be used as a documentation source.
If you recognize yourself, this might be your lucky day. After many hours of trial and error, I’ve finally been able to reverse engineer it, and to build a library that extract essential FLV information, reads the Meta, and allows you to write them back — including the cue points.
First, you’ll need to FLVInfo2 and the AMF0Parser libraries, below. The FLVInfo2 library has three main functions:
getInfo, which returns information gotten from analyzing the FLV file and it’s meta. Frame rate, bit rate, audio/video codec, width, height, etc.
( [signature] => 1 [hasVideo] => 1 [hasAudio] => 1 [minimalFlashVersion] => 8 => stdClass Object ( [codec] => 4 [width] => 320 [height] => 240 [keyframeRatio] => 0.0180537208278 [keyframeEvery] => 55.3902439024 [fps] => 15 [bitrate] => 448 [codecStr] => On2 VP6 ) [audio] => stdClass Object ( [codec] => 2 [frequency] => 22 [depth] => 16 [channels] => 2 [bitrate] => 48 [codecStr] => MP3 ) )
getMeta, which returns the meta data, and the cue points
Array ( [metas] => Array (  => Array (  => onMetaData  => Array ( [duration] => 151.46 [width] => 320 [height] => 240 [videodatarate] => 500 [canSeekToEnd] => 1 [videocodecid] => 4 [audiodatarate] => 48 [audiocodecid] => 2 [framerate] => 15 [creationdate] => Tue Jun 17 13:06:15 2008 [Encoded_By] => orangetango Video Encoder [Encoded_With] => orangetango Video Encoder [metadatacreator] => orangetango FLV meta data writer ) ) ) [cuepoints] => Array (  => Array (  => onCuePoint  => Array ( [name] => name1 [time] => 4.41 [type] => event ) )  => Array (  => onCuePoint  => Array ( [name] => name1_end [time] => 6.7 [type] => event ) ) ) )
rewriteMeta, which inputs an FLV file, meta data and cue points, and outputs a new FLV file.
As a practical example, you find the
rewriteTagsForFile function in the
example.php file, that rewrites the meta data and gets the cue points from a FLVCoreCuePoints XML file.
flvinfo2.php (20k , updated Dec 20, 2008)
AMF0Parser.php (16k, updated Dec 20, 2008)
Flash Video logo found on Wikipedia.
If you like this article, leaving a comment, tweeting ofr liking it is always appreciated.