17 05 2017
  1. <?php  
  2. class local_video  
  3. {  
  4.     public $options;  
  5.     public $ffmpeg;  
  6.     public $phpcms_path;  
  7.     public $backup;  
  8.     function __construct($options,$ffmpeg,$backup=true)  
  9.     {  
  10.         $this->options=pc_base::load_config('convert');  
  11.         $this->options = array_filter($options) + $this->options;  
  12.         $this->ffmpeg=$ffmpeg;   //ffmpeg路径  
  13.         $this->backup=$backup;  
  14.     }  
  15.     //获取视频信息  
  16.     function video_info($file)  
  17.     {  
  18.         ob_start();  
  19.         passthru(sprintf($this->ffmpeg . ' -i "%s" 2>&1'$file));//ffmpeg -i test.avi 2>&1  
  20.         $info = ob_get_contents();  
  21.         ob_end_clean();  
  22.         // 通过使用输出缓冲,获取到ffmpeg所有输出的内容。  
  23.         $ret  = array();  
  24.         // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s  
  25.         if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/",$info$match))  
  26.         {  
  27.             $ret['duration'] = $match[1]; // 提取出播放时间  
  28.             $da              = explode(':'$match[1]);  
  29.             $ret['seconds']  = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 转换为秒  
  30.             $ret['start']    = $match[2]; // 开始时间  
  31.             $ret['bitrate']  = $match[3]; // bitrate 码率 单位 kb  
  32.         }  
  33.         // Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc  
  34.         if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/"$info$match))  
  35.         {  
  36.             $ret['vcodec']     = $match[1]; // 编码格式  
  37.             $ret['vformat']    = $match[2]; // 视频格式  
  38.             $ret['resolution'] = $match[3]; // 分辨率  
  39.             $a                 = explode('x'$match[3]);  
  40.             $ret['width']      = $a[0];  
  41.             $ret['height']     = $a[1];  
  42.         }  
  43.         // Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s  
  44.         if (preg_match("/Audio: (\w*), (\d*) Hz/"$info$match))  
  45.         {  
  46.             $ret['acodec']      = $match[1];       // 音频编码  
  47.             $ret['asamplerate'] = $match[2];  // 音频采样频率  
  48.         }  
  49.         if (isset($ret['seconds']) && isset($ret['start']))  
  50.         {  
  51.             $ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间  
  52.         }  
  53.         $ret['size'] = filesize($file); // 文件大小  
  54.         return $ret;  
  55.     }  
  56.     function convert()  
  57.     {  
  58.         $verifyToken = md5('unique_salt' . $this->options['timestamp']);  
  59.         if ($verifyToken == $this->options['token'])  
  60.         {  
  61.             $orgFile  = $this->options['org_path'] . $this->options['org'];  
  62.             $setting=' ';  
  63.             if(isset($this->options['video_size']))  
  64.                 $setting=$setting.'-vf scale="'.$this->options['video_size'].'"';  
  65.             $mp4      = $this->ffmpeg . ' -i  '  . $orgFile . ' -ss 00:01:02 -vcodec libx264 -strict -2 '.$setting.' ' . $this->options['mp4_path_temp'] . $this->options['org'] . ''//转换视频  
  66.             exec($mp4);  
  67.             if(isset($this->options['watermark'])){  
  68.                 $watermark=' "'.$this->options['watermark'].'" ';  
  69.                 $mp4 = $this->ffmpeg.' -i '.$this->options['mp4_path_temp'] . $this->options['org'] . ''.' -vf '.$watermark.' '.$this->options['mp4_path'] . $this->options['org'] . ''//增加水印  
  70.                 exec($mp4);  
  71.                 @unlink($this->options['mp4_path_temp'] . $this->options['org'] . '');  
  72.             }  
  73.             $duration = $this->video_info($orgFile);  
  74.             $seconds  = intval($duration['seconds']);  
  75.             $offset   = intval($seconds / 21);  //截图间隔 秒  
  76.             $thumbs = explode(','$this->options['thumb_size']);  
  77.             for ($i = 0; $i <= count($thumbs); $i++)  
  78.             {  
  79.                 $targetPath = $this->options['thumb_path'] . $this->options['org'] . '/';  
  80.                 if (!file_exists($targetPath))  
  81.                     @mkdir(rtrim($targetPath'/'), 0777);  
  82.                 if ($i == 0)  
  83.                 {  
  84.                     $time     = 1;  
  85.                     $name     = $i == 0 ? 'default.jpg' : $i . '.jpg';  
  86.                     $img_size = $this->options['main_size'];  
  87.                 }  
  88.                 else  
  89.                 {  
  90.                     $time       = $i * $offset;  
  91.                     $name       = $i . '.jpg';  
  92.                     $img_size   = $thumbs[$i-1];  
  93.                 }  
  94.                 $jpg = $this->ffmpeg . ' -i  ' . $orgFile . ' -f  image2  -ss ' . $time . ' -vframes 1  -s ' . $img_size . ' ' . $targetPath . $name;    //截图  
  95.                 @exec($jpg);  
  96.             }  
  97.             //复制文件到对应的FTP服务器  
  98.             $ftp_server    = pc_base::load_config('ftp_server');  
  99.             $remote_server = $_POST['remote_server'];  
  100.             $ftp_server    = $ftp_server[$remote_server];  
  101.             if($ftp_server['ftp_server'])  
  102.                 pc_base::ftp_upload($orgFile,  
  103.                                 $ftp_server['ftp_server'],  
  104.                                 $ftp_server['ftp_user_name'],  
  105.                                 $ftp_server['ftp_user_pass']);  
  106.             //备份到所有FTP服务器  
  107.             if($this->backup){  
  108.                 $ftp_backup = pc_base::load_config('ftp_backup');  
  109.                 foreach ($ftp_backup as $v)  
  110.                 {  
  111.                     pc_base::ftp_upload( $orgFile,  
  112.                                         $ftp_backup['ftp_server'],  
  113.                                         $ftp_backup['ftp_user_name'],  
  114.                                         $ftp_backup['ftp_user_pass']);  
  115.                 }  
  116.             }  
  117.             $result['url']=$ftp_server['ftp_server']['http_address']. $this->options['mp4_path'] . $this->options['uniqid'] . '.mp4';//记录视频播放地址  
  118.             $result['uniqid']=$this->options['uniqid'];  
  119.             $result['videoTime'] = $this->video_info($orgFile);  
  120.             $result['videoTime'] = $result['videoTime']['seconds'];  
  121.             return $result;//返回处理结果  
  122.         }  
  123.         else  
  124.             die('验证失败!');  
  125.     }  
  126. }  
  127. ?>  
发表评论