I'm trying to create a progress bar with FFMPEG using php and AJAX. When a user uploads a video file I want to be able to display the current percent until completion. I have managed to get everything working but I have one issue.
The data returns what I want, but it also returns all the data from the previous iterations... like it just keeps stacking everything on top rather than flushing out the data from previous iterations. I tried to work with tail
thinking it would return only the last line, but it did not return anything.
Here is the code I'm working with:
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);
The above code returns:
// 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
As you can see the data stacks, I need only the new line of data, not the data stacking.
** EDIT ** this has been marked as a duplicate, rather than me explain how it's different, I'd like to hear how this is the same thing? I am not writing to a log file and, like most people, do not feel that's a good solution.