15

Atm I have this as my code, the first line seems to work well but the 2nd gives errrors.

os.chdir('C://Users/Alex/Dropbox/code stuff/test')
subprocess.call('ffmpeg -i test%d0.png output.avi')

Also when I try to run it as this, it gives a 1s cmd flicker and then nothing happens

os.system('ffmpeg -i test%d0.png output.avi')
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
Coolcrab
  • 2,655
  • 9
  • 39
  • 59
  • 1
    At the top of the `commands` module is a big warning: *Deprecated since version 2.6: The `commands` module has been removed in Python 3. Use the `subprocess` module instead.* Heed it. – Martijn Pieters May 25 '13 at 09:28
  • 1
    Use `os.chdir()` and `subprocess.check_output(['ffmpeg', '-i', 'image%d0.png', 'output.avi'])` instead, or use `subprocess.call()` if you are more in the exit code and / or do not need the command output at all. – Martijn Pieters May 25 '13 at 09:31
  • Getting 'Traceback (most recent call last): File "", line 1, in subprocess.call('cd /Users/Alex') File "C:\Python27\lib\subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\subprocess.py", line 711, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 948, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified' When I use call – Coolcrab May 25 '13 at 09:37
  • Don't use `call` to change directories. Use `os.chdir()` instead. – Martijn Pieters May 25 '13 at 09:38
  • `os.chdir('C://Users/Alex/Dropbox/code stuff/test/') subprocess.call('ffmpeg -i image%d0.png output.avi')` Still doesn't seem to work, I think that it forgets the dir it is in? – Coolcrab May 25 '13 at 09:42
  • No, it doesn't forget such things. Something else is wrong for your case. – Martijn Pieters May 25 '13 at 09:44
  • `os.chdir('C://Users/Alex/Dropbox/code stuff/') os.system('ffmpeg -i image%d0.png output.avi')` this makes the cmd flicker on and off, but it doesn't do what it should. – Coolcrab May 25 '13 at 09:47
  • and you are right, the dir is working so its the 2nd line thats broken. – Coolcrab May 25 '13 at 09:49
  • I rolled back the question to its initial form, instead of the edited form which includes the correct usage (i.e. the answer below): thus making the question unclear. – DilithiumMatrix Dec 10 '20 at 16:28

5 Answers5

27

For the later generations looking for the answer, this worked. (You have to separate the command by the spaces.)

import os
import subprocess
os.chdir('C://Users/Alex/')
subprocess.call(['ffmpeg', '-i', 'picture%d0.png', 'output.avi'])
subprocess.call(['ffmpeg', '-i', 'output.avi', '-t', '5', 'out.gif'])
Tamil Selvan S
  • 562
  • 8
  • 25
Coolcrab
  • 2,655
  • 9
  • 39
  • 59
  • 2
    what space are you talking about? Inside `subprocess.call()` is a list. I see no spaces inside the strings `' space where ... ? '` – PatrickT May 29 '20 at 18:38
  • @PatrickT check the doc for subprocess.call, each list element is separated by a space in the final command. – Info5ek Jun 18 '20 at 12:55
10

It is better to call subprocess.call in another way.

The preferred way is:

subprocess.call(['ffmpeg', '-i', 'test%d0.png', 'output.avi'])

Alternatively:

subprocess.call('ffmpeg -i test%d0.png output.avi', shell=True)

You can find the reasons for this in the manual. I quote:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • I did the top thing and got back this error: `Traceback (most recent call last): File "C:\Users\Alex\Dropbox\code stuff\solarsystem.py", line 56, in subprocess.call(['ffmpeg', '-i', 'test%d0.png', 'output.avi']) File "C:\Python27\lib\subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\subprocess.py", line 711, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 948, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified` And it works in CMD – Coolcrab May 25 '13 at 10:34
  • 1
    My guess would be that the location of the `ffmpeg` binary is not in the [system variable "PATH"](http://www.computerhope.com/issues/ch000549.htm). Either use the full path to the ffmpeg binary in `subprocess.call`, or update the setting for Path. – Roland Smith May 25 '13 at 10:40
  • ffmpeg is in system32, so it should be callable everywhere right? – Coolcrab May 25 '13 at 10:42
  • Try `ffmpeg.exe` instead of `ffmpeg`? – Roland Smith May 25 '13 at 10:43
5

I know this question is old, but now there is an excellent wrapper for ffmpeg in Python : ffmpeg-python. You will find it at https://github.com/kkroening/ffmpeg-python

With it, the command could be achieved this way:

import ffmpeg
ffmpeg
    .input('test*.png', pattern_type='glob')
    .output('output.avi')
    .run()
antoninod
  • 338
  • 3
  • 10
3

I also use subprocess but in another way.

As @Roland Smith claimed, the preferred method is (which isn't really comfortable):

subprocess.call(['ffmpeg', '-i', 'test%d0.png', 'output.avi'])

The second method can be more comfortable to use but can have some problems:

subprocess.call('ffmpeg -i test%d0.png output.avi', shell=True)

Besides the fact that is suggested avoiding setting the shell parameter to "True", I had problems when the output folder contains round brackets, that is: "temp(5)/output.avi".

A more robust way is the following:

import subprocess
import shlex

cmd = shlex.split('ffmpeg -i test%d0.png output.avi')
subprocess.call(cmd)

To know more about shlex. In particular, for shlex.split:

Split the string s using shell-like syntax.

Rosario Scavo
  • 466
  • 4
  • 7
0

What version of Python are you using? getstatusoutput() is deprecated since version 2.6. In Python 3 you can use subprocess for the same effect.

subprocess.getoutput('cd /Users/Alex/code/pics/')
craighagerman
  • 373
  • 2
  • 8