Answer the question
In order to leave comments, you need to log in
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 -")
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question