begin process at 2010 02 10 09:55:50
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > LECTURE DES CARACTÉRISTIQUES D'UN FICHIER FLV

LECTURE DES CARACTÉRISTIQUES D'UN FICHIER FLV


 Information sur la source

Note :
Aucune note
Catégorie :Graphique Classé sous :FLV, Flash Video, Caractéristique Niveau :Débutant Date de création :23/09/2008 Date de mise à jour :25/09/2008 13:43:45 Vu :3 726

Auteur : bubulemaster

Ecrire un message privé
Commentaire sur cette source (18)
Ajouter un commentaire et/ou une note

 Description

Classe permettant de lire les caractéristique d'un fichier FLV.
Je n'ai pas fini cette classe car PHP travaille sur 32 bits or pour la durée, dans le fichiers c'est sur 64 bits.
De plus, je ne lis pas le caractéristiques vidéo et audio (à cause de la raison ci-dessus).

Source

  • <?php
  • /*
  • * Class to read Flash Video file
  • *
  • * Write by MARTINEAU Emeric (php4php@free.fr)
  • *
  • *******************************************************************************
  • * USE :
  • * $MyClass = new FLVFile(string $file_to_read, [bool $stop_when_read_one_video_and_one_audio_tag, bool $debug]) ;
  • *
  • * $file_to_read : FLV file,
  • * $stop_when_read_one_video_and_one_audio_tag : no read all file, just one audio and video tag,
  • * $debug : if true, $xxx->debugMesage : array with debug message
  • *******************************************************************************
  • * ATTRIBUTS
  • * - $fileName : file name past in constructor,
  • * - $isFileFound : true if file found,
  • * - $isFLVFile : true if FLV file,
  • * - $isReadable : true if file not read protect,
  • * - $isAudioTag : true if file contain audio stream,
  • * - $isVideoTag : true if file contain video stream,
  • * - $onMetaData : meta data of file, array,
  • * AMF_DATA_TYPE_NUMBER :
  • * "type" = AMF_DATA_TYPE_NUMBER
  • * "value" = number
  • * AMF_DATA_TYPE_BOOLEAN :
  • * "type" = AMF_DATA_TYPE_BOOLEAN
  • * "value" = true or false
  • * AMF_DATA_TYPE_STRING :
  • * "type" = AMF_DATA_TYPE_STRING
  • * "value" = string
  • * AMF_DATA_TYPE_OBJECT :
  • * "type" = AMF_DATA_TYPE_OBJECT
  • * "value" = array of data (not tested)
  • * AMF_DATA_TYPE_MOVIE :
  • * "type" = AMF_DATA_TYPE_MOVIE
  • * not implemented
  • * AMF_DATA_TYPE_NULL :
  • * "type" = AMF_DATA_TYPE_NULL
  • * AMF_DATA_TYPE_UNDEFINED :
  • * "type" = AMF_DATA_TYPE_UNDEFINED
  • * AMF_DATA_TYPE_UNSUPPORTED :
  • * "type" = AMF_DATA_TYPE_UNSUPPORTED
  • * AMF_DATA_TYPE_REFERENCE :
  • * "type" = AMF_DATA_TYPE_REFERENCE
  • * not implemented
  • * AMF_DATA_TYPE_ECMA_ARRAY :
  • * "type" = AMF_DATA_TYPE_ECMA_ARRAY
  • * "value" = array(
  • * key_name => value
  • * )
  • * AMF_DATA_TYPE_STRICT_ARRAY :
  • * "type" = AMF_DATA_TYPE_STRICT_ARRAY
  • * "value" = array(value1, value2)
  • * AMF_DATA_TYPE_DATE :
  • * "type" = AMF_DATA_TYPE_DATE
  • * "value" = date
  • * "UTC_offset" = utc offset
  • * AMF_DATA_TYPE_LONG_STRING :
  • * "type" = AMF_DATA_TYPE_LONG_STRING
  • * "value" = string
  • * AMF_DATA_TYPE_RECORD_SET :
  • * "type" = AMF_DATA_TYPE_RECORD_SET
  • * not implemented
  • * AMF_DATA_TYPE_XML :
  • * "type" = AMF_DATA_TYPE_XML
  • * not implemented
  • * AMF_DATA_TYPE_OBJECT :
  • * "type" = AMF_DATA_TYPE_XML
  • * not implemented
  • * AMF_DATA_TYPE_AMF3DATA :
  • * "type" = AMF_DATA_TYPE_AMF3DATA
  • * not implemented
  • * AMF_DATA_TYPE_UNKNOW :
  • * "type" = AMF_DATA_TYPE_UNKNOW
  • * if appear, problem in file
  • *
  • *
  • * - $warningMessage : array, warning message,
  • * - $errorMessage : array, error message
  • * - $debugMessage : array, debug message,
  • *
  • * METHODS
  • * no method
  • *******************************************************************************
  • * DOCUMENTATION
  • * http://osflash.org/flv
  • * http://code.activestate.com/recipes/457406/
  • * http://inlet-media.de/flvtool2
  • * http://www.adobe.com/devnet/flv/pdf/video_file_format_spec_v9.pdf
  • *******************************************************************************
  • * TODO :
  • * - in parseData() :
  • * - test AMF_DATA_TYPE_OBJECT,
  • * - implement AMF_DATA_TYPE_REFERENCE,
  • * - implement AMF_DATA_TYPE_RECORD_SET,
  • * - implement AMF_DATA_TYPE_XML,
  • * - implement AMF_DATA_TYPED_OBJECT,
  • * - implement AMF_DATA_TYPE_AMF3DATA,
  • */
  • class FLVFile
  • {
  • /**************/
  • /* PUBLIC VAR */
  • /**************/
  • // Name of file
  • public $fileName ;
  • // True if file found
  • public $isFileFound = false ;
  • // True id file is FLV
  • public $isFLVFile = false ;
  • // True if file can be read
  • public $isReadable = false ;
  • // True if audio tag is present
  • public $isAudioTag = false ;
  • // True if video tag is present
  • public $isVideoTag = false ;
  • // onMetaData value
  • public $onMetaData = null ;
  • // Warning string
  • public $warningMessage = array() ;
  • // Error string
  • public $errorMessage = array() ;
  • // Debug message
  • public $debugMessage = array() ;
  • /********************/
  • /* PUBLIC CONSTANCE */
  • /********************/
  • public static $AMF_DATA_TYPE_NUMBER = 0 ;
  • public static $AMF_DATA_TYPE_BOOLEAN = 1 ;
  • public static $AMF_DATA_TYPE_STRING = 2 ;
  • public static $AMF_DATA_TYPE_OBJECT = 3 ;
  • public static $AMF_DATA_TYPE_MOVIE = 4 ;
  • public static $AMF_DATA_TYPE_NULL = 5 ;
  • public static $AMF_DATA_TYPE_UNDEFINED = 6 ;
  • public static $AMF_DATA_TYPE_REFERENCE = 7 ;
  • public static $AMF_DATA_TYPE_ECMA_ARRAY = 8 ;
  • public static $AMF_DATA_TYPE_STRICT_ARRAY = 10 ;
  • public static $AMF_DATA_TYPE_DATE = 11 ;
  • public static $AMF_DATA_TYPE_LONG_STRING = 12 ;
  • public static $AMF_DATA_TYPE_UNSUPPORTED = 13 ;
  • public static $AMF_DATA_TYPE_RECORD_SET = 14 ;
  • public static $AMF_DATA_TYPE_XML = 15 ;
  • public static $AMF_DATA_TYPE_TYPED_OBJECT = 16 ;
  • public static $AMF_DATA_TYPE_AMF3DATA = 17 ;
  • public static $AMF_DATA_TYPE_UNKNOW = -1 ;
  • /***************/
  • /* PRIVATE VAR */
  • /***************/
  • // Ressource file id
  • private $fileId ;
  • // current position in file. Use sur skipData()
  • private $currentPosFile = 0 ;
  • // file size
  • private $fileSize = 0 ;
  • // Stop when found first video stream
  • private $stopAtFirstVideoAudio = true ;
  • // Make debug string
  • private $debug = false ;
  • /**********************/
  • /* PRIVATE CONSTANTES */
  • /**********************/
  • private static $FLV_TAG_TYPE_AUDIO = 8 ;
  • private static $FLV_TAG_TYPE_VIDEO = 9 ;
  • private static $FLV_TAG_TYPE_META = 18 ; /* 0x12 */
  • private static $AMF_END_OF_OBJECT = 9 ;
  • /*
  • * Constructor
  • */
  • public function __construct($file_name, $stop_at_first__video_and_audio = true, $debug = false)
  • {
  • $this->stopAtFirstVideoAudio = $stop_at_first__video_and_audio ;
  • $this->debug = $debug ;
  • if (file_exists($file_name))
  • {
  • $this->filename = $file_name ;
  • $this->fileSize = filesize($file_name) ;
  • $this->filefound = true ;
  • $this->fileId = @fopen($file_name, "rb");
  • if ($this->fileId)
  • {
  • $this->isReadable = true ;
  • $this->isFLVFile = $this->checkHeader() ;
  • if ($this->isFLVFile)
  • {
  • /* Lecture des tags */
  • $this->readTags() ;
  • }
  • fclose($this->fileId) ;
  • }
  • else
  • {
  • $this->error("Can't open file to read.") ;
  • }
  • }
  • else
  • {
  • $this->error("File doesn't exist.") ;
  • }
  • }
  • /*
  • * Check if file is valid FLV file
  • */
  • private function checkHeader()
  • {
  • $returnValue = false ;
  • /* Id */
  • $id = @fread($this->fileId, 3) ;
  • if ($id == 'FLV')
  • {
  • $this->currentPosFile += 3 ;
  • $fileVersion = $this->readUInt8() ;
  • /* file version */
  • if ($fileVersion == 1)
  • {
  • /* audio/video are present */
  • $tag = $this->readUInt8() ;
  • if (($tag & 1) != 0)
  • {
  • $this->isVideoTag = true ;
  • }
  • if (($tag & 4) != 0)
  • {
  • $this->isAudioTag = true ;
  • }
  • /* header size (always 9) */
  • if ($this->readUInt32() == 9)
  • {
  • $returnValue = true ;
  • }
  • else
  • {
  • $this->error("Header size must be 9.") ;
  • }
  • }
  • else
  • {
  • $this->error("Only version 1 of file is supported.") ;
  • }
  • }
  • else
  • {
  • $this->error("Not valid file (no found FLV tag).") ;
  • }
  • return $returnValue ;
  • }
  • /* Read Uin64 */
  • private function readUInt64()
  • {
  • $uInt64 = @fread($this->fileId, 8) ;
  • $this->currentPosFile += 8 ;
  • return (ord($uInt64[0]) << 56) | (ord($uInt64[1]) << 48) | (ord($uInt32[2]) << 40) | (ord($uInt64[3]) << 32) | (ord($uInt64[4]) << 24) | (ord($uInt64[5]) << 16) | (ord($uInt64[6]) << 8) | ord($uInt64[7]) ;
  • }
  • /* Read Uin32 */
  • private function readUInt32()
  • {
  • $uInt32 = @fread($this->fileId, 4) ;
  • $this->currentPosFile += 4 ;
  • return (ord($uInt32[0]) << 24) | (ord($uInt32[1]) << 16) | (ord($uInt32[2]) << 8) | ord($uInt32[3]) ;
  • }
  • /* Read Uin24 */
  • private function readUInt24()
  • {
  • $uInt24 = @fread($this->fileId, 3) ;
  • $this->currentPosFile += 3 ;
  • return (ord($uInt24[0]) << 16) | (ord($uInt24[1]) << 8) | ord($uInt24[2]) ;
  • }
  • /* Read Uin16 */
  • private function readUInt16()
  • {
  • $uInt16 = @fread($this->fileId, 2) ;
  • $this->currentPosFile += 2 ;
  • return (ord($uInt16[0]) << 8) | ord($uInt16[1]) ;
  • }
  • /* Read Uin8 */
  • private function readUInt8()
  • {
  • $uInt8 = @fread($this->fileId, 1) ;
  • $this->currentPosFile++ ;
  • return ord($uInt8) ;
  • }
  • /* Read byte */
  • private function readByte()
  • {
  • $this->currentPosFile++ ;
  • return @fread($this->fileId, 1) ;
  • }
  • /* Read double */
  • private function readDouble()
  • {
  • // 32 bits
  • //$val = 0xBF866666 ; // -1.05
  • //$val = 0xC1199999 ; //-9.6
  • //$val = 0x3EC00000 ; // 0.375
  • $val = $this->readUInt64() ;
  • //http://www.commentcamarche.net/base/representation.php3
  • //http://www.arcanapercipio.com/lessons/codage_binaire_des_nombres/codage_binaire_des_nombres.html
  • //http://membres.lycos.fr/electroti/reels.htm
  • //http://fr.wikibooks.org/wiki/Architecture_des_ordinateurs/Repr%C3%A9sentation_des_donn%C3%A9es
  • //http://hal.inria.fr/docs/00/07/14/77/PDF/RR-5105.pdf
  • /* 64 bits */
  • // S0eeeeeeeeeeMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  • $sizeOfMantisse = 52 ;
  • $signMask = 1 << 63 ; //
  • $exposantMask = 0x7FF << $sizeOfMantisse ; // eeeeeeeeeee
  • $mantisseMask = 0xFFFFFFFFFFFFF ; // MM...MM
  • $biais = 1023 ;
  • /* 32 bits
  • $sizeOfMantisse = 23 ;
  • $signMask = 1 << 31 ; // 0x80000000
  • $exposantMask = 0xFF << $sizeOfMantisse ; // eeeeeeeeee 0x7F800000
  • $mantisseMask = 0x7FFFFF ; // MM...MM
  • $biais = 127 ;
  • */
  • /* 80 bits
  • $sizeOfMantisse = 64 ;
  • $signMask = 1 << 79 ;
  • $exposantMask = 0x7FFF<< $sizeOfMantisse ; // ee...ee
  • $mantisseMask = 0xFFFFFFFFFFFFFFFF ; // MM...MM
  • $biais = 16383 ;
  • */
  • $sign = (($val & $signMask) != 0) ;
  • $exposant = ($val & $exposantMask) >> $sizeOfMantisse ;
  • $tmpMantisse = $val & $mantisseMask ;
  • $div = 1 ;
  • $mantisse = 1 ;
  • for($i = 0; $i < $sizeOfMantisse; $i++)
  • {
  • $bit = ($tmpMantisse >> ($sizeOfMantisse - $i)) & 1 ;
  • $mantisse += ((1 / $div) * $bit) ;
  • $div = $div * 2 ;
  • }
  • if (($exposant == 0) && ($mantisse == 0))
  • {
  • $number = 0.0 / 1.0 ;
  • }
  • else
  • {
  • $number = pow(2, $exposant - $biais) * $mantisse ;
  • if ($sign)
  • {
  • $number = -1 * $number ;
  • }
  • }
  • return $number ;
  • }
  • /* Read Int16 */
  • private function readInt16()
  • {
  • $int16 = @fread($this->fileId, 2) ;
  • $this->currentPosFile += 2 ;
  • return ~((ord($int16[0]) << 8 | ord($int16[1])) - 1) & 0xFFFF ;
  • }
  • /* Read string (uint16) */
  • private function readString()
  • {
  • $length = $this->readUInt16() ;
  • $string = "" ;
  • for($i = 0; ($i < $length) && ($this->eof() == false); $i++)
  • {
  • $string .= $this->readByte() ;
  • }
  • return $string ;
  • }
  • /* Read long string (uint32) */
  • private function readLongString()
  • {
  • $length = $this->readUint32() ;
  • $string = "" ;
  • for($i = 0; ($i < $length) && ($this->eof() == false); $i++)
  • {
  • $string .= $this->readByte() ;
  • }
  • return $string ;
  • }
  • /* Return true if end-of-file */
  • private function eof()
  • {
  • return ($this->currentPosFile >= $this->fileSize) || feof($this->fileId) ;
  • }
  • /* Read tag */
  • private function readTags()
  • {
  • $currentTagNumber = 0 ;
  • $previousSize = 0 ;
  • $readAudio = false ;
  • $readVideo = false ;
  • while ($this->eof() == false)
  • {
  • $currentTagNumber++ ;
  • $previousTagSize = $this->readUInt32() ;
  • $typeOfTag = $this->readUInt8() ;
  • $length = $this->readUInt24() ;
  • $timeStamp = $this->readUInt32() ;
  • $streamID = $this->readUInt24() ;
  • if ($length > 0)
  • {
  • if ($streamID != 0)
  • {
  • $this->warning(sprintf("Tag n°%d have stream id not null (%X)", $currentTagNumber, $streamID)) ;
  • }
  • if ($previousTagSize != $previousSize)
  • {
  • $this->warning(sprintf("Tag n°%d not correct previous tag size (size of previous tag = %d / previous tag size = %d)", $currentTagNumber, $previousSize, $previousTagSize)) ;
  • }
  • $previousSize = $length ;
  • if ($this->debug)
  • {
  • $this->debug("Tag n°" . $currentTagNumber) ;
  • $this->debug(" previous tage size : " . $previousTagSize) ;
  • $message = " Type : " ;
  • if ($typeOfTag == $this->FLV_TAG_TYPE_META)
  • {
  • $message .= "meta data" ;
  • }
  • else if ($typeOfTag == $this->FLV_TAG_TYPE_AUDIO)
  • {
  • $message .= "audio" ;
  • }
  • else if ($typeOfTag == $this->FLV_TAG_TYPE_VIDEO)
  • {
  • $message .= "video" ;
  • }
  • else
  • {
  • $message .= "unknow (" . $typeOfTag . ")" ;
  • }
  • $this->debug($message) ;
  • $this->debug(" Length : " . $length) ;
  • $this->debug(" End : " . ($length + $this->currentPosFile)) ;
  • $this->debug(" Time stamp : " . $timeStamp) ;
  • $this->debug(" Stream id : " . $streamID) ;
  • }
  • if (($typeOfTag == $this->FLV_TAG_TYPE_META) && ($length > 18))
  • {
  • if ($currentTagNumber == 1)
  • {
  • $this->readMetaBody($length + $this->currentPosFile) ;
  • }
  • else
  • {
  • $this->skipData($length) ;
  • }
  • }
  • else if ($typeOfTag == $this->FLV_TAG_TYPE_AUDIO)
  • {
  • $readAudio = true ;
  • $this->skipData($length) ;
  • }
  • else if ($typeOfTag == $this->FLV_TAG_TYPE_VIDEO)
  • {
  • $readVideo = true ;
  • $this->skipData($length) ;
  • }
  • else
  • {
  • $this->skipData($length) ;
  • }
  • if ($this->stopAtFirstVideoAudio == true)
  • {
  • if (($readAudio == true) and ($readVideo == true))
  • {
  • break ;
  • }
  • }
  • }
  • }
  • }
  • /* Skip data */
  • private function skipData($size)
  • {
  • $this->currentPosFile += $size ;
  • fseek($this->fileId, $size, SEEK_CUR) ;
  • }
  • /* Skip data */
  • private function backData($size)
  • {
  • $this->currentPosFile -= $size ;
  • fseek($this->fileId, $this->currentPosFile, SEEK_SET) ;
  • }
  • /* Add warning mesage */
  • private function warning($message)
  • {
  • $this->warningMessage[] = $message ;
  • }
  • /* Add error mesage */
  • private function error($message)
  • {
  • $this->errorMessage[] = $message ;
  • }
  • /* Add debug mesage */
  • private function debug($message)
  • {
  • $this->debugMessage[] = $message ;
  • }
  • /* Read meta data */
  • private function readMetaBody($endOfTag)
  • {
  • $type = $this->readUInt8() ;
  • if ($type == $this->AMF_DATA_TYPE_STRING)
  • {
  • $length = 0 ;
  • $string = "" ;
  • $string = $this->readString() ;
  • if ($string == "onMetaData")
  • {
  • $this->onMetaData = $this->parseData($endOfTag) ;
  • }
  • else
  • {
  • $this->error("Meta data have not 'onMetaData' string.") ;
  • }
  • }
  • else
  • {
  • $this->error("Not valid meta data.") ;
  • }
  • }
  • /* Parse data */
  • private function parseData($endOfTag) //amf_parse_object
  • {
  • $type = $this->readUInt8() ;
  • switch($type)
  • {
  • case $this->AMF_DATA_TYPE_NUMBER :
  • $val["type"] = $this->AMF_DATA_TYPE_NUMBER ;
  • $val["value"] = $this->readDouble() ;
  • break ;
  • case $this->AMF_DATA_TYPE_BOOLEAN :
  • $val["type"] = $this->AMF_DATA_TYPE_BOOLEAN ;
  • $val["value"] = (ord($this->readByte()) == 0 ? false : true) ;
  • break ;
  • case $this->AMF_DATA_TYPE_STRING :
  • $val["type"] = $this->AMF_DATA_TYPE_STRING ;
  • $val["value"] = $this->readString() ;
  • break ;
  • case $this->AMF_DATA_TYPE_OBJECT :
  • $key = $this->readString() ;
  • $val["type"] = $this->AMF_DATA_TYPE_OBJECT ;
  • $val["name"] = $key ;
  • $val["value"] = $this->parseData($endOfTag) ;
  • break ;
  • case $this->AMF_DATA_TYPE_MOVIE :
  • // not supported
  • $val["type"] = $this->AMF_DATA_TYPE_MOVIE ;
  • break ;
  • case $this->AMF_DATA_TYPE_NULL :
  • $val["type"] = $this->AMF_DATA_TYPE_NULL ;
  • break ;
  • case $this->AMF_DATA_TYPE_UNDEFINED :
  • $val["type"] = $this->AMF_DATA_TYPE_UNDEFINED ;
  • break ;
  • case $this->AMF_DATA_TYPE_UNSUPPORTED :
  • $val["type"] = $this->AMF_DATA_TYPE_UNSUPPORTED ;
  • break ;
  • case $this->AMF_DATA_TYPE_REFERENCE :
  • // not supported
  • $val["type"] = $this->AMF_DATA_TYPE_REFERENCE ;
  • break ;
  • case $this->AMF_DATA_TYPE_ECMA_ARRAY :
  • /* In documentation : "Approximate number of fields of ECMA array.", approximate ??? */
  • $length = $this->readUInt32() ;
  • $val["type"] = $this->AMF_DATA_TYPE_ECMA_ARRAY ;
  • for($i = 0; ($i < $length) && ($this->eof() == false); $i++)
  • {
  • $key = $this->readString() ;
  • $object = $this->parseData($endOfTag) ;
  • $val["value"][] = array($key => $object) ;
  • }
  • /* If Type = 8 (ECMA array type), the ECMAArrayLength provides a hint to the software about
  • how many items might be in the array. The array continues until
  • SCRIPTDATAVARIABLEEND (UI24 Always 9) appears. */
  • $end = $this->readUInt24() ;
  • while (($end != 9) && ($this->eof() == false) && ($endOfTag > $this->currentPosFile))
  • {
  • // Back in file cause is not end SCRIPTDATAVARIABLEEND
  • $this->backData(3) ;
  • $key = $this->readString() ;
  • $object = $this->parseData($endOfTag) ;
  • $val["value"][] = array($key => $object) ;
  • $end = $this->readUInt24() ;
  • }
  • /* if is end of tag who have stop loop, back to UInt24 who have read.
  • I don't know why, some time we don't have SCRIPTDATAVARIABLEEND */
  • if ($endOfTag < $this->currentPosFile)
  • {
  • $this->backData(3) ;
  • }
  • break ;
  • case $this->AMF_DATA_TYPE_STRICT_ARRAY :
  • // If Type = 10 (strict array type), the array begins with a UI32 type and contains that exact
  • // number of items. The array does not terminate with a SCRIPTDATAVARIABLEEND tag.
  • $arrayLength = $this->readUInt32() ;
  • $val["type"] = $this->AMF_DATA_TYPE_STRICT_ARRAY ;
  • for($i = 0; ($i < $arrayLength) && ($this->eof() == false); $i++)
  • {
  • $object = $this->parseData($endOfTag) ;
  • $val["value"][] = $object ;
  • if ($object["type"] == $this->AMF_DATA_TYPE_UNKNOW)
  • {
  • break ;
  • }
  • }
  • break ;
  • case $this->AMF_DATA_TYPE_DATE :
  • $val["type"] = $this->AMF_DATA_TYPE_DATE ;
  • $val["value"] = $this->readDouble() ;
  • $val["UTC_offset"] = $this->readInt16() ;
  • break ;
  • case $this->AMF_DATA_TYPE_LONG_STRING :
  • $val["type"] = $this->AMF_DATA_TYPE_LONG_STRING ;
  • $val["value"] = $this->readLongString() ;
  • break ;
  • case $this->AMF_DATA_TYPE_RECORD_SET :
  • // not supported
  • $val["type"] = $this->AMF_DATA_TYPE_RECORD_SET ;
  • break ;
  • case $this->AMF_DATA_TYPE_XML :
  • // not supported
  • $val["type"] = $this->AMF_DATA_TYPE_XML ;
  • break ;
  • case $this->AMF_DATA_TYPED_OBJECT :
  • // not supported
  • $val["type"] = $this->AMF_DATA_TYPED_OBJECT ;
  • break ;
  • case $this->AMF_DATA_TYPE_AMF3DATA :
  • // not supported
  • $val["type"] = $this->AMF_DATA_TYPED_OBJECT ;
  • break ;
  • default:
  • $val["type"] = $this->AMF_DATA_TYPE_UNKNOW ;
  • $val["code_type"] = $type ;
  • break ;
  • }
  • return $val ;
  • }
  • }
  • ?>
