Php tool class [video conversion class]

Source: Internet
Author: User
Tags flv file subq
[Video conversion class ]??? Here we will briefly introduce the software required to build a video website, including ffmpeg and mplayer. They are mainly used for video transcoding. ffmpeg can basically process all video files in all formats, but cannot transcode videos in rmvb and rm formats, you need to use the MPlayer transcoding tool to complete the transcoding task .?? To play the video on a webpage, you need to transcode the php tool class [video conversion class]

?

? ? Here we will briefly introduce the software required to build a video website, including ffmpeg and mplayer. They are mainly used for video transcoding. ffmpeg can basically process all video files in all formats, but cannot transcode videos in rmvb and rm formats, you need to complete the transcoding task with the assistance of the MPlayer transcoding tool.

? ? Transcoding is required if you want to play the video on a webpage. If you use a flash player to play a video, you need to transcode the video in flv format. if you use html5 or pad to play the video, you need to transcode the video in MP4 format. During transcoding, we usually transcode two types of videos: high-definition videos and smooth videos for users with different network speeds.

Class VideoConvert {// original video file var $ src; // suffix var $ suffix; // actual video type var $ format; // video md5value: in tester, it is mainly used to generate the initial position var $ md5value of the thumbnail under the same level directory; // video length var $ ori_length; // video information var $ src_identify = array (); // error log var $ err_log = array (); // command address 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; // the source file does not exist if (! File_exists ($ this-> src) {print_r ("the file [$ this-> src] not exists. \ r \ n
");} Else {print_r (" the file [$ this-> src] exists. \ r \ n
") ;}} Function init () {// specifies the suffix $ this-> suffix = strtolower (substr ($ this-> src, strrpos ($ this-> src ,'. '); // read the first three bytes of the file content and determine the actual file format $ handle = fopen ($ this-> src, 'r '); $ this-> format = strtolower (fread ($ handle, 3); fclose ($ handle ); // the video's md5 value $ this-> md5value = md5_file ($ this-> src ); // inentify $ this-> src_identify = $ this-> getidenfy ($ this-> src ); // video length $ this-> ori_length = $ this-> src_identify ['id _ length']; $ this-> Ori_length = empty ($ this-> ori_length )? 0: $ this-> ori_length;} function showInfo () {$ this-> pr ("suffix type: $ this-> suffix "); $ this-> pr ("actual type: $ 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 16 k-vcodec libx264-coder 1-g 250-keyint_min 25-12740-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 libmp 3lame-y $ dst"; // 2> & 1 "; $ this-> pr ($ cmd); $ handle = @ popen ($ cmd, 'r'); while (! Feof ($ handle) {$ line = fgets ($ handle, 1024) ;}@ pclose ($ handle); return true ;} // Convert mp4 to function video2mp4 ($ src, $ dst, $ rate) {file_exists ($ dst. ". mp4 ") [email 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 128 k-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 screenshot-OK ** @ param string $ src source file * @ param string $ dst target file * @ param int $ length * @ param int $ pic_count * @ access public * @ return void */f Unction grabImage ($ src, $ dst, $ length, $ pic_count) {$ grabRes = $ this-> grabImageFfmpeg ($ src, $ dst, $ length, $ pic_count ); if (@! Filesize ($ dst) {return false;} return $ grabRes ;} /*** grabImageFfmpeg capture images through ffmpeg-OK ** @ param string $ src source file * @ param string $ dst target file * @ access public * @ return void */function grabImageFfmpeg ($ src, $ dst, $ length, $ pic_count) {// in the middle of the video $ 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 = maid; $ 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 ;} /*** create a new image from an existing image with resizeImage-OK * @ param string $ src source file * @ param string $ obj target file * @ param string $ width * @ param string $ height the target file 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 = ($ h Eight/$ 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, $ widt H_orig, $ height_orig); imagejpeg ($ image_p, $ obj); return true ;} /*** injectMetaData adds metadata to the flv file-OK * to support player drag and drop * return flv duration * @ param string $ file * @ access public * @ return init or false, if failed */function injectMetaData ($ file) {$ info = array ('code' => false, 'MSG '=>' not start '); // whether the object exists if (! File_exists ($ file) {$ info ['MSG '] =' The file does not exist. '; 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-> getidense ($ 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
";}/*** Getiden get video information-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 ); // if the mplayer does not get the video length, use ffmpeg to get the video length again. // The video length $ return = $ identify ['id _ length']; if ($ return = ''|! Is_numeric ($ return) {// Obtain $ cmd = $ this-> system_ffmpeg again. "-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 ){}}

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.