title: Getting realtime output from ffmpeg to be used in progress bar (PyQt4, stdout)
body: <p>I've looked at a number of questions but still can't quite figure this out.  I'm using PyQt, and am hoping to run <code>ffmpeg -i file.mp4 file.avi</code> and get the output as it streams so I can create a progress bar.</p>
  
  <p>I've looked at these questions:
  <a href="https://stackoverflow.com/questions/747982/can-ffmpeg-show-a-progress-bar">Can ffmpeg show a progress bar?</a>
  <a href="https://stackoverflow.com/questions/1606795/catching-stdout-in-realtime-from-subprocess">catching stdout in realtime from subprocess</a></p>
  
  <p>I'm able to see the output of a rsync command, using this code:</p>
  
  <pre><code>import subprocess, time, os, sys
  
  cmd = "rsync -vaz -P source/ dest/"
  p, line = True, 'start'
  
  
  p = subprocess.Popen(cmd,
                       shell=True,
                       bufsize=64,
                       stdin=subprocess.PIPE,
                       stderr=subprocess.PIPE,
                       stdout=subprocess.PIPE)
  
  for line in p.stdout:
      print("OUTPUT&gt;&gt;&gt; " + str(line.rstrip()))
      p.stdout.flush()
  </code></pre>
  
  <p>But when I change the command to <code>ffmpeg -i file.mp4 file.avi</code> I receive no output.  I'm guessing this has something to do with stdout / output buffering, but I'm stuck as to how to read the line that looks like </p>
  
  <pre><code>frame=   51 fps= 27 q=31.0 Lsize=     769kB time=2.04 bitrate=3092.8kbits/s
  </code></pre>
  
  <p>Which I could use to figure out progress.  </p>
  
  <p>Can someone show me an example of how to get this info from ffmpeg into python, with or without the use of PyQt (if possible)</p>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  I ended up going with jlp's solution, my code looked like this:</p>
  
  <pre><code>#!/usr/bin/python
  import pexpect
  
  cmd = 'ffmpeg -i file.MTS file.avi'
  thread = pexpect.spawn(cmd)
  print "started %s" % cmd
  cpl = thread.compile_pattern_list([
      pexpect.EOF,
      "frame= *\d+",
      '(.+)'
  ])
  while True:
      i = thread.expect_list(cpl, timeout=None)
      if i == 0: # EOF
          print "the sub process exited"
          break
      elif i == 1:
          frame_number = thread.match.group(0)
          print frame_number
          thread.close
      elif i == 2:
          #unknown_line = thread.match.group(0)
          #print unknown_line
          pass
  </code></pre>
  
  <p>Which gives this output:</p>
  
  <pre><code>started ffmpeg -i file.MTS file.avi
  frame=   13
  frame=   31
  frame=   48
  frame=   64
  frame=   80
  frame=   97
  frame=  115
  frame=  133
  frame=  152
  frame=  170
  frame=  188
  frame=  205
  frame=  220
  frame=  226
  the sub process exited
  </code></pre>
  
  <p>Perfect!</p>