<?php
/*
 * Class to read Flash Video file
 *
 * Write by MARTINEAU Emeric (php4php@free.fr)
 *
 *******************************************************************************
 * USE :
 *   $MyClass = new FLVFile(string $file_to_read, [bool $stop_when_read_one_video_and_one_audio_tag, bool $debug]) ;
 *
 *   $file_to_read : FLV file,
 *   $stop_when_read_one_video_and_one_audio_tag : no read all file, just one audio and video tag,
 *   $debug : if true, $xxx->debugMesage : array with debug message
 *******************************************************************************
 * ATTRIBUTS
 *   - $fileName : file name past in constructor,
 *   - $isFileFound : true if file found,
 *   - $isFLVFile : true if FLV file,
 *   - $isReadable : true if file not read protect,
 *   - $isAudioTag : true if file contain audio stream,
 *   - $isVideoTag : true if file contain video stream,
 *   - $onMetaData : meta data of file, array,
 *                   AMF_DATA_TYPE_NUMBER :
 *                                          "type" = AMF_DATA_TYPE_NUMBER
 *                                          "value" = number
 *                   AMF_DATA_TYPE_BOOLEAN : 
 *                                          "type" = AMF_DATA_TYPE_BOOLEAN
 *                                          "value" = true or false 
 *                   AMF_DATA_TYPE_STRING : 
 *                                          "type" = AMF_DATA_TYPE_STRING
 *                                          "value" = string 
 *                   AMF_DATA_TYPE_OBJECT : 
 *                                          "type" = AMF_DATA_TYPE_OBJECT
 *                                          "value" = array of data (not tested) 
 *                   AMF_DATA_TYPE_MOVIE : 
 *                                          "type" = AMF_DATA_TYPE_MOVIE
 *                                          not implemented
 *                   AMF_DATA_TYPE_NULL : 
 *                                          "type" = AMF_DATA_TYPE_NULL
 *                   AMF_DATA_TYPE_UNDEFINED : 
 *                                          "type" = AMF_DATA_TYPE_UNDEFINED
 *                   AMF_DATA_TYPE_UNSUPPORTED : 
 *                                          "type" = AMF_DATA_TYPE_UNSUPPORTED
 *                   AMF_DATA_TYPE_REFERENCE : 
 *                                          "type" = AMF_DATA_TYPE_REFERENCE
 *                                          not implemented
 *                   AMF_DATA_TYPE_ECMA_ARRAY : 
 *                                          "type" = AMF_DATA_TYPE_ECMA_ARRAY
 *                                          "value" = array(
 *                                                           key_name => value
 *                                                         )
 *                   AMF_DATA_TYPE_STRICT_ARRAY : 
 *                                          "type" = AMF_DATA_TYPE_STRICT_ARRAY
 *                                          "value" = array(value1, value2) 
 *                   AMF_DATA_TYPE_DATE : 
 *                                          "type" = AMF_DATA_TYPE_DATE
 *                                          "value" = date
 *                                          "UTC_offset" = utc offset
 *                   AMF_DATA_TYPE_LONG_STRING : 
 *                                          "type" = AMF_DATA_TYPE_LONG_STRING
 *                                          "value" = string
 *                   AMF_DATA_TYPE_RECORD_SET : 
 *                                          "type" = AMF_DATA_TYPE_RECORD_SET
 *                                          not implemented
 *                   AMF_DATA_TYPE_XML : 
 *                                          "type" = AMF_DATA_TYPE_XML
 *                                          not implemented
 *                   AMF_DATA_TYPE_OBJECT : 
 *                                          "type" = AMF_DATA_TYPE_XML
 *                                          not implemented
 *                   AMF_DATA_TYPE_AMF3DATA : 
 *                                          "type" = AMF_DATA_TYPE_AMF3DATA
 *                                          not implemented 
 *                   AMF_DATA_TYPE_UNKNOW : 
 *                                          "type" = AMF_DATA_TYPE_UNKNOW
 *                                          if appear, problem in file  
 *                   
 *        
 *   - $warningMessage : array, warning message,
 *   - $errorMessage : array, error message
 *   - $debugMessage : array, debug message,
 *
 * METHODS
 *   no method
 *******************************************************************************
 * DOCUMENTATION
 *   http://osflash.org/flv
 *   http://code.activestate.com/recipes/457406/
 *   http://inlet-media.de/flvtool2
 *   http://www.adobe.com/devnet/flv/pdf/video_file_format_spec_v9.pdf
 *******************************************************************************
 * TODO :
 *   - in parseData() : 
 *                     - test AMF_DATA_TYPE_OBJECT,
 *                     - implement AMF_DATA_TYPE_REFERENCE,
 *                     - implement AMF_DATA_TYPE_RECORD_SET,
 *                     - implement AMF_DATA_TYPE_XML,
 *                     - implement AMF_DATA_TYPED_OBJECT,
 *                     - implement AMF_DATA_TYPE_AMF3DATA,
 */
