搜尋
首頁後端開發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 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP:輕鬆創建交互式Web內容PHP:輕鬆創建交互式Web內容Apr 14, 2025 am 12:15 AM

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python:比較兩種流行的編程語言PHP和Python:比較兩種流行的編程語言Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP的持久相關性:它還活著嗎?PHP的持久相關性:它還活著嗎?Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

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

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尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能