From b3e5fb6faa02c327fcf0b76ddfbda7ef87410c16 Mon Sep 17 00:00:00 2001 From: rWatcher Date: Thu, 16 Sep 2010 23:32:00 -0400 Subject: [PATCH] Added a library for determining FLV height and width. --- modules/noffmpeg/helpers/movie.php | 15 +- modules/noffmpeg/libraries/FLVMetaData.php | 194 +++++++++++++++++++++ 2 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 modules/noffmpeg/libraries/FLVMetaData.php diff --git a/modules/noffmpeg/helpers/movie.php b/modules/noffmpeg/helpers/movie.php index 801796b8..40d0940a 100644 --- a/modules/noffmpeg/helpers/movie.php +++ b/modules/noffmpeg/helpers/movie.php @@ -23,6 +23,9 @@ * * Note: by design, this class does not do any permission checking. */ + +include MODPATH . "noffmpeg/libraries/MP4Info.php"; + class movie_Core { static function get_edit_form($movie) { $form = new Forge("movies/update/$movie->id", "", "post", array("id" => "g-edit-movie-form")); @@ -113,7 +116,17 @@ class movie_Core { $extension = isset($pi["extension"]) ? $pi["extension"] : "flv"; // No extension? Assume FLV. $mime_type = in_array(strtolower($extension), array("mp4", "m4v")) ? "video/mp4" : "video/x-flv"; - return array(320, 240, $mime_type, $extension); + $vid_width = 320; + $vid_height = 240; + if (strtolower($extension) == "flv") { + $flvinfo = new FLVMetaData($file_path); + $info = $flvinfo->getMetaData(); + if (($info["width"] != "") && ($info["height"] != "")) { + $vid_width = $info["width"]; + $vid_height = $info["height"]; + } + } + return array($vid_width, $vid_height, $mime_type, $extension); //throw new Exception("@todo MISSING_FFMPEG"); // END rWatcher Edit. } diff --git a/modules/noffmpeg/libraries/FLVMetaData.php b/modules/noffmpeg/libraries/FLVMetaData.php new file mode 100644 index 00000000..bfd8e435 --- /dev/null +++ b/modules/noffmpeg/libraries/FLVMetaData.php @@ -0,0 +1,194 @@ +. + * + * @author Amin Saeedi, + * @copyright Copyright (c) 2009, Amin Saeedi + * @version 1.0 + * + */ +class FLVMetaData { + private $buffer; + private $metaData; + private $fileName; + private $typeFlagsAudio; + private $typeFlagsVideo; + + public $VCidMap = array( + 2=>"Sorenson H.263", + 3=>"Screen Video", + 4=>"VP6", + 5=>"VP6 with Alpha channel", + ); //Video Codec ID(s) + + public $ACidMap = array( + "Linear PCM, platform endian", + "ADPCM", + "MP3", + "Linear PCM, little endian", + "Nellymoser 16-kHz Mono", + "Nellymoser 8-kHz Mono", + "Nellymoser", + "G.711 A-law logarithmic PCM", + "G.711 mu-law logarithmic PCM", + "reserved", + "AAC", + "Speex", + 14=>"MP3 8-Khz", + 15=>"Device-specific sound" + ); //Audio Codec ID(s) + +/** + * CONSTRUCTOR : initialize class members + * + * @param string $flv : flv file path + */ + public function __construct($flv) { + $this->fileName = $flv; + $this->metaData = array( + "duration"=>null, + "size"=>null, + "framerate"=>null, + "width"=>null, + "height"=>null, + "videodatarate"=>null, + "audiodatarate"=>null, + "audiodelay"=>null, + "audiosamplesize"=>null, + "audiosamplerate"=>null, + "audiocodecid"=>null, + "videocodecid"=>null, + "version"=>null, + "headersize"=>0 + ); + } + +/** + * Gets metadata of FLV file + * + * @return array $this->metaData : matadata of FLV + */ + public function getMetaData(){ + if(!file_exists($this->fileName)){ + echo "Error! {$this->fileName} does not exist.
"; + return false; + } + if(!is_readable($this->fileName)){ + echo "Error! Could not read the file. Check the file permissions.
"; + return false; + } + $f = @fopen($this->fileName,"rb"); + if(!$f){ + echo "Unknown Error! Could not read the file.
"; + return; + } + $signature = fread($f,3); + if($signature != "FLV"){ + echo "Error! Wrong file format."; + return false; + } + $this->metaData["version"] = ord(fread($f,1)); + $this->metaData["size"] = filesize($this->fileName); + + $flags = ord(fread($f,1)); + $flags = sprintf("%'04b", $flags); + $this->typeFlagsAudio = substr($flags, 1, 1); + $this->typeFlagsVideo = substr($flags, 3, 1); + + for ($i=0; $i < 4; $i++) { + $this->metaData["headersize"] += ord(fread($f,1)) ; + } + + $this->buffer = fread($f, 400); + fclose($f); + if(strpos($this->buffer, "onMetaData") === false){ + echo "Error! No MetaData Exists."; + return false; + } + + foreach($this->metaData as $k=>$v){ + $this->parseBuffer($k); + } + return $this->metaData; + } + +/** + * Takes a field name of metadata, retrieve it's value and set it in $this->metaData + * + * @param string $fieldName : matadata field name + */ + private function parseBuffer($fieldName){ + $fieldPos = strpos($this->buffer, $fieldName); //get the field position + if($fieldPos !== false){ + $pos = $fieldPos + strlen($fieldName) + 1; + $buffer = substr($this->buffer,$pos); + + $d = ""; + for($i=0; $i < 8;$i++){ + $d .= sprintf("%08b", ord(substr($buffer,$i,1))); + } + + $total = self::bin2Double($d); + $this->metaData[$fieldName] = $total; + } + } + +/** + * Calculates double-precision value of given binary string + * (IEEE Standard 754 - Floating Point Numbers) + * + * @param string binary data $strBin + * @return Float calculated double-precision number + */ + public static function bin2Double($strBin){ + $sb = substr($strBin, 0, 1); // first bit is sign bit + $exponent = substr($strBin, 1, 11); // 11 bits exponent + $fraction = "1".substr($strBin, 12, 52); //52 bits fraction (1.F) + + $s = pow(-1, bindec($sb)); + $dec = pow(2, (bindec($exponent) - 1023)); //Decode exponent + + if($dec == 2047){ + if($fraction == 0){ + if($s==0){ + echo "Infinity"; + }else{ + echo "-Infinity"; + } + }else{ + echo "NaN"; + } + } + + if($dec > 0 && $dec < 2047){ + $t = 1; + for($i=1 ; $i <= 53; $i++){ + $t += ((int)substr($fraction, $i, 1)) * pow(2, -$i); //decode significand + } + $total = $s * $t * $dec ; + return $total; + } + return false; + } +} +?>