【发布时间】:2012-01-29 06:46:15
【问题描述】:
我正在使用 PHP 启动 FFMPEG 转码命令。这是在 Ubuntu 10.04 服务器上运行的典型 LAMP 设置,配置没有什么特别之处。
我遇到的问题是,当我运行 PHP Exec 命令时,它返回了错误的进程 ID。它没有返回正确的 PID,而是返回高 2 的 PID。即 FFMPEG 进程 ID 为 3557,但 PHP Exec 返回 3559。
我一直设法每次都制作这个。问题是,我确实需要正确的进程 ID,以便稍后我可以选择停止该进程。
FFMPEG 命令可以正常工作并且启动时没有问题,所以我认为这不是问题的根源:
$cmd = "ffmpeg -r 4 -f mjpeg -an -i 'http://" . $internalhost . ":" . $stream_port . "'
-vcodec libx264 -vpre fastfirstpass -vpre baseline -b 24k -bt 32k -threads 0
http://localhost:8090/" . $localport . ".ffm";
$ffmpg = new Process($cmd);
我知道...它很丑,但它有效而且我不认为变量很重要。
要启动代码,我使用的是 PHP 手册网站上的一个类,如下所示:
class Process
{
private $pid;
private $command;
public function __construct($cl=false){
if ($cl != false){
$this->command = $cl;
$this->runCom();
}
}
private function runCom(){
$command = 'nohup '.$this->command.' > /dev/null 2> /dev/null & echo $!';
exec($command ,$op);
$this->pid = (int)$op[0];
}
public function setPid($pid){
$this->pid = $pid;
}
public function getPid(){
return $this->pid;
}
public function status(){
$command = 'ps -p '.$this->pid;
exec($command,$op);
if (!isset($op[1]))return false;
else return true;
}
public function start(){
if ($this->command != '')$this->runCom();
else return true;
}
public function stop(){
$command = 'kill '.$this->pid;
exec($command);
if ($this->status() == false)return true;
else return false;
}
}
我猜正在发生的事情是,由于某种原因,FFMPEG 正在启动另外 2 个进程,而不是返回主进程的 PID,而是返回最后一个进程的 PID。
虽然可能是错的,但我仍在摸不着头脑。
【问题讨论】: