搜索
首页后端开发php教程php工具类之【视频变换类】

php工具类之【视频转换类】

?

? ? 在这里简要介绍一下搭建视频网站所需要的软件,这些软件包括ffmpeg、mplayer。它们主要用来负责视频的转码工作,ffmpeg基本上对所有格式的视频文件都可以处理,但是对rmvb和rm格式的视频无法转码,这个时候,就需要通过MPlayer转码工具的协助,完成转码任务。

? ? 如果要在网页上播放,就需要转码。如果采用flash播放器播放视频,这个时候就需要转码出flv格式的视频;如果采用html5或者pad播放,就需要转码出MP4格式的视频。在转码的处理中,我们通常会分别转码出两种视频,分别是高清和流畅视频,方便不同网速的用户观看。

class VideoConvert{	//视频的原始文件	var $src;	//后缀名	var $suffix;	//视频实际类型	var $format;	//视频md5value: 在tester中主要用于生成同级目录下的缩略图的初始位置	var $md5value;	//视频长度	var $ori_length;		//视频信息	var $src_identify = array();	//错误日志	var $err_log = array();	//指令地址	var $system_mencoder = '/usr/bin/mencoder ';	var $system_ffmpeg = '/usr/local/bin/ffmpeg ';	var $system_mplayer = '/usr/bin/mplayer ';	var $system_yamdi = '/usr/local/bin/yamdi ';	var $system_qtfaststart = '/usr/local/bin/qt-faststart ';	function __construct($filePath) {		$this->src = $filePath;		//源文件不存在		if (!file_exists($this->src)) {			print_r("the file[$this->src] not exists.\r\n<br>");		} else {			print_r("the file[$this->src] exists.\r\n<br>");		}	}		function init() {		//截取后缀名		$this->suffix = strtolower(substr($this->src, strrpos($this->src, '.')));		        //读取文件内容的前3个字节,判断真实文件格式		$handle = fopen($this->src, 'r');        $this->format = strtolower(fread($handle, 3));        fclose($handle);				//视频的md5值		$this->md5value = md5_file( $this->src );				//inentify		$this->src_identify =$this->getIdentify($this->src);				//视频长度		$this->ori_length = $this->src_identify['id_length'];		$this->ori_length = empty($this->ori_length)?0:$this->ori_length;	}	function showInfo() {		$this->pr("后缀类型:$this->suffix");		$this->pr("实际类型:$this->format");		$this->pr("md5value:$this->md5value");		$this->pr("ori_length:$this->ori_length");		$this->pr("id_demuxer:".$this->src_identify['id_demuxer']);		$this->pr("id_video_format:".$this->src_identify['id_video_format']);	}	function rmvb2avi($src, $dst, $identify) {		//$cmd = $this->system_mencoder." $src -o ".$src."_ -of avi -oac mp3lame -ovc xvid -xvidencopts bitrate=$datarate";		if($identify['id_video_format'] == 'WMV3') {			$cmd = $this->system_mencoder." $src -o $dst -of avi -oac mp3lame -ovc copy";		} else {			$cmd = $this->system_mencoder." $src -o $dst -of avi -oac mp3lame -ovc xvid -xvidencopts fixed_quant=11";		}		$this->pr($cmd);		$handle = @popen($cmd, 'r');		while (!feof($handle)) {			$line = fgets($handle, 1024);		}		@pclose($handle);		return true;	}		function video2f4v($src, $dst, $datarate) {		$cmd = $this->system_ffmpeg." -i ".$src."  -f flv -acodec libfaac -ab 16k -vcodec libx264 -coder 1 -g 250 -keyint_min 25 -sc_threshold 40 -bf 3 -b_strategy 1 -partitions +parti8x8+parti4x4+partp8x8+partb8x8 -directpred 1 -flags +loop -deblockalpha 0 -deblockbeta 0 -flags2 +fastpskip+wpred-dct8x8 -me_method hex -me_range 16 -subq 6 -trellis 1 -b ".$datarate."k -qcomp 1 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 -r 29.97 -y ".$dst;//." 2>&1";		$this->pr($cmd);		$handle = @popen($cmd, 'r');		while(!feof($handle)) {			$line = fgets($handle, 1024);		}		@pclose($handle);		return true;	}		function video2flv($src, $dst) {		$cmd = $this->system_ffmpeg." -i $src -f flv -vcodec flv -ar 22050 -acodec libmp3lame -y $dst";		// 2>&1";		$this->pr($cmd);		$handle = @popen($cmd, 'r');		while(!feof($handle)) {			$line = fgets($handle, 1024);		}		@pclose($handle);		return true;	}		//转换mp4供iphone和ipad看	function video2mp4($src, $dst, $rate) {		file_exists($dst.".mp4")[email&#160;protected]($dst.".mp4"):'';		$cmd = $this->system_ffmpeg." -threads 4 -i ".$src." -r 29.97 -vcodec libx264 -flags +loop -cmp +chroma -deblockalpha 0 -crf 24 -bt ".$rate."k -refs 1 -coder 0 -subq 5 -partitions +parti4x4+parti8x8+partp8x8 -g 250 -keyint_min 25 -level 30 -qmin 10 -qmax 51 -trellis 2 -sc_threshold 40 -i_qfactor 0.71 -acodec libfaac -ab 128k -ar 48000 -f mp4 ".$dst.".mp4";		//2>&1";		$this->pr($cmd);		$handle = @popen($cmd, 'r');		while(!feof($handle)) {			$line = fgets($handle, 1024);		}		@pclose($handle);				$mp4identify = $this->getIdentify($dst.".mp4");		$mp4le = abs($mp4identify['id_length']);		if($mp4le>0) {			$cmd = $this->system_qtfaststart." ".$dst.".mp4 ".$dst.".mp4-new"." 2>&1";			$handle = @popen($cmd, 'r');			while(!feof($handle)) {				$line = fgets($handle, 1024);			}			@pclose($handle);			$mp4identify = $this->getIdentify($dst.".mp4-new");			$mp4le = abs($mp4identify['id_length']);			if ($mp4le>0) {				unlink($dst.".mp4");				rename($dst.".mp4-new",$dst.".mp4");			}		}				return true;	}	/**	 * grabImage 抓图-ok	 * 	 * @param string $src 源文件	 * @param string $dst 目标文件	 * @param int $length 时长	 * @param int $pic_count 截图数量	 * @access public	 * @return void	 */	function grabImage($src, $dst, $length,$pic_count) {		$grabRes = $this->grabImageFfmpeg($src, $dst, $length,$pic_count);        if (@!filesize($dst)) {            return false;        }		return $grabRes;	}	/**	 * grabImageFfmpeg 通过ffmpeg抓图-ok	 * 	 * @param string $src 源文件	 * @param string $dst 目标文件	 * @access public	 * @return void	 */	function grabImageFfmpeg($src, $dst, $length,$pic_count) {		//在视频中间截图		$ss = $length/2;		$cmd = $this->system_ffmpeg ." -y -i $src -vframes 1 -ss $ss -an -vcodec mjpeg -f rawvideo $dst 2>&1";			$fd = @popen($cmd, 'r');		while (!feof($fd)) {			$line = fgets($fd, 1024);		}		@pclose($fd);		$count = $pic_count+1;		if ($length>$count) {			$s = $length/$count;			for ($i=1;$i<$count;$i++) {				$dstpic = $dst.'.'.$i.'.jpg';				$cmd = $this->system_ffmpeg ." -y -i $src -vframes 1 -ss ".($i*$s)." -an -vcodec mjpeg -f rawvideo $dstpic 2>&1";				$fd = @popen($cmd, 'r');				while (!feof($fd)) {					$line = fgets($fd, 1024);				}				fclose($fd);			}		}		return true;	}		/**	* resizeImage从一个已有图片建立一个新的图片-ok	 * @param string $src 源文件	 * @param string $obj 目标文件	 * @param string $width 目标文件宽	 * @param string $height 目标文件高	 * @access public	*/	function resizeImage($src, $obj, $width, $height) {		list($width_orig, $height_orig, $type_orig) = getimagesize($src);		if ($width && ($width_orig < $height_orig)) {			$width = ($height / $height_orig) * $width_orig;		} else {			$height = ($width / $width_orig) * $height_orig;		}		switch($type_orig) {			case 1: 				$image = imagecreatefromgif($src);				break;			case 2: 				$image = imagecreatefromjpeg($src);				break;			case 3: 				$image = imagecreatefrompng($src);				break;			default:				return false;		}		$image_p = imagecreatetruecolor($width, $height);		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);		imagejpeg($image_p, $obj);		return true;	}	/**	 * injectMetaData 向flv文件添加元数据-ok	 * 				以支持播放器的拖放     * 				返回flv时长	 * @param string $file 	 * @access public	 * @return init or false, if failed	 */	function injectMetaData($file) {		$info = array('code'=>false, 'msg'=>'未开始');		//文件是否存在		if (!file_exists($file)) {			$info['msg'] = '文件不存在。';			return $info;		}		$cmd = $this->system_yamdi . " -i $file -o ".$file."_ | grep lasttimestamp";		$fd = @popen($cmd, 'r');        $lasttimestamp = 0;		while (!feof($fd)) {			$line = fgets($fd, 1024);            if (strpos($line, ':')) {                $lasttimestamp = substr($line, strpos($line, ':') + 1);            }		}		pclose($fd);		$l = $this->getIdentify($file.'_');		if ($l['id_length']) {			unlink($file);			rename($file.'_',$file);		}				//Logger::trace(sprintf('lasttimestamp: %s', $lasttimestamp));		//Logger::debug('Inject End');		if ($lasttimestamp == '' || $lasttimestamp == 0){			$lasttimestamp = $this->getIdentify($file);		}		return $lasttimestamp['id_length'];	}	function pr($msg) {		echo "$msg\r\n<br>";	}		/**	 * getIdentify 获取视频信息-ok	 * @access public	 * @return void	 */	function getIdentify($file) {		$identify = array();		if (!is_readable($file)) {			return false;		}		$cmd = $this->system_mplayer . " -msglevel all=0 -identify -vc dummy -endpos 00:00:00 $file 2>&1";		$fd = @popen($cmd, 'r');		while (!feof($fd)) {			$line = fgets($fd);			if (strpos($line, 'ID_') === 0) {				$line = explode('=', $line);				$line[0] = strtolower($line[0]);				$identify[$line[0]] = trim($line[1]);			}		}		@pclose($fd);		//假如mplayer没有获取到视频长度就用ffmpeg再次获取		//视频长度		$return = $identify['id_length'];		if ($return == '' || !is_numeric($return)){			//再次获取						$cmd = $this->system_ffmpeg . " -i $file 2>&1";			$fd = @popen($cmd, 'r');			while (!feof($fd)) {				$line = fgets($fd);				$line = trim($line);				$line = strtolower($line);				if (strpos($line, 'duration:') === 0) {					$line = explode(',', $line);					$line = explode(':', $line[0]);					$identify['id_length'] = abs($line[1])*3600+abs($line[2])*60+abs(((int)$line[3]));					break;				}			}		}		return $identify;	}		function log($key, $value) {	}}

?

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP的当前状态:查看网络开发趋势PHP的当前状态:查看网络开发趋势Apr 13, 2025 am 12:20 AM

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP与其他语言:比较PHP与其他语言:比较Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能PHP与Python:核心功能Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP:网络开发的关键语言PHP:网络开发的关键语言Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP:许多网站的基础PHP:许多网站的基础Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

超越炒作:评估当今PHP的角色超越炒作:评估当今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

PHP中的弱参考是什么?什么时候有用?PHP中的弱参考是什么?什么时候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

解释PHP中的__ Invoke Magic方法。解释PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。