·您当前的位置:首页 > 技术教程 > FFmpeg技术 >

[FFmpeg]php结合ffmpeg转换视频并抓取截图的类

时间:2014-07-28 22:47酷播
在最近的一个小项目中用到了php结合ffmpeg转换视频,同时抓取截图的类,因为项目比较小,没有考虑太多东西,而且只是在WINDOWS下测试成功,不过也发布出来

在最近的一个小项目中用到了php结合ffmpeg转换视频,同时抓取截图的类,因为项目比较小,没有考虑太多东西,而且只是在WINDOWS下测试成功,不过也发布出来。附一个简单的php结合ffmpeg转换视频 demo

  1. <?php 
  2. class movie2flv { 
  3.     var $cmd = '%1$s -i %2$s -b 360 -r 25 -s %4$dx%5$d -hq -deinterlace  -ab 56 -ar 22050 -ac 1  %3$s 2>>%6$s'; 
  4.     var $piccmd = '%1$s -i %2$s -y -f image2 -ss 1.2 -s %3$dx%4$d %5$s'; 
  5.     var $ffmpeg = ''; 
  6.     var $moviesrc = ''; 
  7.     var $flv = ''; 
  8.     var $flvwidth = '320'; 
  9.     var $flvheight = '240'; 
  10.     var $pic = ''; 
  11.     var $picwidth = '120'; 
  12.     var $picheight = '90'; 
  13.     var $logfile; 
  14.     var $code = ''; 
  15.  
  16. function __construct($arg = array()) { 
  17.     $this->ffmpeg = $arg['ffmpeg'] ? $arg['ffmpeg'] : $this->ffmpeg; 
  18.     $this->moviesrc = $arg['moviesrc']; 
  19.     
  20.     $this->flv = $arg['flv']; 
  21.     $this->flvwidth = $arg['flvwidth'] ? $arg['flvwidth'] : $this->flvwidth; 
  22.     $this->flvwidth = $arg['flvheight'] ? $arg['flvheight'] : $this->flvheight; 
  23.     
  24.     $this->pic = $arg['pic']; 
  25.     $this->picwidth = $arg['picwidth'] ? $arg['picwidth'] : $this->picwidth; 
  26.     $this->picwidth = $arg['picheight'] ? $arg['picheight'] : $this->picheight; 
  27.     $this->logfile = $arg['logfile'] ? $arg['logfile'] : $this->logfile; 
  28. } 
  29.  
  30. function getmoviecmd() { 
  31.     return sprintf($this->cmd,$this->ffmpeg,$this->moviesrc,$this->flv,$this->flvwidth,$this->flvheight,$this->logfile); 
  32. } 
  33.  
  34. function getpiccmd() { 
  35.     return sprintf($this->piccmd,$this->ffmpeg,$this->moviesrc,$this->picwidth,$this->picheight,$this->pic); 
  36. } 
  37.  
  38. function run($cmd) { 
  39.    $descriptorspec = array( 
  40.        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from 
  41.        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to 
  42.        2 => array("pipe", "w") // stderr is a file to write to 
  43.    ); 
  44.  
  45.    $pipes= array(); 
  46.    $process = proc_open($cmd, $descriptorspec, $pipes); 
  47.  
  48.    $output= ""; 
  49.  
  50.    if (!is_resource($process)) return false; 
  51.  
  52.    #close child's input imidiately 
  53.    fclose($pipes[0]); 
  54.  
  55.    stream_set_blocking($pipes[1],false); 
  56.    stream_set_blocking($pipes[2],false); 
  57.  
  58.    $todo= array($pipes[1],$pipes[2]); 
  59.  
  60.    while( true ) { 
  61.        $read= array(); 
  62.        if( !feof($pipes[1]) ) $read[]= $pipes[1]; 
  63.        if( !feof($pipes[2]) ) $read[]= $pipes[2]; 
  64.  
  65.        if (!$read) break; 
  66.  
  67.        $ready= stream_select($read, $write=NULL, $ex= NULL, 2); 
  68.  
  69.        if ($ready === false) { 
  70.            break; #should never happen - something died 
  71.        } 
  72.  
  73.        foreach ($read as $r) { 
  74.            $s= fread($r,1024); 
  75.            $output.= $s; 
  76.        } 
  77.    } 
  78.  
  79.    fclose($pipes[1]); 
  80.    fclose($pipes[2]); 
  81.  
  82.    $this->code = proc_close($process); 
  83.  
  84.    return $output; 
  85. } 
  86. } 
  87.  
  88. function __destruct() { 
  89.     
  90. } 
  91. ?> 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器