class FLVFile
{
    /**************/
    /* PUBLIC VAR */
    /**************/

    // Name of file
    public $fileName ;

    // True if file found
    public $isFileFound = false ;

    // True id file is FLV
    public $isFLVFile = false ;

    // True if file can be read
    public $isReadable = false ;

    // True if audio tag is present
    public $isAudioTag = false ;

    // True if video tag is present
    public $isVideoTag = false ;
    
    // onMetaData value
    public $onMetaData = null ;
    
    // Warning string
    public $warningMessage = array() ;

    // Error string
    public $errorMessage = array() ;
    
    // Debug message
    public $debugMessage = array() ;
    
    /********************/
    /* PUBLIC CONSTANCE */
    /********************/
    public static $AMF_DATA_TYPE_NUMBER = 0 ;
    public static $AMF_DATA_TYPE_BOOLEAN = 1 ;
    public static $AMF_DATA_TYPE_STRING = 2 ;
    public static $AMF_DATA_TYPE_OBJECT = 3 ;
    public static $AMF_DATA_TYPE_MOVIE = 4 ;
    public static $AMF_DATA_TYPE_NULL = 5 ;
    public static $AMF_DATA_TYPE_UNDEFINED = 6 ;
    public static $AMF_DATA_TYPE_REFERENCE = 7 ;
    public static $AMF_DATA_TYPE_ECMA_ARRAY = 8 ;
    public static $AMF_DATA_TYPE_STRICT_ARRAY = 10 ;
    public static $AMF_DATA_TYPE_DATE = 11 ;
    public static $AMF_DATA_TYPE_LONG_STRING = 12 ;
    public static $AMF_DATA_TYPE_UNSUPPORTED = 13 ;
    public static $AMF_DATA_TYPE_RECORD_SET = 14 ;
    public static $AMF_DATA_TYPE_XML = 15 ;
    public static $AMF_DATA_TYPE_TYPED_OBJECT = 16 ;
    public static $AMF_DATA_TYPE_AMF3DATA = 17 ;
    public static $AMF_DATA_TYPE_UNKNOW = -1 ;

