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 subprocess.call ()
I have tried the following with no success:
subprocess.call('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
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 subprocess.call( 'ffmpeg -r 10 -i frame%03d.png -r ntsc movie%03d.mpg' % (my_frame, my_frame), shell=True)
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
subprocess.call(['ffmpeg', '-r', '10', '-i', 'frame%03d.png' % my_frame,
['-r', 'ntsc', 'movie%03d.mpg' % my_frame])
More typing, but safer.
source to share
'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.
source to share