11
votes

I have been incorporating subprocess calls in my program. I have had no issues with subprocess calls for other commands, but I am having trouble getting the command line input

ffmpeg -r 10 -i frame%03d.png -r ntsc movie.mpg

To work inside a subprocess.call()

I tried the following with no success:

subprocess.call('ffmpeg -r 10 -i %s frame%03.d.png - r ntsc movie.mpg')

Any thoughts? Do I separate out different commands, do I specify string, integer etc. with %s, %d?

4
Run your command, but with each argument split out in a list. If you do it as a single string then you will have to specify shell=True which you likely don't want to do anyway. So, assuming your command is built up as variable cmd, run subprocess.call(cmd.split()) and wait for the magic. - sberry
What is the error? shell=True may solve it. - tdelaney
Thanks, yes, shell=True lets me run the commands as one big string, but I was also curious to see the syntax for it if I replace 'movie.mpg' with a variable name. - user3295674
The command you want to run has frame%03d.png. The command you're running with subprocess has frame%03.d.png . It also has an extra %s that doesn't correspond to anything in your command. So… what exactly are you trying to do? What's the actual command line you want to execute? - abarnert
Unrelated, but you should use -framerate instead of -r when using the image2 demuxer since, IIRC, it is less picky if the inputs vary by frame size or pixel format. - llogan

4 Answers

20
votes

When you use subprocess, your command must either be a string that looks exactly like what you would type on the command line (and you set shell=True), or a list where each command is an item in the list (and you take the default shell=False). In either case, you have to deal with the variable part of the string. For instance, the operating system has no idea what "%03d" is, you have to fill it in.

I can't tell from your question exactly what the parameters are, but lets assume you want to convert frame 3, it would look something like this in a string:

my_frame = 3
subprocess.call(
    'ffmpeg -r 10 -i frame%03d.png -r ntsc movie%03d.mpg' % (my_frame, my_frame),
    shell=True)

Its kinda subtle in this example, but that's risky. Suppose these things were in a directory whose name name had spaces (e.g., ./My Movies/Scary Movie). The shell would be confused by those spaces.

So, you can put it into a list and avoid the problem

my_frame = 3
subprocess.call(['ffmpeg', '-r', '10', '-i', 'frame%03d.png' % my_frame,
    ['-r',  'ntsc', 'movie%03d.mpg' % my_frame])

More typing, but safer.

7
votes

I found this alternative, simple, answer to also work.

subprocess.call('ffmpeg -r 10 -i frame%03d.png -r ntsc '+str(out_movie), shell=True)
2
votes
import shlex
import pipes
from subprocess import check_call

command = 'ffmpeg -r 10 -i frame%03d.png -r ntsc ' + pipes.quote(out_movie)
check_call(shlex.split(command))
0
votes

'ffmpeg -r 10 -i frame%03d.png -r ntsc movie.mpg' should be fine. OTOH, If you don't need the power of frame%03d.png, frame*.png is a bit simpler.

If you want to "see the syntax for it if I replace 'movie.mpg' with a variable name", it looks something like this:

cmd = 'ffmpeg -r 10 -i "frame%%03d.png" -r ntsc "%s"' % moviename

We need to escape the % with an extra % to hide it from Python's % substitution machinery. I've also added double quotes " , to cope with the issues that tdelaney mentioned.