PHP 视频格式转换类

class video{
     
    /*******************
    使用本类需要有mencoder.exe视频格式转换器
    需要修改下面的绝对路径
    支持的格式 rmvb,wmv,mkv,3gp,mp4,mpg,avi,mp3
    该类需要结合两个视频转换软件 mencoder.exe 和 ffmpeg.exe
    *******************/
    private $mencoder_url = "D:\wamp\mplayer\mencoder.exe";
    private $ffmpeg_url = "D:\wamp\\ffmpeg\bin\\ffmpeg.exe";
    private $savePath = "D:\a";
    private $format = array("rmvb","wmv","mkv","3gp","mp4","mpg","avi",'mp3');
    public $err = '';
    function video(){
         
    }
     
    public function run($fromfile,$to='flv',$w=720,$h=480,$n=1500){
        //是否格式正确
        if($this->is_format($fromfile,$to)==false) { 
        	$this->err .= 'extension not in format';
        	echo 'extension not in format';
 			return false;
		}
		// echo 11;
        //获取视频截图
        $this->printscreen($fromfile,300,300);
        //获取视频时长
        // $b = $this->getTime($fromfile);
        $ext = $this->getExt($fromfile);
        echo  $ext;
        //转换格式
        if($ext=='rmvb' || $ext=='rm' || $ext=='mp4'){
        	// echo $ext;
            $this->convert($fromfile,$to,$w,$h,$n);
              
        }else{
            $this->convertF($fromfile,$to,$w,$h);
        }
    }
    //获取后缀名
    public function getExt($file){
        $r =  pathinfo($file);
        return $r['extension'];
    }
    public function convertF($fromfile,$to,$w,$h){
        $file = $this->ping($fromfile);
        $tofile = $this->pingTo($fromfile,$to);
        echo $file.'
'; $code = "{$this->ffmpeg_url} -i {$file} -ab 56 -ar 22050 -b 1500 -qscale 1 -r 20 -s {$w}x{$h} {$tofile}"; echo $code; echo exec($code,$e); $this->err = $e; } public function convert($fromfile,$to='flv',$w,$h,$n){ $file = $this->ping($fromfile); echo $file; $tofile = $this->pingTo($fromfile,$to); $ovc = 'lavc'; $code = "{$this->mencoder_url} {$file} -o {$tofile} -of lavf -oac mp3lame -lameopts abr:br=56 -ovc {$ovc} -lavcopts vcodec=flv:vbitrate={$n}:mbd=2:o=mpv_flags=+mv0:trell:v4mv:last_pred=1:dia=4:cmp=0:vb_strategy=1 -vf scale={$w}:{$h} -ofps 20 -srate 22050"; // $out=exec($code,$e); proc_close(proc_open ($code, array(), $e)); pclose(popen ($code,'r')); echo exec($code, $e); $this->err = $e; // echo $e; } //拼接完整来源地址 public function ping($fromFile){ $fromFile = str_replace('/','\\',$fromFile); // $path = $this->savePath.$fromFile; // echo $fromFile.'
'; return $fromFile; } //拼接完整的目标转换后的视频文件地址 public function pingTo($fromFile,$to){ $a = pathinfo($fromFile); $a['dirname'] = str_replace('/','\\',$a['dirname']); $path = $a['dirname'].'\\'.$a['filename'].'.'.$to; // echo $path.'
'; return $path; } //提取视频截图 public function printscreen($fromfile,$w=350,$h=240){ $file = $this->ping($fromfile); $toImg = $this->pingTo($fromfile,'jpg'); $code = "{$this->ffmpeg_url} -i {$file} -y -f image2 -ss 8 -t 0.001 -s {$w}x{$h} {$toImg}"; exec($code,$e); $this->err = $e; } //检查是否是其中格式 public function is_format($file,$to){ if(!file_exists($file)) return false; $a = pathinfo($file); //如果转换的格式和原格式相同 if($a['extension']==$to) return false; if(in_array($a['extension'],$this->format)) return true; else return false; } //提取视频时长 public function getTime($fromfile){ $file = $this->ping($fromfile); $code = "{$this->ffmpeg_url} -i {$file}"; exec($code,$e); $this->err = $e; } } $rr='./sun.3gp'; $v = new video(); $v->run($rr,'mp4',480,480);

你可能感兴趣的:(PHP)