Video.class.php 4.26 KB
<?php
class Lib_Video
{
	private static $height = 0;
	private static $width = 0;
	/**
	 * 获取视频缩略图
	 * 
	 * @param string $file
	 * @return string
	 */
	public static function getVideoThumb($file)
	{
		$realPath = '';
		$path = pathinfo($file);
		$thumbPic = sprintf('%s.jpg', $path['dirname'].'/'.md5(uniqid()));
		$command = 'ffmpeg -y -i '.escapeshellcmd($file);
		$command .= ' -f image2 -vframes 1 '.$thumbPic;
		exec($command, $result , $retval);
		if(empty($retval))
		{
			$size = getimagesize($thumbPic);
			self::$height = $size[1];
			self::$width = $size[0];
			$realPath = Lib_Images::saveImage($thumbPic);
			if(empty($realPath))
			{
				$realPath = '';
			}
			//上传图片
			@unlink($thumbPic);
		}
		return $realPath;
	}
	
	/***
	 * 裁剪视频
	 * 
	 * @param string $file
	 * @param int $top
	 * @param int $bottom
	 * @param int $left
	 * @param int $right
	 * @return boolean
	 */
	public static function cropVideo($file, $top = 0, $bottom = 0, $left = 0, $right = 0)
	{
		$oldSize = filesize($file);
		$command = 'ffmpeg -y -i '.escapeshellcmd($file);
		$command.= sprintf(" -croptop %d -cropbotom %d -cropleft %d -cropright %d %s",
				 $top, $top, $bottom, $left, $right, escapeshellcmd($file));
		exec($command, $result , $retval);
		$newSize = filesize($file);
		if(empty($retval) && $newSize<$oldSize)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 静音
	 * 
	 * @param string $file
	 * @return boolean
	 */
	public static function muteVideo($file)
	{
		$command = sprintf('ffmpeg -y -i %s -an %s', escapeshellcmd($file), escapeshellcmd($file));
		exec($command, $result , $retval);
		if(empty($retval))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 旋转视频90度
	 * 
	 * @param string $file
	 * @return boolean
	 */
	public static function rotateVideo($file)
	{
		$path = pathinfo($file);
		$tmpVideo = sprintf('%s.mp4', $path['dirname'].'/'.md5(uniqid()));
		$command = 'mencoder -fps 30000/1001 -ofps 30000/1001 -ovc x264 -vf rotate=1 ';
		$command .= ' -oac faac -srate 24000 -faacopts mpeg=4:object=2:raw:br=64 -of lavf ';
		$command .= ' -lavfopts format=mp4 -x264encopts nocabac:threads=1024 ';
		$command .= sprintf('%s -o %s ',escapeshellcmd($file), escapeshellcmd($tmpVideo));
		file_put_contents('/tmp/video.txt', $command);
		exec($command, $result , $retval);
		if(empty($retval))
		{
			@rename($tmpVideo, $file);
			@unlink($tmpVideo);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 保存视频
	 * 
	 * @param string $file
	 * @return string {url:string, video_url:string}
	 */
	public static function saveVideo($file)
	{
		$path = Config_File_Video::$videoServer['video'].Config_File_Upload::$video['path'];
		$tmpFile = current($file);
		$videoKey = key($file);
	    //$cpFile = sprintf("%s/%s",dirname($tmpFile['tmp_name']), md5(uniqid()));
		//@copy($tmpFile['tmp_name'], $cpFile);
		$up = new Util_Upload_File($file, $path);
		$up->setFormat(Config_File_Upload::$video['format']);
		$up->setMaxFileSize(Config_File_Upload::$video['upload_max_size']);
		$up->setSuffix('mp4');
		$checkStatus = $up->check();
		$video = '';
		if($checkStatus['code'] == 0)
		{
			$checkStatus = $up->write ();
			$video = $checkStatus ['result'] ['hit'][$videoKey]['relaPath'];
			$pic = '';
			$times = 10;
			/*while(empty($pic) && $times > 0)
			{
				$pic = self::getVideoThumb($cpFile);
				$times --;
			}*/
	//		@unlink($cpFile);
			return self::encode($video, $pic);
		}
		else
		{
			return '';
		}
		
	}
	
	/**
	 * 获取视频地址
	 *
	 * @param string $video
	 * @return string
	 */
	public static function getVideoUrl($video)
	{
		return VIDEO_SITE_DOMAIN.$video;
	}
	
	/**
	 * 格式化视频数据
	 * 
	 * @param string $video_url
	 * @param string $url
	 * @return string
	 */
	public static function encode($video_url, $url)
	{
		$data = array('video_url'=> $video_url, 'url'=> $url);
		return Util_Json::encode($data);
	}
	
	/**
	 * 返回格式化视频数据 [{url:string, video_url:string}]
	 *  
	 * @param string $data 
	 * @return array
	 */
	public static function decode($data)
	{
		$data = Util_Json::decode($data, true);
		if(isset($data['video_url']))
		{
			$data['video_url'] = self::getVideoUrl($data['video_url']);
		}
		else
		{
			$data = array('video_url'=>'', 'url'=> $data);
		}
		return $data;
	}
}