<?php


class pMMfile {
	var $ffmpeg;
	var $file;
	var $fileinfo;
	var $info_getid3;
	var $movie;
	var $info_ffmpeg;

	function pMMfile($file,$ffmpegcommand = "ffmpeg") {
		$this->file = $file;
		$this->ffmpeg = new pffmpeg($ffmpegcommand);
		$this->movie=false;
	}

	function Analyze() {
		$this->loadFFmpeg();
	}
	

	
	function loadFFmpeg() {
		if (!is_file($this->file)) return;
		if (is_array($this->info_ffmpeg)) return;
		$ffmpeg = new pFFmpeg();
		$this->info_ffmpeg = $ffmpeg->fileinfo($this->file);
	}

	
	function exist() {
		return is_file($this->file);
	 }

	function filesize() {
		$filesize = filesize($this->file);
		if ($filesize >(1024*1024)) {$sizestr = number_format($filesize/(1024*1024))." MB";} else {
			if ($filesize >(1024))  {$sizestr = number_format($filesize/1024)." KB";} else {
				$sizestr = number_format($filesize)." B";
			}
		}
		return $sizestr;
	}



	function playtime() {
		$this->loadFFmpeg();
		if (isset($this->info_ffmpeg['duration']))
			return $this->info_ffmpeg['duration'];
		return 0;
	}
	
	function FileFormat() {
		$this->loadFFmpeg();
		if (isset($this->info_ffmpeg['format']))
			return $this->info_ffmpeg['format'];
		return false;
	}
	
	function IsPlayable() {
		return ($this->fileformat()=="mp3" || $this->fileformat()=="flv" || $this->ffmpeg->test()===false);
	}
	
	function hasaudio() {
		$this->loadFFmpeg();
		if (isset($this->info_ffmpeg['audio'])) return true;
		return false;
	}
	function hasvideo() {
		$this->loadFFmpeg();
		if (isset($this->info_ffmpeg['video'])) return true;
		return false;
	}
	function width() {
		$this->loadFFmpeg();
		if (isset($this->info_ffmpeg['video']['resolution_x']))
			return $this->info_ffmpeg['video']['resolution_x'];
		return 0;
	}
	function height() {
		$this->loadFFmpeg();
		if (isset($this->info_ffmpeg['video']['resolution_y']))
			return $this->info_ffmpeg['video']['resolution_y'];
		return 0;
	}
}

class pFFmpeg {
	var $command;
	var $output;


	

	function pFFmpeg($command = "ffmpeg") {
		$this->command = $command;
		$this->output = "";
	}
	
	function test() {
		$this->exec("-version");
		if (substr($this->output,0,14)!="FFmpeg version") return false;
		return true;
	}
	
	function exec($options) {
		$result = array();
		//$cmd = $this->command." ".escapeshellarg($options)." 2>&1";
		$cmd = $this->command." ".$options." 2>&1";
		// 2>&1 is used to redirect error messages to standard output
		$this->setoutput(array($cmd));
		exec($cmd,$result,$status);
		$this->setoutput($result,false);
		return ($status == 1);
	}

	function setOutput($outputarray, $clear = true)  {
		if ($clear) $this->output = "";
		foreach ($outputarray as $line)
			$this->output .= $line."\n";
	}


	function grabPicture($inputfile,$outputfile,$forceoverwrite=true,$size="",$when="00:00:03") {
		$format = strtolower(substr($outputfile,strrpos($outputfile, '.')+1));
		switch ($format) {
			case "jpg": $format = "-vcodec mjpeg"; break;
			case "gif" : $format = "-vcodec gif"; break;
			case "png" : $format = "-vcodec png"; break;
			default : $format = $format;
		}
		$overwrite = ( $forceoverwrite ? "-y" : "");
		$size = ( $size=="" ? "" : "-s $size");

		if (is_file($outputfile) && !$forceoverwrite)
		{
			$this->setOutput(array("File already exists"));
			return false;
		}

		$this->exec("-i $inputfile $overwrite $size -an -vframes 1 -ss $when -f rawvideo $format $outputfile");

		return is_file($outputfile);
	}

