Hello,
Apparently, subprocess is currently the solution
du jour to run an external application from Python 3.12.
For some reason, output is returned through stderr instead of stdout:
import subprocess
import sys
import os
os.chdir(r'c:\temp')
p = run( [ r'c:\ffprobe.exe','-hide_banner',r"input.mp4"], shell=True,capture_output = True,text = True, check=True)
#!!! outputs to stderr!!!
print( 'stdout:', p.stdout) #nothing
print( 'stderr:', p.stderr)
What am I doing wrong?
Thank you.
I would be surprised if Python knows anything about run. However subprocess.run is another story.
This causes your program to crash because run is not defined (as perfingo mentioned).
p = run( [ r'c:\ffprobe.exe','-hide_banner',r"input.mp4"], shell=True,capture_output = True,text = True, check=True)
Which means this is not running:
print( 'stderr:', p.stderr)
And the "stderr" you are seeing is the normal Python error handling. for your program, not from running a subprocess that calls ffprobe.
Why are you using shell=True?
it's not uncommon for some programs to use
stderr
(as ffprobe do) for informational messages or even regular output,
particularly if the
stdout
is used for different kinds of data.
So use it like this.
import subprocess
output = subprocess.run(['ffprobe', '-hide_banner', 'sample.mp4'],capture_output=True, text='utf-8', universal_newlines=True)
print(output.stderr)
Output:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'sample.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42mp41isomavc1
creation_time : 2015-08-07T09:13:02.000000Z
Duration: 00:00:30.53, start: 0.000000, bitrate: 411 kb/s
Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 480x270 [SAR 1:1 DAR 16:9], 301 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default)
Metadata:
creation_time : 2015-08-07T09:13:02.000000Z
handler_name : L-SMASH Video Handler
encoder : AVC Coding
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 112 kb/s (default)
Metadata:
creation_time : 2015-08-07T09:13:02.000000Z
handler_name : L-SMASH Audio Handler