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 ffmpeg -i file.mp4 file.avi
and get the output as it streams so I can create a progress bar.
I've looked at these questions: Can ffmpeg show a progress bar? catching stdout in realtime from subprocess
I'm able to see the output of a rsync command, using this 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>>> " + str(line.rstrip()))
p.stdout.flush()
But when I change the command to ffmpeg -i file.mp4 file.avi
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
frame= 51 fps= 27 q=31.0 Lsize= 769kB time=2.04 bitrate=3092.8kbits/s
Which I could use to figure out progress.
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)
EDIT: I ended up going with jlp's solution, my code looked like this:
#!/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
Which gives this output:
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
Perfect!
The only way I've found to get dynamic feedback/output from a child process is to use something like pexpect:
the called sub process foo.sh just waits a random amount of time between 10 and 20 seconds, here's the code for it:
You'll want to use some regular expression that matches the output you're getting from ffmpeg and does some kind of calculation on it to show the progress bar, but this will at least get you the unbuffered output from ffmpeg.
stderr
, notstdout
.If all you want to do is print the output line, like in your example above, then simply this will do:
Note that the line of ffmpeg chat is terminated with
\r
, so it will overwrite in the same line! I think this means you can't iterate over the lines inp.stderr
, as you do with your rsync example. To build your own progress bar, then, you may need to handle the reading yourself, this should get you started:In this specific case for capturing ffmpeg's status output (which goes to STDERR), this SO question solved it for me: FFMPEG and Pythons subprocess
The trick is to add
universal_newlines=True
to thesubprocess.Popen()
call, because ffmpeg's output is in fact unbuffered but comes with newline-characters.Also note that in this code sample the STDERR status output is directly redirected to
subprocess.STDOUT
You can also do it pretty clearly with PyQt4's QProcess (as asked in the original question) by connecting a slot from the QProcess to a QTextEdit or whatever. I'm still pretty new to python and pyqt but here's how I just managed to do it:
This answers didn't worked for me :/ Here is the way I did it.
Its from my project KoalaBeatzHunter.
Enjoy!
Next, you need 3 more functions to implement it.
And finally you do:
Hope this will help!
If you have the duration (Which you can also get from the FFMPEG output) you can calculate the progress by reading the elapsed time (time) output when encoding.
A simple example: