在python中运行cmd(ffmpeg)

2024-04-25 20:57:53 发布

您现在位置:Python中文网/ 问答频道 /正文

自动取款机我有这个作为我的代码,第一行似乎工作良好,但第二行给出错误。

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

错误:

Traceback (most recent call last):
  File "C:\Users\Alex\Dropbox\code stuff\solarsystem.py", line 56, in <module>
    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

Tags: inpylib错误linecodecallusers
3条回答

我知道这个问题由来已久,但是现在有一个很好的Python中ffmpeg的包装: ffmpeg-python。你可以在https://github.com/kkroening/ffmpeg-python找到它

有了它,命令可以这样实现:

import ffmpeg
ffmpeg
    .input('test*.png', pattern_type='glob')
    .output('output.avi')
    .run()

最好用另一种方式调用subprocess.call

首选方法是:

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

或者:

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

您可以在manual中找到原因。我引用:

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.

对于后来寻找答案的人来说,这是成功的。(必须用空格分隔命令。)

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'])

相关问题 更多 >

    热门问题