<?php
/**
* Video preview Plugin - This will allow preview on video files
* author: ionutvmi
* 6 Oct 2012
*/

$plugins->add_hook("icon_top","video_get");
$plugins->add_hook("index_files","video_preview");
$plugins->add_hook("file","video_preview2");

function video_info(){
    return    array(    
    "name" => "Video Plugin",
    "author" => "ionutvmi",
    "author_site" => "http://master-land.net",
    "description" => "This will allow preview on video files(requires ffmpeg installed)",
    );
}


function video_preview(){
    global $icon,$ext,$d;
    if(in_array($ext->extension,array("3gp","mp4","avi","wmv")))
        $icon = "/icon.php?s=".base64_encode($d->path);
    
}

function video_preview2($value){
    global $ext,$icon,$show_icon,$file;
    if(in_array($ext->extension,array("3gp","mp4","avi","wmv"))){
        $new_icon = "/icon.php?s=".base64_encode($file->path);
        $show_icon = str_replace($icon,$new_icon,$show_icon);
    }
    

    return $value;
}


function video_get(){
	global $ext;
	$file = MAI_ROOT.substr(base64_decode($_GET["s"]),1);
	$info = (object)pathinfo($file);
	if(!in_array($info->extension,array("3gp","mp4","avi","wmv")))
		return true;
	
	if(!extension_loaded('ffmpeg')){
		$cz=file_get_contents(MAI_TPL."style/png/3gp.png");
		header("Content-type: image/png");
		echo $cz;
	}

	$media = new ffmpeg_movie($file);
	$k_frame=intval($media->getFrameCount());
	$w=$media->GetFrameWidth(); 
	$h=$media->GetFrameHeight(); 


	$frame = $media->getFrame(10);
	if ($frame) {
		$gd1 = $frame->toGDImage(); 
		$gd2 = imagecreatetruecolor(100,100); 
		imagecopyresampled($gd2, $gd1, 0,0,0,0, 100, 100, $w, $h); 
		if($gd2){ 
			header('Content-Type: image/jpeg'); 
			imagejpeg($gd2); 
			imagedestroy($gd1); 
			imagedestroy($gd2); 
		}
	}
}