So I'm trying to capture the output of HandBrakeCLI as a Python subprocess. This is not a problem with stderr as each update creates a new line in the file-like pipe. However, with stdout, HandBrakeCLI makes in-place updates and I'm having difficulty capturing them. I'm not even ure what the in-place updates are called, which makes finding relevant tips a bit tricky.
The only solution I've come up with thus far is to write the stdout to an actual file and read from that, but I'd rather do this the sane way (in memory).
COMMAND = ['HandBrakeCLI', '-v', '-i', 'in.m4v', '-o', 'out.m4v', '-Z', 'Normal']
outfile = open('output.txt', 'w')
proc = subprocess.Popen(COMMAND, stdout=outfile, stderr=subprocess.PIPE)
infile = open('output.txt', 'r')
while proc.poll() is None:
print infile.read()
infile.seek(0)
This works, but there must be a better way. When attempting to use communicate() or just plain proc.stdout.read() I get nothing.
What am I doing wrong? Thanks!
Update
Per @wim suggestion I checked to see what the raw output HandBrakeCLI provided and it looked something like this:
\rEncoding: task 1 of 1, 0.15 %
What is the best way to handle a stdout that is prefixed with a \r?