So, I'm trying to grab 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 channel. However, with stdout, HandBrakeCLI makes in-place updates, and I'm having difficulty grabbing them. I donβt even assume what are called in-place updates, which makes it difficult to find the appropriate advice.
The only solution I came up with is to write stdout to the actual file and read it, but I would rather do it in a smart 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)
It works, but there should be a better way. When I try to use a connection () or just a simple proc.stdout.read (), I get nothing.
What am I doing wrong? Thanks!
Update
In the @wim view, which I checked to see what the original output provided by HandBrakeCLI, and it looked something like this:
\rEncoding: task 1 of 1, 0.15 %
What is the best way to handle stdout with the \ r prefix?
kolanos
source share