A
A
ANDRE8882021-09-12 15:41:16
Python
ANDRE888, 2021-09-12 15:41:16

How can I write the contents of stdout to a variable without waiting for a newline to be printed?

Task: In python, start a process, read its output and write it to a variable. I'll show you with ffmpeg as an example.
This is handled by the following code

import subprocess

def func(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while True:
        line = p.stdout.readline()
        print (line)
    
func("ffmpeg -i rtsp://admin:[email protected]:554//h264Preview_01_main -vn -filter:a silencedetect=n=-30dB:d=1 -f null -")


The output reads fine, right up to the point where ffmpeg ends its messages with a newline character \n.
At some point ffmpeg just starts overwriting the last line with \r and I don't get any data until it spits out a message with a newline character.
Obviously the problem is in readline().
Question: How can I write the contents of stdout to a variable without waiting for a newline character to be printed?
I tried to be smart with flush(), read(), communicate(), check_output(). But for me it all ends either with the program freezing, or the output is not written to a variable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-09-12
@ANDRE888

Because you read with .readline() which, by definition, expects the character '\n'
If you want another character, just use read and scan the buffer for \r yourself and collect the line
as an option, replace the output with something using pipes, for example tr , and so that it does not buffer the output, add stdbuf -o0, discussed here

ffmpeg -i input.mov output.webm 2>&1 | stdbuf -o0 tr '\r' '\n'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question