    /***************/
    /* PRIVATE VAR */
    /***************/
    // Ressource file id
    private $fileId ;
    
    // current position in file. Use sur skipData()
    private $currentPosFile = 0 ;
    
    // file size
    private $fileSize = 0 ;
    
    // Stop when found first video stream
    private $stopAtFirstVideoAudio = true ;
    
    // Make debug string
    private $debug = false ;
    
    /**********************/
    /* PRIVATE CONSTANTES */
    /**********************/
    private static $FLV_TAG_TYPE_AUDIO = 8 ;
    private static $FLV_TAG_TYPE_VIDEO = 9 ;
    private static $FLV_TAG_TYPE_META = 18 ; /* 0x12 */
    private static $AMF_END_OF_OBJECT = 9 ;

    /*
     * Constructor
     */
    public function __construct($file_name, $stop_at_first__video_and_audio = true, $debug = false)
    {
    	  $this->stopAtFirstVideoAudio = $stop_at_first__video_and_audio ;
    	  
    	  $this->debug = $debug ;

        if (file_exists($file_name))
        {
            $this->filename = $file_name ;
            
            $this->fileSize = filesize($file_name) ;

            $this->filefound = true ;

            $this->fileId  = @fopen($file_name, "rb");

            if ($this->fileId)
            {
                $this->isReadable = true ;

                $this->isFLVFile = $this->checkHeader() ;

                if ($this->isFLVFile) 
                {
                    /* Lecture des tags */
                    $this->readTags() ;
                }

                fclose($this->fileId) ;
            }
            else
            {
            	  $this->error("Can't open file to read.") ;
            }
        } 
        else
        {
        	  $this->error("File doesn't exist.") ;
        }
    }
    
