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

python, subprocess

Solution

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()

Problem

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. I cant however make this work from python with `subprocess.Popen()`. Anyone have an idea how one passes quotes into subprocess.Popen? Here is the code: ``` 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() ``` When I do this, ffmpeg won't take it any other way other than quotes around the concat segement. Is there a way to successfully pass this line to subprocess.Popen command?

Original source

Related problems