	function converttoFLV($inputfile,$outputfile,$samequality=false,$forceoverwrite=true) {
		if (is_file($outputfile) && !$forceoverwrite)
		{
			$this->setOutput(array("File already exists"));
			return false;
		}
		if (!strtolower(substr($outputfile,strlen($outputfile)-4))==".flv")
		{
			$this->setOutput(array("Wrong flv filename : ".$outputfile));
			return false;
		}
		$overwrite = ( $forceoverwrite ? "-y" : "");
		$sameq = ( $samequality ? "-sameq" : "");
		$this->exec("-i $inputfile $overwrite $sameq -f flv $outputfile");
		return is_file($outputfile);
	}

	function converttoMP3($inputfile,$outputfile,$samequality=false,$forceoverwrite=true) {
		if (is_file($outputfile) && !$forceoverwrite)
		{
			$this->setOutput(array("File already exists"));
			return false;
		}
		if (!strtolower(substr($outputfile,strlen($outputfile)-4))==".mp3")
		{
			$this->setOutput(array("Wrong mp3 filename : ".$outputfile));
			return false;
		}

		$overwrite = ( $forceoverwrite ? "-y" : "");
		$sameq = ( $samequality ? "-sameq" : "");
		$this->exec("-i $inputfile -y $overwrite $sameq -ar 44100 $outputfile");
		return is_file($outputfile);
	}

	function formats() {
		$this->exec("-formats");
		return $this->output;
	}
	
	function FileInfo($file) {
		$result = array();
		$this->exec("-i ".$file);
		
		if (strpos($this->output,"I/O error occured")!==false)
			return array(); // file not found
		
		if (preg_match('/Input #0, (.*?),/', $this->output, $matches)>0)
			$result['format'] = $matches[1];
		if (preg_match('/Duration: (.*?),/', $this->output, $matches)>0)
			$result['duration'] = $matches[1];
		if (preg_match('/bitrate: (.*?)\n/', $this->output, $matches)>0)
			$result['bitrate'] = $matches[1];
		if (preg_match('/: Audio: (.*?)\n/', $this->output, $matches)>0) {
			$result['audio']['parameters'] = $matches[1];
			$result['audio']['codec'] = substr($result['audio']['parameters'],0,strpos($result['audio']['parameters'],","));
			if (preg_match('/, (.*?) Hz/', $result['audio']['parameters'], $matches)>0)
				$result['audio']['sample_rate'] = $matches[1];
			if (preg_match('/, ([^,]*?) kb\/s/', $result['audio']['parameters'], $matches)>0)
				$result['audio']['bitrate'] = $matches[1];
		}
		if (preg_match('/: Video: (.*?)\n/', $this->output, $matches)>0) {
			$result['video']['parameters'] = $matches[1];
			$result['video']['codec'] = substr($result['video']['parameters'],0,strpos($result['video']['parameters'],","));
			
			
			if (preg_match('/, ([^,]*?) fps/', $result['video']['parameters'], $matches)>0)
				$result['video']['framerate'] = $matches[1];
			if (preg_match('/, ([^,]*?) kb\/s/', $result['video']['parameters'], $matches)>0)
				$result['video']['bitrate'] = $matches[1];
			if (preg_match('/, ([0-9]*?)x([0-9]*?)(,|\r)/', $result['video']['parameters'], $matches)>0) {
				$result['video']['resolution_x'] = $matches[1];
				$result['video']['resolution_y'] = $matches[2];
				$result['video']['size'] = $matches[1]."x".$matches[2];
			}
		}

		return $result;

	
	}

}

/*
$F = new pFFmpeg();

echo "<pre>";
$F->fileinfo("barsandtone.flv");
echo "</pre>";

echo "<pre>";
$F->fileinfo("video_13.mpg");
echo "</pre>";

echo "<pre>";
$F->fileinfo("test.mp3");
echo "</pre>";


echo "<pre>";
$F->fileinfo("test.flv");
echo "</pre>";

echo "<pre>";
$F->fileinfo("Terminus.avi");
echo "</pre>";
*/
?>