    /*
     * Check if file is valid FLV file
     */
    private function checkHeader()
    {
        $returnValue = false ;

        /* Id */
        $id = @fread($this->fileId, 3) ;

        if ($id == 'FLV')
        {
            $this->currentPosFile += 3 ;
            
            $fileVersion = $this->readUInt8() ;

            /* file version */
            if ($fileVersion == 1)
            {
                /* audio/video are present */
                $tag = $this->readUInt8() ;
                
                if (($tag & 1) != 0)
                {
                    $this->isVideoTag = true ;
                }

                if (($tag & 4) != 0)
                {
                    $this->isAudioTag = true ;
                }
               
                /* header size (always 9) */
                if ($this->readUInt32() == 9)
                {               	  
                    $returnValue = true ;
                }
                else
                {
                	  $this->error("Header size must be 9.") ;
                }
            }
            else
            {
            	  $this->error("Only version 1 of file is supported.") ;
            }
        }
        else
        {
        	  $this->error("Not valid file (no found FLV tag).") ;
        }

        return $returnValue ;
    }

    /*  Read Uin64 */
    private function readUInt64()
    {
        $uInt64 = @fread($this->fileId, 8) ;

        $this->currentPosFile += 8 ;

        return (ord($uInt64[0]) << 56) | (ord($uInt64[1]) << 48) | (ord($uInt32[2]) << 40) | (ord($uInt64[3]) << 32) | (ord($uInt64[4]) << 24) | (ord($uInt64[5]) << 16) | (ord($uInt64[6]) << 8) | ord($uInt64[7]) ;
    }
    
    /*  Read Uin32 */
    private function readUInt32()
    {
        $uInt32 = @fread($this->fileId, 4) ;

        $this->currentPosFile += 4 ;

        return (ord($uInt32[0]) << 24) | (ord($uInt32[1]) << 16) | (ord($uInt32[2]) << 8) | ord($uInt32[3]) ;
    }
    
    /*  Read Uin24 */
    private function readUInt24()
    {
        $uInt24 = @fread($this->fileId, 3) ;
        
        $this->currentPosFile += 3 ;
        
        return (ord($uInt24[0]) << 16) | (ord($uInt24[1]) << 8) | ord($uInt24[2]) ;
    }
    
    /*  Read Uin16 */
    private function readUInt16()
    {
        $uInt16 = @fread($this->fileId, 2) ;
        
        $this->currentPosFile += 2 ;
        
        return (ord($uInt16[0]) << 8) | ord($uInt16[1]) ;
    }  

    /*  Read Uin8 */
    private function readUInt8()
    {
        $uInt8 = @fread($this->fileId, 1) ;
        
        $this->currentPosFile++ ;
        
        return ord($uInt8) ;
    } 

    /*  Read byte */
    private function readByte()
    {       
        $this->currentPosFile++ ;
        
        return @fread($this->fileId, 1) ;
    }    
   
    /* Read double */
    private function readDouble()
    {
        // 32 bits
        //$val = 0xBF866666  ; // -1.05
        //$val = 0xC1199999 ; //-9.6
        //$val = 0x3EC00000 ; // 0.375
        
        $val = $this->readUInt64() ;
        
        //http://www.commentcamarche.net/base/representation.php3
        //http://www.arcanapercipio.com/lessons/codage_binaire_des_nombres/codage_binaire_des_nombres.html
        //http://membres.lycos.fr/electroti/reels.htm
        //http://fr.wikibooks.org/wiki/Architecture_des_ordinateurs/Repr%C3%A9sentation_des_donn%C3%A9es
        //http://hal.inria.fr/docs/00/07/14/77/PDF/RR-5105.pdf
        /* 64 bits */
        // S0eeeeeeeeeeMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
        $sizeOfMantisse = 52 ;
        $signMask = 1 << 63 ; //
        $exposantMask = 0x7FF << $sizeOfMantisse ; // eeeeeeeeeee
        $mantisseMask = 0xFFFFFFFFFFFFF ; // MM...MM
        $biais = 1023 ;
                
        /* 32 bits
        $sizeOfMantisse = 23 ;
        $signMask = 1 << 31 ; // 0x80000000
        $exposantMask = 0xFF << $sizeOfMantisse ; // eeeeeeeeee 0x7F800000
        $mantisseMask = 0x7FFFFF ; // MM...MM
        $biais = 127 ;        
        */
        
        /* 80 bits
        $sizeOfMantisse = 64 ;
        $signMask = 1 << 79 ; 
        $exposantMask = 0x7FFF<< $sizeOfMantisse ; // ee...ee
        $mantisseMask = 0xFFFFFFFFFFFFFFFF ; // MM...MM
        $biais = 16383 ;          
        */
        
        $sign = (($val & $signMask) != 0) ;
        
        $exposant = ($val & $exposantMask) >> $sizeOfMantisse ;

        $tmpMantisse = $val & $mantisseMask ;

        $div = 1 ;
        
        $mantisse = 1 ;
        
        for($i = 0; $i < $sizeOfMantisse; $i++)
        {                      
            $bit = ($tmpMantisse >> ($sizeOfMantisse - $i)) & 1 ;

            $mantisse += ((1 / $div) * $bit) ;
            
            $div = $div * 2 ;            
        }       
        
        if (($exposant == 0) && ($mantisse == 0))
        {
            $number = 0.0 / 1.0 ;
        }
        else
        {             
            $number = pow(2, $exposant - $biais) * $mantisse ;

            if ($sign)
            {
                $number = -1 * $number ;
            }
        }
        
        return $number ;
    }
    
