在 python (ffmpeg) 中运行 cmd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16748148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running cmd in python (ffmpeg)
提问by Coolcrab
Atm I have this as my code, the first line seems to work well but the 2nd gives errrors.
Atm 我有这个作为我的代码,第一行似乎运行良好,但第二行给出了错误。
os.chdir('C://Users/Alex/Dropbox/code stuff/test')
subprocess.call(['ffmpeg', '-i', 'test%d0.png', 'output.avi'])
error:
错误:
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
采纳答案by Coolcrab
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'])
回答by craighagerman
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.
您使用的是什么版本的 Python?自 2.6 版起不推荐使用 getstatusoutput()。在 Python 3 中,您可以使用 subprocess 来达到相同的效果。
subprocess.getoutput('cd /Users/Alex/code/pics/')
回答by Roland Smith
It is better to call subprocess.callin another way.
最好subprocess.call用另一种方式调用。
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.
所有调用都需要 args 并且应该是字符串或程序参数序列。提供参数序列通常是首选,因为它允许模块处理任何需要的参数转义和引用(例如,允许文件名中的空格)。如果传递单个字符串,则 shell 必须为 True(见下文),否则字符串必须简单地命名要执行的程序而不指定任何参数。
回答by antoninod
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
我知道这个问题是旧的,但现在在Python中的ffmpeg优良的包装:
ffmpeg-python。你会在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()

