在最近的一个小项目中用到了php结合ffmpeg转换视频,同时抓取截图的类,因为项目比较小,没有考虑太多东西,而且只是在WINDOWS下测试成功,不过也发布出来。附一个简单的php结合ffmpeg转换视频 demo
下载地址 http://www.dayanmei.com/download.php?filename=phpffmpeg.rar
<?php
class movie2flv {
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';
var $piccmd = '%1$s -i %2$s -y -f image2 -ss 1.2 -s %3$dx%4$d %5$s';
var $ffmpeg = '';
var $moviesrc = '';
var $flv = '';
var $flvwidth = '320';
var $flvheight = '240';
var $pic = '';
var $picwidth = '120';
var $picheight = '90';
var $logfile;
var $code = '';
function __construct($arg = array()) {
$this->ffmpeg = $arg['ffmpeg'] ? $arg['ffmpeg'] : $this->ffmpeg;
$this->moviesrc = $arg['moviesrc'];
$this->flv = $arg['flv'];
$this->flvwidth = $arg['flvwidth'] ? $arg['flvwidth'] : $this->flvwidth;
$this->flvwidth = $arg['flvheight'] ? $arg['flvheight'] : $this->flvheight;
$this->pic = $arg['pic'];
$this->picwidth = $arg['picwidth'] ? $arg['picwidth'] : $this->picwidth;
$this->picwidth = $arg['picheight'] ? $arg['picheight'] : $this->picheight;
$this->logfile = $arg['logfile'] ? $arg['logfile'] : $this->logfile;
}
function getmoviecmd() {
return sprintf($this->cmd,$this->ffmpeg,$this->moviesrc,$this->flv,$this->flvwidth,$this->flvheight,$this->logfile);
}
function getpiccmd() {
return sprintf($this->piccmd,$this->ffmpeg,$this->moviesrc,$this->picwidth,$this->picheight,$this->pic);
}
function run($cmd) {
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$pipes= array();
$process = proc_open($cmd, $descriptorspec, $pipes);
$output= "";
if (!is_resource($process)) return false;
#close child's input imidiately
fclose($pipes[0]);
stream_set_blocking($pipes[1],false);
stream_set_blocking($pipes[2],false);
$todo= array($pipes[1],$pipes[2]);
while( true ) {
$read= array();
if( !feof($pipes[1]) ) $read[]= $pipes[1];
if( !feof($pipes[2]) ) $read[]= $pipes[2];
if (!$read) break;
$ready= stream_select($read, $write=NULL, $ex= NULL, 2);
if ($ready === false) {
break; #should never happen - something died
}
foreach ($read as $r) {
$s= fread($r,1024);
$output.= $s;
}
}
fclose($pipes[1]);
fclose($pipes[2]);
$this->code = proc_close($process);
return $output;
}
}
function __destruct() {
}
?>