Calling ffmpeg subprocess (command line)

I have included subprocess calls in my program. I had no problem with subprocess calls for other commands, but I am having a hard time getting command line input

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


To work inside ()

I have tried the following with no success:'ffmpeg -r 10 -i %s frame%03.d.png - r ntsc movie.mpg')


Any thoughts? Split individual commands, specify string, integer, etc. With the help of %s

, %d



source to share

4 answers

I found this alternative, simple answer to work.'ffmpeg -r 10 -i frame%03d.png -r ntsc '+str(out_movie), shell=True)




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

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

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


In this example, its thin subtle but so risky. Let's assume that these things were in a directory whose name had spaces (eg. / My Movies / Scary Movie). The shell will be confused by these spaces.

So you can put it on the list and avoid the problem

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


More typing, but safer.



import shlex
import pipes
from subprocess import check_call

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




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

should be good. OTOH, If you don't need power frame%03d.png

, it's a frame*.png

little easier.

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 avoid %

with the extra %

to hide it from the Python% replacement mechanism. I've also added double quotes "

to deal with the problems tdelaney mentioned.



All Articles