Lecture des caractéristiques d'un fichier flv

Soyez le premier à donner votre avis sur cette source.

Snippet vu 7 456 fois - Téléchargée 20 fois

Contenu du snippet

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 / Exemple :


<?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,
* 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 ; } } ?>

A voir également

Ajouter un commentaire

Commentaires

Commenter la réponse de webdeb

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.