python脚本中的ffmpeg
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42438380/
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
ffmpeg in python script
提问by jawwe
I would like to run the following command in a python script, I also want to make it loop over several videos in a folder. This is the command I want to run.
我想在 python 脚本中运行以下命令,我还想让它循环播放文件夹中的多个视频。这是我要运行的命令。
ffmpeg -i mymovie.avi -f image2 -vf fps=fps=1 output%d.png
ffmpeg -i mymovie.avi -f image2 -vf fps=fps=1 output%d.png
I want to fit it in something like this:
我想把它放在这样的地方:
import ffmpy
import os
path = './Videos/MyVideos/'
for filename in os.listdir(path):
name = filename.replace('.avi','')
os.mkdir(os.path.join(path,name))
*ffmpeg command here*
I found a wrapper for ffmpeg called ffmpy, could this be a solution?
我找到了一个名为 ffmpy 的 ffmpeg 包装器,这可能是一个解决方案吗?
回答by ocelot
From a brief look at FFMPY, you could do this using ffmpy.FFmpeg, as that allows any and all FFMPEG command line options, including -f.-- Click the link for documentation.
简单地看一下 FFMPY,您可以使用 ffmpy.FFmpeg 来完成此操作,因为它允许任何和所有 FFMPEG 命令行选项,包括 -f。-- 单击文档链接。
You could do the FFMPEG command with os.system
. You'll need to import OS anyway to iterate through the files.
您可以使用os.system
. 无论如何,您都需要导入操作系统以遍历文件。
You would need to iterate through all the files in a directory though. This would be the more challenging bit, it's quite easy with a for loop though.
不过,您需要遍历目录中的所有文件。这将是更具挑战性的一点,尽管使用 for 循环很容易。
for filename in os.listdir(path):
if (filename.endswith(".mp4")): #or .avi, .mpeg, whatever.
os.system("ffmpeg -i {0} -f image2 -vf fps=fps=1 output%d.png".format(filename))
else:
continue
The above code iterates through the directory at path
, and uses command prompt to execute your given FFMPEG command, using the filename (if it's a video file) in place of mymovie.avi
上面的代码遍历目录 at path
,并使用命令提示符执行给定的 FFMPEG 命令,使用文件名(如果是视频文件)代替mymovie.avi
回答by Tee Jung
Try pydemux in https://github.com/Tee0125/pydemux. Pydemux module can extract video frames as in Pillow Image format
在https://github.com/Tee0125/pydemux 中尝试 pydemux 。Pydemux 模块可以像 Pillow Image 格式一样提取视频帧
from PyDemux import Video
v = Video.open('video.mov')
i = 0
while True:
im = v.get_frame()
if im is None:
break
im.save('output%d.png'%i)
i = i + 1