executing a subprocess from python

Question:

I think something is getting subtly mangeled when I attempt to execute a subprocess from a python script

I attempt to execute vlc with some (a lot) of arguments.

the instance of vlc that arises complains:

Your input can’t be opened:
VLC is unable to open the MRL ‘ -vvv rtsp://192.168.1.201:554/ch0_multicast_one –sout=#transcode{acodec=none}:duplicate{dst=rtp{sdp=rtsp://:5544/user_hash.sdp},dst=display} :no-sout-rtp-sap :no-sout-standard-sap :ttl=1 :sout-keep’. Check the log for details.

Here is the python code

pid = subprocess.Popen(["vlc "," -vvv rtsp://%s" % target_nvc.ip_address + ":554/ch0_multicast_one --sout=#transcode{acodec=none}:duplicate{dst=rtp{sdp=rtsp://:5544/user_hash.sdp},dst=display} :no-sout-rtp-sap :no-sout-standard-sap :ttl=1 :sout-keep" ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

I have examined the output of the subprocess function (using a shell), and if I copy paste that string into my cmd window, the vlc instance works fine… Is this a privilege thing?

Asked By: michael

||

Answers:

Since you’re passing a list to subprocess.Popen, each parameter must be in its own element. So you’d want something like:

pid = subprocess.Popen([
    "vlc",
    "-vvv",
    "rtsp://%s:554/ch0_multicast_one" % target_nvc.ip_address,
    # etc
], ...)

Each parameter (that the shell would normally parse apart for you) must be in a separate list element.

You can also pass a single command line string and let the shell pull it apart:

pid = subprocess.Popen("vlc -vvv rtsp://...", shell=True, ...)

Using the first form is better for commands that have lots of arguments.

Answered By: Greg Hewgill

You should use this…

pid = subprocess.Popen(["vlc", "-vvv", 
    "rtsp://%s" % target_nvc.ip_address + ":554/ch0_multicast_one", 
    "--sout=#transcode{acodec=none}:duplicate{dst=rtp{sdp=rtsp://:5544/user_hash.sdp},dst=display}", 
    ":no-sout-rtp-sap", ":no-sout-standard-sap", 
    ":ttl=1", ":sout-keep" ], stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE, stdin=subprocess.PIPE)
Answered By: Mike Pennington
movies_path = glob.glob("D:\MOVIES*****.mp4", recursive=True) + 
              glob.glob("D:\MOVIES*****.mkv", recursive=True) + 
              glob.glob("D:\MOVIES*****.avi", recursive=True)

# probably the right movie
rightMoviePath = difflib.get_close_matches(which_movie, movies_path, len(movies_path), 0)
movie_name = rightMoviePath[0].split("\")[-1]
hebrew_subtitle_path = glob.glob(rightMoviePath[0].replace(movie_name, "Hebrew.srt"))[0]
english_subtitle_path = glob.glob(rightMoviePath[0].replace(movie_name, "English.srt"))[0]

process, player = subprocess.Popen(["C:\Users\yonat\Downloads\VLC\vlc.exe", "--sub-file", hebrew_subtitle_path, rightMoviePath[0]],
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Answered By: jonathanZilca
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.