title: How do I get the last line of a popen() callback in every iteration?
body: <p>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. </p>
  
  <p>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 <code>tail</code> thinking it would return only the last line, but it did not return anything.</p>
  
  <p>Here is the code I'm working with:</p>
  
  <p><strong>encode.php</strong></p>
  
  <pre><code>$video_path = 'test.mp4';
  $cmd        = 'ffmpeg -i ' . $video_path .' -y -hide_banner output.mp4 2&gt;&amp;1';
  
  while (@ ob_end_flush());
  
  $proc = popen($cmd, 'r');
  
  while (!feof($proc))
  {
  
      $file = escapeshellarg(fread($proc, 4096));
      //$line = `tail -n 1 $file`; // &lt;-tried this with no luck
      echo fread($file, 4096) . "\n";
      @ flush();
  }
  return 'complete';
  pclose($proc);
  </code></pre>
  
  <p>The above code returns:</p>
  
  <pre><code>// 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 
  </code></pre>
  
  <p>As you can see the data stacks, I need only the new line of data, not the data stacking.</p>
  
  <p>** 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.</p>