使用php脚本解析输出2>&1
我有一个 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") ;
在testing123.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 命令执行时,我才会获得一个数据库条目。 有没有办法继续执行文件或其他内容,以便每隔几秒钟将输入到文件的输出插入到我的数据库中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
while
循环。fgets
文档中有一些示例。文档中的评论之一建议使用
stream_get_line
而不是fgets
。如果您有兴趣,这里是直接链接: http://www .php.net/manual/en/function.fgets.php#86319。You need to use a
while
loop. There are some examples in thefgets
documentation.One of the comments in the documentation suggests using
stream_get_line
instead offgets
. In case you're interested, here's the direct link: http://www.php.net/manual/en/function.fgets.php#86319.