    /* Read Int16 */
    private function readInt16()
    {
        $int16 = @fread($this->fileId, 2) ;
    
        $this->currentPosFile += 2 ;
    
        return ~((ord($int16[0]) << 8 | ord($int16[1])) - 1) & 0xFFFF ;
    }
    
    /* Read string (uint16) */
    private function readString()
    {
        $length = $this->readUInt16() ;
                
        $string = "" ;

        for($i = 0; ($i < $length) && ($this->eof() == false); $i++)
        {
            $string .= $this->readByte() ;
        }

        return $string ;
    }    
    
    /* Read long string (uint32) */
    private function readLongString()
    {
        $length = $this->readUint32() ;
         
        $string = "" ;

        for($i = 0; ($i < $length) && ($this->eof() == false); $i++)
        {
            $string .= $this->readByte() ;
        }
        
        return $string ;
    }
    
    /* Return true if end-of-file */
    private function eof()
    {
        return ($this->currentPosFile >= $this->fileSize) || feof($this->fileId) ;
    }
    
    /* Read tag */    
    private function readTags()
    {
    	$currentTagNumber = 0 ;
    	$previousSize = 0 ;
        $readAudio = false ;
        $readVideo = false ;
    	  
        while ($this->eof() == false)
        {
        	$currentTagNumber++ ;
        	  
            $previousTagSize = $this->readUInt32() ;
            $typeOfTag = $this->readUInt8() ;
            $length = $this->readUInt24() ;
            $timeStamp = $this->readUInt32() ;
            $streamID = $this->readUInt24() ;
            
            if ($length > 0)
            {
                if ($streamID != 0)
                {
            	    $this->warning(sprintf("Tag n°%d have stream id not null (%X)", $currentTagNumber, $streamID)) ;
                }
            
                if ($previousTagSize != $previousSize)
                {
            	    $this->warning(sprintf("Tag n°%d not correct previous tag size (size of previous tag = %d / previous tag size = %d)", $currentTagNumber, $previousSize, $previousTagSize)) ;
                }
            
                $previousSize = $length ;
                 
                if ($this->debug)
                {
                	$this->debug("Tag n°" . $currentTagNumber) ;
                	$this->debug("  previous tage size : " . $previousTagSize) ;
                	  
                	$message = "  Type : " ;
                	  
                    if ($typeOfTag == $this->FLV_TAG_TYPE_META)
                    {
                    	$message .= "meta data" ;
                    }
                    else if ($typeOfTag == $this->FLV_TAG_TYPE_AUDIO)
                    {
                    	$message .= "audio" ;
                    }
                    else if ($typeOfTag == $this->FLV_TAG_TYPE_VIDEO)
                    {
                    	$message .= "video" ;
                    }
                    else
                    {
                    	$message .= "unknow (" . $typeOfTag . ")" ;
                    }
                    
                    $this->debug($message) ;
                                        
                    $this->debug("  Length : " . $length) ;
                    $this->debug("  End : " . ($length + $this->currentPosFile)) ;
                    $this->debug("  Time stamp : " . $timeStamp) ;
                    $this->debug("  Stream id : " . $streamID) ;
                }  
                
                if (($typeOfTag == $this->FLV_TAG_TYPE_META) && ($length > 18)) 
                {
                    if ($currentTagNumber == 1)
                    {
                        $this->readMetaBody($length + $this->currentPosFile) ;
                    }
                    else
                    {
                        $this->skipData($length) ;
                    }
                }
                else if ($typeOfTag == $this->FLV_TAG_TYPE_AUDIO)
                {
                    $readAudio = true ;
            	    $this->skipData($length) ;
                }
                else if ($typeOfTag == $this->FLV_TAG_TYPE_VIDEO)
                {  	
                    $readVideo = true ;
                	$this->skipData($length) ;
                }
                else
                {
            	      $this->skipData($length) ;
                }
                
                if ($this->stopAtFirstVideoAudio == true)
                {
                    if (($readAudio == true) and ($readVideo == true))
                    {
                        break ;
                    }
                }                  
            }
        }
    }
    
    /* Skip data */
    private function skipData($size)
    {
    	$this->currentPosFile += $size ;
    	
    	fseek($this->fileId, $size, SEEK_CUR) ;
    }
    
    /* Skip data */
    private function backData($size)
    {
    	$this->currentPosFile -= $size ;
    	
    	fseek($this->fileId, $this->currentPosFile, SEEK_SET) ;
    }    
    
    /* Add warning mesage */
    private function warning($message)
    {
    	  $this->warningMessage[] = $message ;
    }
    
    /* Add error mesage */
    private function error($message)
    {
    	  $this->errorMessage[] = $message ;
    }
    
    /* Add debug mesage */
    private function debug($message)
    {
    	  $this->debugMessage[] = $message ;
    }  

    /* Read meta data */
    private function readMetaBody($endOfTag)
    {
        $type = $this->readUInt8() ;
        
        if ($type == $this->AMF_DATA_TYPE_STRING)
        {
            $length = 0 ;
            $string = "" ;
            
            $string = $this->readString() ;

            if ($string == "onMetaData")
            {
                $this->onMetaData = $this->parseData($endOfTag) ;
            }
            else
            {
                $this->error("Meta data have not 'onMetaData' string.") ;
            }
        }
        else
        {
            $this->error("Not valid meta data.") ;
        }
    }
        
