解析输出2>&;1使用php脚本


parse output 2>&1 with php script

我有一个exec命令,用于使ffmpeg工作。

$process = exec("/usr/local/bin/ffmpeg -i /home/g/Desktop/cave.wmv -deinterlace 
-acodec libfaac -ab 96k -ar 44100 -vcodec libx264 -s 480x320 -f flv /home/g/Desktop
/file.flv 2>&1 | php testing123.php") ;

在测试123.php时,我使用

$h = fopen('php://stdin', 'r'); $str = fgets($h); fclose($h);
//topic removed
 $sql = 'INSERT INTO table
(id,status) VALUES(?,?)';
$stmt3 = $conn->prepare($sql);
$result=$stmt3->execute(array(rand(),$str));

但是,正如您可能已经猜到的那样,只有当文件最初从另一个文件中的exec命令执行时,我才会得到一个数据库条目是否有一种方法可以继续执行该文件或其他内容,以便每隔几秒钟将提供给该文件的输出插入我的数据库

您需要使用while循环。fgets文档中有一些示例。

$h = fopen('php://stdin', 'r');
while ( ($str = fgets($h)) !== false )
{
  $sql = 'INSERT INTO ...';
  mysql_query($sql);
}
fclose($h);

文档中的一条注释建议使用stream_get_line而不是fgets。如果您感兴趣,这里有直接链接:http://www.php.net/manual/en/function.fgets.php#86319.