ï»¿<?php
/**
 * MP4Info
 * 
 * @author 		Tommy Lacroix <lacroix.tommy@gmail.com>
 * @copyright   Copyright (c) 2006-2009 Tommy Lacroix
 * @license		LGPL version 3, http://www.gnu.org/licenses/lgpl.html
 * @package 	php-mp4info
 * @link 		$HeadURL$
 *
 * Version allégée par Jean-Yves Giraud (décembre 2013) pour récupérer uniquement largeur et hauteur d'une video MP4
 *
 */

// ---

/**
 * MP4Info main class
 * 
 * @author 		Tommy Lacroix <lacroix.tommy@gmail.com>
 * @version 	1.1.20090611	$Id$
 */
class MP4Info {
	const MP4_VIDEO_CODEC_H264 = 0xe0;

	/**
	 * Get information from MP4 file
	 *
	 * @author 	Tommy Lacroix <lacroix.tommy@gmail.com>
	 * @param	string		$file
	 * @return	array
	 * @access 	public
	 * @static
	 */
	public static function getInfo($file) {
		// Open file
		$f = @fopen($file,'rb');
		if (!$f)
			return false;
		
		// Get all boxes
		$boxes = array();
		while (($box = MP4Info_Box::fromStream($f))) {
			$boxes[] = $box;
		}
		
		// Close
		fclose($f);
		
		// Return info
		if ($boxes == array())
			return false;
		else
			return self::getInfoFromBoxes($boxes);
	} // getInfo method
	
	
	/**
	 * Get information from MP4 boxes
	 *
	 * @author 	Tommy Lacroix <lacroix.tommy@gmail.com>
	 * @param	string		$file
	 * @return	array
	 * @access 	public
	 * @static
	 */	
	public static function getInfoFromBoxes($boxes, &$context=null) {
		if ($context === null) {
			$context = new stdClass();
			$context->hasVideo = false;
			$context->hasAudio = false;
			$context->video = new stdClass();
			$context->audio = new stdClass();
			$context->tracks = array('video'=>array(),'audio'=>array());
			$root = true;
		} else {
			$root = false;
		}
		
		// Process each box
		foreach ($boxes as &$box) {
			// Interpret box
			switch ($box->getBoxTypeStr()) {
				case 'stsd':
					$values = $box->getValues();
					foreach ($values as $code=>$data) {
						switch ($code) {
							case 'avc1':
							case 'mp4v':
							case 'h264':
							case 'H264':
								$context->video->codec = self::MP4_VIDEO_CODEC_H264;
								$context->video->codecStr = 'H.264';
								if (is_array($data)) {
									if (isset($data['width'])) {
										$context->video->width = $data['width'];
									}
									if (isset($data['height'])) {
										$context->video->height = $data['height'];
									}
								}
								$context->hasVideo = true;
								break;
							default:
								break;
						}
					}
					break;
			}
			
			// Process children
			if ($box->hasChildren()) {
				self::getInfoFromBoxes($box->children(), $context);
			}
		}
		return $context;
		
	} // getInfoFromBoxes method
	
} // MP4Info class

include "Box.php";