    /* Parse data */
    private function parseData($endOfTag) //amf_parse_object
    {
        $type = $this->readUInt8() ;

        switch($type)
        {
            case $this->AMF_DATA_TYPE_NUMBER :            
                $val["type"] = $this->AMF_DATA_TYPE_NUMBER ;
                $val["value"] = $this->readDouble() ;
                break ;
            case $this->AMF_DATA_TYPE_BOOLEAN :
                $val["type"] = $this->AMF_DATA_TYPE_BOOLEAN ;
                $val["value"] = (ord($this->readByte()) == 0 ? false : true) ;
                break ;
            case $this->AMF_DATA_TYPE_STRING :
                $val["type"] = $this->AMF_DATA_TYPE_STRING ;
                $val["value"] = $this->readString() ;
                break ;
            case $this->AMF_DATA_TYPE_OBJECT :
                $key = $this->readString() ;

                $val["type"] = $this->AMF_DATA_TYPE_OBJECT ;
                $val["name"] = $key ;
                $val["value"] = $this->parseData($endOfTag) ;

                break ;
            case $this->AMF_DATA_TYPE_MOVIE :
                // not supported
                $val["type"] = $this->AMF_DATA_TYPE_MOVIE ;
                break ;
            case $this->AMF_DATA_TYPE_NULL :
                $val["type"] = $this->AMF_DATA_TYPE_NULL ;
                break ;            
            case $this->AMF_DATA_TYPE_UNDEFINED :
                $val["type"] = $this->AMF_DATA_TYPE_UNDEFINED ;
                break ;            
            case $this->AMF_DATA_TYPE_UNSUPPORTED :
                $val["type"] = $this->AMF_DATA_TYPE_UNSUPPORTED ;
                break ;
            case $this->AMF_DATA_TYPE_REFERENCE :
                // not supported
                $val["type"] = $this->AMF_DATA_TYPE_REFERENCE ;
                break ;
            case $this->AMF_DATA_TYPE_ECMA_ARRAY :
                /* In documentation : "Approximate number of fields of ECMA array.", approximate ???  */
                $length = $this->readUInt32() ;

                $val["type"] = $this->AMF_DATA_TYPE_ECMA_ARRAY ;
                
                for($i = 0; ($i < $length) && ($this->eof() == false); $i++)
                {
                    $key = $this->readString() ;
                    $object = $this->parseData($endOfTag) ;
                    $val["value"][] = array($key => $object) ;
                }

                /* If Type = 8 (ECMA array type), the ECMAArrayLength provides a hint to the software about
                   how many items might be in the array. The array continues until
                   SCRIPTDATAVARIABLEEND (UI24 Always 9) appears. */
                $end = $this->readUInt24() ;

                while (($end != 9) && ($this->eof() == false) && ($endOfTag > $this->currentPosFile))
                {
                    // Back in file cause is not end SCRIPTDATAVARIABLEEND
                    $this->backData(3) ;

                    $key = $this->readString() ;
                    $object = $this->parseData($endOfTag) ;
                    $val["value"][] = array($key => $object) ;                
                    
                    $end = $this->readUInt24() ;
                }
                
                /* if is end of tag who have stop loop, back to UInt24 who have read.
                   I don't know why, some time we don't have SCRIPTDATAVARIABLEEND */
                if ($endOfTag < $this->currentPosFile)
                {
                    $this->backData(3) ;
                }
                
                break ;
            case $this->AMF_DATA_TYPE_STRICT_ARRAY :
                // If Type = 10 (strict array type), the array begins with a UI32 type and contains that exact
                // number of items. The array does not terminate with a SCRIPTDATAVARIABLEEND tag.
                $arrayLength = $this->readUInt32() ;

                $val["type"] = $this->AMF_DATA_TYPE_STRICT_ARRAY ;
                
                for($i = 0; ($i < $arrayLength) && ($this->eof() == false); $i++)
                {
                    $object = $this->parseData($endOfTag) ;
 
                    $val["value"][] = $object ;
                    
                    if ($object["type"] == $this->AMF_DATA_TYPE_UNKNOW)
                    {
                        break ;
                    }                
                }

                break ;
            case $this->AMF_DATA_TYPE_DATE :
                $val["type"] = $this->AMF_DATA_TYPE_DATE ;
                $val["value"] = $this->readDouble() ;
                $val["UTC_offset"] = $this->readInt16() ;
                break ;
            case $this->AMF_DATA_TYPE_LONG_STRING :
                $val["type"] = $this->AMF_DATA_TYPE_LONG_STRING ;
                $val["value"] = $this->readLongString() ;
                break ;
            case $this->AMF_DATA_TYPE_RECORD_SET :
                // not supported
                $val["type"] = $this->AMF_DATA_TYPE_RECORD_SET ;
                break ;            
            case $this->AMF_DATA_TYPE_XML :
                // not supported
                $val["type"] = $this->AMF_DATA_TYPE_XML ;
                break ;            
            case $this->AMF_DATA_TYPED_OBJECT :
                // not supported
                $val["type"] = $this->AMF_DATA_TYPED_OBJECT ;
                break ;            
            case $this->AMF_DATA_TYPE_AMF3DATA :
                // not supported
                $val["type"] = $this->AMF_DATA_TYPED_OBJECT ;
                break ; 
            default:
                $val["type"] = $this->AMF_DATA_TYPE_UNKNOW ;
                $val["code_type"] = $type ;
                break ;            
        }

        return $val ;
    }
}
?>



 Historique

25 septembre 2008 13:43:45 :
Passage en objet PHP 5

 Sources de la même categorie

Source avec Zip RECHERCHE DE MOTIF DANS UNE IMAGE par ParseError
BARRES PARAMÉTRABLES EN DÉGRADÉ DE COULEUR ET AVEC TEXTE DA... par hornetbzz
Source avec Zip Source avec une capture GALERIE PHOTO SIMPLE À GÉRER par francky6691
Source avec Zip Source avec une capture DIAPORAMA AVEC AJOUT ET REDIMENSIONEMENT DE PHOTOS, CRÉATION... par giloum
Source avec Zip Source avec une capture AFFICHAGE INFOBULLE SUR GRAPHIQUE ARTICHOW par nirronico

 Sources en rapport avec celle ci

Source avec Zip TÉLÉCHARGER LES VIDEOS DE YOUTUBE AVEC PERMALIEN DEPUIS YOUT... par amteur

Commentaires et avis

Commentaire de webdeb le 24/09/2008 01:49:17

Pourquoi une classe écrite en PHP 4 et pas en PHP 5 ?
AMF-PHP ne supporte pas PHP 5 ?
Pourquoi nommes tes constantes avec le préfixe AMF ? Tu n'utilises pourtant pas de services AMF-PHP à ce que je vois.

Commentaire de depression le 24/09/2008 10:11:24

AMF PHP n'a pas de problème avec PHP5. De toutes façon, ce n'est pas lui qui interprète le PHP.

On pourrait vulgairement comparer cela à AJAX, où le javascript appelle un PHP et interprète le retour, mais en aucun cas exécute le code PHP.

Commentaire de bubulemaster le 24/09/2008 11:34:26

En fait, j'ai appelé ça AMF pour Adobe Meta Format, je ne connais pas le AMF dont vous parlez.

Commentaire de Palleas_44 le 24/09/2008 19:21:09

C'est dommage de voir une classe PHP 5 aujourd'hui, ça me donne envie de pleurer :(

Plus serieusement, tu y gagnerais à utiliser PHP 5 :)

Commentaire de nicomilville le 24/09/2008 19:23:16

Non, c'est dommage de voir une classe PHP 4 lol !

a++

PS : je pense que c'est une erreur de frappe !

Commentaire de Palleas_44 le 24/09/2008 19:25:23

En effet, pardon !

Commentaire de nicomilville le 24/09/2008 19:27:25

Pas grave, ça arrive a tout le monde et très souvent !

a++

Commentaire de bubulemaster le 25/09/2008 13:34:10

C'est vrai j'aurais du faire un classe PHP 5 surtout que je développe sur cette plateforme.
Je l'ai pas fait par fainéantise. Allez, je vais la mettre à jour.

Commentaire de webdeb le 25/09/2008 14:35:24

Si tu pouvais remplacer les attributs publics par des attributs privés, et ajouter des méthodes getter() et setter() pour chacun, ce serait encore plus sympas. Les attributs publics ne sont pas très conseillés car ils ne permettent pas de faire du contrôle d'accès sur les valeurs à atteindre.

++

Hugo.

Commentaire de fgth99 le 16/06/2009 17:52:17

