yumyumyum
yumyumyum

Reputation: 11

Using subprocess on Popen option

I use python 2.7 and ffmpeg on Mac OS 10.10. When use some command from python, coded like below

p = subprocess.Popen(cmd.strip().split(" "))

This goes well all the time, but not in ffmpeg filter_complex case. Below code can run on the terminal with directly input.

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex " nullsrc=size=1920x1440 [base]; [0:v] setpts=PTS-STARTPTS, scale=1920x1440 [frame0];[1:v] setpts=PTS-STARTPTS, scale=640x480 [frame1]; [base] [frame0] overlay=shortest=1:x=1920:y=1440 [tmp1]; [tmp1] [frame1] overlay=shortest=1:x=640:y=480  output.mp4

But cannot run with python Popen script like below.

cmd = "ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex " nullsrc=size=1920x1440 [base]; [0:v] setpts=PTS-STARTPTS, scale=1920x1440 [frame0];[1:v] setpts=PTS-STARTPTS, scale=640x480 [frame1]; [base] [frame0] overlay=shortest=1:x=1920:y=1440 [tmp1]; [tmp1] [frame1] overlay=shortest=1:x=640:y=480  output.mp4"
p = subprocess.Popen(cmd.strip().split(" "))

This has problem at split(" ") works unexpected subdivision. So I prepare the split command directly like

split_cmd = [ffmpeg_exe,'-i','input1.mp4','-i','input2.mp4','-filter_complex','\" nullsrc=size=1920x1440 [base]; [0:v] setpts=PTS-STARTPTS, scale=1920x1440 [frame0];[1:v] setpts=PTS-STARTPTS, scale=640x480 [frame1]; [base] [frame0] overlay=shortest=1:x=1920:y=1440 [tmp1]; [tmp1] [frame1] overlay=shortest=1:x=640:y=480\"','output.mp4'];
p = subprocess.Popen(split_cmd)

Even in this case, system return

[AVFilterGraph @ 0x7fb1c9f000c0] No such filter: '" nullsrc' 
Error configuring filters.

this could be system confused the -filter_complex option as ffmpeg option. Anyone can help this, please.

Upvotes: 1

Views: 440

Answers (1)

MisterMiyagi
MisterMiyagi

Reputation: 50076

You are actually passing in the opening and closing double-quote ", since there is no shell in-between that uses them. Use just

' nullsrc=size=1920x1440 [base]; [0:v] setpts=PTS-STARTPTS, scale=1920x1440 [frame0];[1:v] setpts=PTS-STARTPTS, scale=640x480 [frame1]; [base] [frame0] overlay=shortest=1:x=1920:y=1440 [tmp1]; [tmp1] [frame1] overlay=shortest=1:x=640:y=480'

instead, i.e. without the opening and closing \".

split_cmd = [ffmpeg_exe,'-i','input1.mp4','-i','input2.mp4','-filter_complex',' nullsrc=size=1920x1440 [base]; [0:v] setpts=PTS-STARTPTS, scale=1920x1440 [frame0];[1:v] setpts=PTS-STARTPTS, scale=640x480 [frame1]; [base] [frame0] overlay=shortest=1:x=1920:y=1440 [tmp1]; [tmp1] [frame1] overlay=shortest=1:x=640:y=480','output.mp4']

You can also try using shlex.split, as described in the subprocess documentation.

Upvotes: 2

Related Questions