【问题标题】:How do I get the last line of a popen() callback in every iteration?如何在每次迭代中获取 popen() 回调的最后一行?
【发布时间】:2017-05-29 14:47:04
【问题描述】:

我正在尝试使用 php 和 AJAX 创建带有 FFMPEG 的进度条。当用户上传视频文件时,我希望能够显示当前百分比直到完成。我设法让一切正常,但我有一个问题。

数据返回我想要的,但它也返回之前迭代的所有数据......就像它只是将所有内容堆叠在顶部,而不是从之前的迭代中清除数据。我尝试与 tail 合作,认为它只会返回最后一行,但它没有返回任何内容。

这是我正在使用的代码:

encode.php

$video_path = 'test.mp4';
$cmd        = 'ffmpeg -i ' . $video_path .' -y -hide_banner output.mp4 2>&1';

while (@ ob_end_flush());

$proc = popen($cmd, 'r');

while (!feof($proc))
{

    $file = escapeshellarg(fread($proc, 4096));
    //$line = `tail -n 1 $file`; // <-tried this with no luck
    echo fread($file, 4096) . "\n";
    @ flush();
}
return 'complete';
pclose($proc);

以上代码返回:

// first iteration
frame=   52 fps= 13 q=29.0 size=     279kB time=00:00:00.10 bitrate=22856.9kbits/s

// second iteration
frame=   52 fps= 13 q=29.0 size=     279kB time=00:00:00.10 bitrate=22856.9kbits/s    
frame=   54 fps= 12 q=29.0 size=     329kB time=00:00:00.16 bitrate=16146.6kbits/s 

// third iteration
frame=   52 fps= 13 q=29.0 size=     279kB time=00:00:00.10 bitrate=22856.9kbits/s    
frame=   54 fps= 12 q=29.0 size=     329kB time=00:00:00.16 bitrate=16146.6kbits/s    
frame=   57 fps= 11 q=29.0 size=     464kB time=00:00:00.26 bitrate=14233.2kbits/s 

正如你所看到的数据堆栈,我只需要新的数据行,而不是数据堆栈。

** 编辑 ** 这已被标记为重复,而不是我解释它有何不同,我想听听这是同一件事吗?我没有写入日志文件,并且像大多数人一样,不认为这是一个好的解决方案。

【问题讨论】:

标签: php laravel ffmpeg


【解决方案1】:

在刷新方法之前需要清理缓冲区

ob_clean

【讨论】:

  • 我尝试了你的建议,我得到了ob_clean(): failed to delete buffer. No buffer to delete
  • echo fread($file, 4096) . "\n"; @ flush(); ob_clean();
  • 感谢您的帮助,但我收到了同样的错误消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2017-10-19
  • 1970-01-01
  • 2015-03-21
相关资源
最近更新 更多