开发者

parse output 2>&1 with php script

开发者 https://www.devze.com 2023-03-15 21:25 出处:网络
I have an exec command I am using to make ffmpeg work. $process = exec(\"/usr/local/bin/ffmpeg -i /home/g/Desktop/cave.wmv -deinterlace

I have an exec command I am using to make ffmpeg work.

$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") ;

In testing123.php I use

$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));

However, as you may have guessed I get one database entry, only when the file initially executes from the exec command in the other file. Is there a way to keep executing the file or something else so that the output fed to the file can be inserted into my database every couple seconds?


You need to use a while loop. There are some examples in the fgets documentation.

$h = fopen('php://stdin', 'r');

while ( ($str = fgets($h)) !== false )
{
  $sql = 'INSERT INTO ...';

  mysql_query($sql);
}

fclose($h);

One of the comments in the documentation suggests using stream_get_line instead of fgets. In case you're interested, here's the direct link: http://www.php.net/manual/en/function.fgets.php#86319.

0

精彩评论

暂无评论...
验证码 换一张
取 消