Passing double quote shell commands in python to subprocess.Popen()?

前端 未结 5 425
自闭症患者
自闭症患者 2020-11-29 05:34

I\'ve been trying to pass a command that works only with literal double quotes in the commandline around the "concat:file1|file2" argument for ffmpeg.

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 06:19

    I'd suggest using the list form of invocation rather than the quoted string version:

    command = ["ffmpeg", "-i", "concat:1.ts|2.ts", "-vcodec", "copy",
               "-acodec", "copy", "temp.mp4"]
    output,error  = subprocess.Popen(
                        command, universal_newlines=True,
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    

    This more accurately represents the exact set of parameters that are going to be passed to the end process and eliminates the need to mess around with shell quoting.

    That said, if you absolutely want to use the plain string version, just use different quotes (and shell=True):

    command = 'ffmpeg -i "concat:1.ts|2.ts" -vcodec copy -acodec copy temp.mp4'
    output,error  = subprocess.Popen(
                        command, universal_newlines=True, shell=True,
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    

提交回复
热议问题