subprocess.call() in Python

Status
Not open for further replies.

senmeis

Full Member level 3
Joined
Nov 26, 2014
Messages
167
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,521
Hi,

I want to use

Python:
subprocess.call(["ffmpeg", "-i", "test.mp4", "test.mp3"])

to make a media conversion.

Question: how can I redirect the console output so that this can be outputted with „print()“?
 

I tried the following two alternatives:



Alternative 1:

Python:
out = subprocess.run(["ffmpeg", "-i", "test.mp4", "test.mp3"], capture_output=True, text=True, shell=True)
print(out.stdout)



Alternative 2:

Python:
out = subprocess.check_output(["ffmpeg", "-i", "test.mp4", "test.mp3"], shell=True)
print(out.decode('utf-8'))


but both don’t print out anything. I just want to get the process info which comes from the console when ffmpeg is executed.
 

You can use the subprocess.PIPE to redirect the console output to your program. For example:

process = subprocess.Popen(["ffmpeg", "-i", "test.mp4", "test.mp3"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Code:
for line in iter(process.stdout.readline, b''):
    print(line.decode('utf-8').rstrip())
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…