A PHP class for ffmpeg video conversion

Source: Internet
Author: User
 Opti $this->options = Array_filter ($options) + $this->options;    $this->ffmpeg= $ffmpeg,//ffmpeg path $this->backup= $backup;        }//Get video information function Video_info ($file) {Ob_start (); PassThru (sprintf ($this->ffmpeg.        '-i '%s ' 2>&1 ', $file));//ffmpeg-i Test.avi 2>&1 $info = ob_get_contents ();        Ob_end_clean ();        Gets the contents of all outputs to ffmpeg by using the output buffer.        $ret = Array (); duration:01:24:12.73, start:0.000000, bitrate:456 kb/s if (Preg_match ("/duration: (. *?), Start: (. *?), Bitrat              E: (\d*) kb\/s/", $info, $match)) {$ret [' duration '] = $match [1];//extract playback time $da            = Explode (': ', $match [1]); $ret [' seconds '] = $da [0] * 3600 + $da [1] * + $da [2]; Convert to seconds $ret [' start '] = $match [2]; Start time $ret [' bitrate '] = $match [3]; Bitrate Bitrate Unit KB}//Stream #0.1:video:rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12TBR, 1k TBN, TBC if (Preg_match ("/video"), (. *), (. *), (. *), (. *?) [, \s]/", $info, $match)) {$ret [' vcodec '] = $match [1];//encoding Format $ret [' vformat '] = $MATC H[2]; Video format $ret [' Resolution '] = $match [3];            Resolution $a = explode (' x ', $match [3]);            $ret [' width '] = $a [0];        $ret [' height '] = $a [1]; }//Stream #0.0:audio:cook, 44100 Hz, stereo, S16, kb/s if (Preg_match ("/audio: (\w*), (\d*) hz/", $in       FO, $match)) {$ret [' acodec '] = $match [1];  Audio encoding $ret [' asamplerate '] = $match [2]; Audio Sampling Frequency} if (Isset ($ret [' seconds ']) && isset ($ret [' Start ']) {$ret [' play_time ' ] = $ret [' seconds '] + $ret [' Start ']; Actual playback time} $ret [' size '] = filesize ($file);    File size return $ret;    } function convert () {$verifyToken = MD5 (' Unique_salt '. $this->options[' timestamp ');    if ($verifyToken = = $this->options[' token ') {$orgFile = $this->options[' Org_path ']. $this-            >options[' org ';            $setting = "; if (isset ($this->options[' video_size ')) $setting = $setting. '-vf scale= '. $this->options[' video_size '].            '"'; $MP 4 = $this->ffmpeg. '-I. ' $orgFile. '-ss 00:01:02-vcodec libx264-strict-2 '. $setting. ' ' . $this->options[' mp4_path_temp '). $this->options[' org ').            ";//Convert video exec ($MP 4); if (isset ($this->options[' watermark ')) {$watermark = ' "'. $this->options[' watermark ']. '"            '; $MP 4 = $this->ffmpeg. '-i ' $this->options[' mp4_path_temp '. $this->options[' org '). ". '. '-VF '. $watermark. ' '. $this->options[' Mp4_path '). $this->options[' org ').            ";//Add watermark Exec ($MP 4); @unlink ($this->options[' mp4_path_temp '). $this->options[' org '.            ''); } $duration = $this-> Video_info ($orgFile);            $seconds = Intval ($duration [' seconds ']);            $offset = Intval ($seconds/21);//interval seconds $thumbs = explode (', ', $this->options[' thumb_size ')); for ($i = 0; $i <= count ($thumbs), $i + +) {$targetPath = $this->options[' Thumb_path '). $t his->options[' org ').                '/';                if (!file_exists ($targetPath)) @mkdir (RTrim ($targetPath, '/'), 0777);                    if ($i = = 0) {$time = 1; $name = $i = = 0? ' Default.jpg ': $i.                    '. jpg ';                $img _size = $this->options[' main_size ');                } else {$time = $i * $OFFSET; $name = $i.                '. jpg ';                $img _size = $thumbs [$i-1]; } $jpg = $this->ffmpeg. '-I. ' $orgFile. '-f image2-ss '. $time. '-vframes 1-s '. $img _size.' ' . $targetPath.            $name;//@exec ($jpg);            }//Copy files to the corresponding FTP server $ftp _server = pc_base::load_config (' Ftp_server ');            $remote _server = $_post[' remote_server ');            $ftp _server = $ftp _server[$remote _server]; if ($ftp _server[' Ftp_server ') pc_base::ftp_upload ($orgFile, $ftp _server[' ftp _server '], $ftp _server[' ftp_user_name ', $ftp _server[' Ftp_us            Er_pass ']);                Back up to all FTP servers if ($this->backup) {$ftp _backup = pc_base::load_config (' ftp_backup ');                                        foreach ($ftp _backup as $v) {pc_base::ftp_upload ($orgFile,                                        $ftp _backup[' Ftp_server ', $ftp _backup[' ftp_user_name '),                $ftp _backup[' Ftp_user_pass ');       }     } $result [' url ']= $ftp _server[' ftp_server '] [' http_address ']. $this->options[' Mp4_path '). $this->options[' uniqid ').            '. mp4 ';//record video playback address $result [' uniqid ']= $this->options[' uniqid '];            $result [' videotime '] = $this->video_info ($orgFile);            $result [' videotime '] = $result [' videotime '] [' seconds ']; return $result;//returns processing result} else die (' validation failed!    '); }}?>

Reference

Http://www.fieryrain.com/blog/FFMPEG_VIDEO_TIME

http://keren.iteye.com/blog/1773536

Http://www.cnblogs.com/dwdxdy/p/3240167.html

Http://www.cnblogs.com/chen1987lei/archive/2010/12/03/1895242.html

The above describes a ffmpeg video conversion PHP class, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.