Bonjour,
Bonne idée que cette classe, quelqu'un l'utilise? Je crois qu'un petit bug s'est glissé ligne 291 : $uInt32[2] au lieu de $uInt64[2] non? Même avec cette correction j'obtiens parfois des valeurs incohérentes sur certains flv en essayant d'extraire les propriétés (largeur,hauteur,durée), cf code ci-joint ajouté à la classe...
+
m a r c
    private function readMetadata(){
        $id = @fread($this->fileId, 3) ;

        // verifie 3 premiers chars
        if ($id != 'FLV'){ echo "Error 123"; return; }

        // extrait "duration" et "width" et "height" du debut du fichier
        $buffer = fread($this->fileId, 256);
        
        // par defaut 0
        $this->duration = $this->width = $this->height = 0;

        $v = $this->getStr($buffer, "duration");
        if ($v != ""){ $this->duration = $this->doubleToInt($v); }

        $v = $this->getStr($buffer, "width");
        if ($v != ""){ $this->width = floor($this->doubleToInt($v)); }

        $v = $this->getStr($buffer, "height");
        if ($v != ""){ $this->height = floor($this->doubleToInt($v)); }

    }
    private function doubleToInt($buf){
        $val = (ord($buf[0]) << 56) | (ord($buf[1]) << 48) | (ord($buf[2]) << 40) | (ord($buf[3]) << 32) | (ord($buf[4]) << 24) | (ord($buf[5]) << 16) | (ord($buf[6]) << 8) | ord($buf[7]) ;
        $sizeOfMantisse = 52 ;
        $signMask = 1 << 63 ; //
        $exposantMask = 0x7FF << $sizeOfMantisse ; // eeeeeeeeeee
        $mantisseMask = 0xFFFFFFFFFFFFF ; // MM...MM
        $biais = 1023 ;

        $sign = (($val & $signMask) != 0) ;

        $exposant = ($val & $exposantMask) >> $sizeOfMantisse ;

        $tmpMantisse = $val & $mantisseMask ;

        $div = 1 ;

        $mantisse = 1 ;

        for($i = 0; $i < $sizeOfMantisse; $i++)
        {
            $bit = ($tmpMantisse >> ($sizeOfMantisse - $i)) & 1 ;

            $mantisse += ((1 / $div) * $bit) ;

            $div = $div * 2 ;
        }

        if (($exposant == 0) && ($mantisse == 0))
        {
            $number = 0.0 / 1.0 ;
        }
        else
        {
            $number = pow(2, $exposant - $biais) * $mantisse ;

            if ($sign)
            {
                $number = -1 * $number ;
            }
        }

        return $number ;
    }

Commentaire de fgth99 le 17/06/2009 16:52:00

Rebonjour,
Correction du code précédent qui marche maintenant, et permet donc d'extraire les largeur+hauteur+durée d'un flv :)
m a r c
  private function doubleToInt($buf){
    $val = (ord($buf[0]) << 56) | (ord($buf[1]) << 48) | (ord($buf[2]) << 40) | (ord($buf[3]) << 32) | (ord($buf[4]) << 24) | (ord($buf[5]) << 16) | (ord($buf[6]) << 8) | ord($buf[7]) ;
    $sizeOfMantisse = 52 ;
    $signMask = 1 << 63 ;
    $exposantMask = 0x7FF << $sizeOfMantisse ;
    $mantisseMask = 0x000FFFFFFFFFFFFF ;
    $biais = (1<<10) - 1 ;

    $sign = (($val & $signMask) != 0)? -1.0 : 1.0 ;

    $exposant = ($val & $exposantMask) >> $sizeOfMantisse ;

    $tmpMantisse = $val & $mantisseMask ;

    $div = 2 ;
    $mantisse = 1 ;
    for($i = 1; $i <= $sizeOfMantisse; $i++)
    {
      $bit = ($tmpMantisse >> ($sizeOfMantisse - $i)) & 1 ;
      $mantisse += ((1 / $div) * $bit) ;
      $div = $div * 2 ;
    }

    if (($exposant == 0) && ($mantisse == 0)) return 0.0;

    return  $sign * pow(2, $exposant - $biais) * $mantisse ;
  }

Commentaire de SA7BOOCH le 25/11/2009 14:14:36

Bonjour fgth99,

est ce que vous pouvez m'envoyer le code de ta méthode getStr utilisé dans la méthode readMetadata.

Merci

Commentaire de fgth99 le 25/11/2009 14:50:32

Bonjour,
Voici la fonction getStr manquante :
function getStr($buf, $tag){
$p = strpos($buf, $tag);
if ($p !== false){
$deb = $p + strlen($tag)+1;
return substr($buf, $deb);
}
return "";
}

Commentaire de SA7BOOCH le 25/11/2009 15:50:06

merci fgth99,

mais ta méthode readMetadata donne les bonne valeurs de height et width mais pas la durée :(

var_dump($flv->duration);
var_dump($flv->height);
var_dump($flv->width);

donne

float(1.35595221018E+87)
float(736)
float(640)

Commentaire de fgth99 le 25/11/2009 16:11:42

effectivement 1.35595221018E+87 meme en microsecondes ca fait beaucoup...ca doit donc dependre de la plateforme/version php, sur mon linux fc9.x86_64 ca affiche des valeurs correctes.

Commentaire de fgth99 le 25/11/2009 21:49:41

..et ca marche aussi sur win32+PHP 5.2.6 (cli)
d:\dev\php\getflvsize>php -f getflvsize.php video3.flv
video3.flv : 258x193 11.500001911889 sec

Commentaire de SA7BOOCH le 26/11/2009 07:23:20

je m'excuse de vous avoir quité hier sans prévenir,

hier j'était sous Ubuntu 9.0.4 32-bit et ce matin j'utilise fc10.x86_32 et j'ai toujour le même problème :( je coris que je vais installer ffmpeg et utiliser ses commandes pour récupérer automatiquement les durées de mes vidéos

Merci pour votre réactivité

Commentaire de SA7BOOCH le 26/11/2009 11:28:40

c'est bon j'ai trouvé une solution à mon problème sur

http://bytes.com/topic/php/answers/9237-hex-floating-point (2éme commentaire).

mon code ressemble à ça en ce moment

private function read_metadata() {

$meta_tags = array(
'duration',
'width',
'height',
'videodatarate',
'canSeekToEnd',
'videocodecid',
'audiodatarate',
'audiocodecid',
'framerate'
);

$buffer = fread($this->fileId, 256);

foreach($meta_tags as $meta_tag) {
$v = $this->getStr($buffer, $meta_tag);
$this->metadata[$meta_tag] = $this->bin2double($v);
}
}

public function get_metadata() {
return $this->metadata;
}

private function hex2bin($hex) {
$l = strlen($hex);
$bin = '';
for ($i=0;$i<$l;$i=$i+2) {
$bin .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $bin;
}

private function hexreverse($hex) {
$l = strlen($hex);
$bytes = array();
for ($i=0;$i<$l;$i=$i+2) {
$bytes[] = $hex[$i].$hex[$i+1];
}
$reversed_bytes = array_reverse($bytes);
return implode($reversed_bytes);
}

private function bin2double($v) {
$hex = bin2hex($v);
$hex_word = substr($hex, 0, 16);
$reversed_hex = $this->hexreverse($hex_word);
$hex_double = $this->hex2bin($reversed_hex);
$unpacked_double = unpack('d', $hex_double);
return round($unpacked_double[1]);
}

private function getStr($buf, $tag) {
$p = strpos($buf, $tag);
if ($p !== false){
$deb= $p + strlen($tag) + 1;
return substr($buf, $deb);
}
return '';
}

public static function seconds_to_hhmmss($duration) {
return sprintf('%02d:%02d:%02d', floor($duration / 3600), floor(($duration%3600) / 60), $duration % 60);
}

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

ffmpeg + php [ par dboujot ] Bonjour. On utilisant notre ami commun, google, j'ai fait une petite recherche sur la librairie FFMPEG, dans le but de pour créer sur mon site des fic BDD de video FLV [ par slashf ] bonjour a tous :) est ce qu'on peux m'aider ? j'ai envi de créé une base de donnée php pour des video au format FLV, ne me ditent pas d'aller chercher affichage de flv contenu dans un répertoir [ par slashf ] bonjour a tous donc voilà je vous explique mon cas : je veux afficher dans une page un lien clicable des video au format flv que j'ai dans un répertoi projet [ par abdmoneem ] salut je suis un etudiant que je doit faire un projet de serveur de streaming de video d'extension .flv (flash), j'ai trouver des difficultes sur la c modifier un fichier .xml [ par gaillardo ] Bonjour,J'ai un lecteur flv qui va chercher l'url de la video à afficher dans un fichier xml (flv_config.xml) et j'aimerai tous simplement réussir à m Aide tableau + boucle +regex [ par szizman ] Dans un premier temps merci pour les tutos, ils sont vraiment excellent et très utiles. J'ai commencé le code il y a 3 semaines et demi donc je vous d


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,624 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales