0

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.

Mike
  • 809
  • 1
  • 10
  • 23
  • Possible duplicate of [what is the good function php to deal with ffmpeg and progress bar](https://stackoverflow.com/questions/19161325/what-is-the-good-function-php-to-deal-with-ffmpeg-and-progress-bar) – Raymond Nijland May 29 '17 at 14:56
  • I read that previously, but it didn't appear to work like what I'm trying to achieve since they are writing to a text file. I'll take another look. – Mike May 29 '17 at 15:00

1 Answers1

0

You need clean buffer before flush method

ob_clean

the_hasanov
  • 782
  • 7
  • 15