有什么好的 Python 库可以处理视频文件的帧吗?
我正在寻找一个Python视频处理库,类似于PIL,我可以在其中迭代源视频的所有帧,访问每个帧的像素数据,绘制到每个帧并将结果保存为新的视频文件。
我发现了几个类似的问题,但它们现在已经很老了:
他们推荐 PyMedia 和 PyFFMPEG。 PyMedia 似乎相当过时(但可能仍然有效?),而 PyFFMPEG 虽然较新,但几乎没有文档。
我没能在 Ubuntu 10.10 上安装这些,在我继续之前,是否有:
a) 我应该看看更好的库?
b) 关于如何启动和运行其中任何一个的良好说明?
I'm looking for a Python video processing library, similar to PIL, where I can iterate through all the frames of a source video, access the pixel data for each frame, draw onto each frame and save the result as a new video file.
I've found a couple of similar questions, but they are pretty old now:
They recommend PyMedia and PyFFMPEG. PyMedia seems rather out of date (but may still work?) and PyFFMPEG, while more recent, has almost no documentation.
I've had no luck installing either these on Ubuntu 10.10, before I press on, is there:
a) A better library I should look at?
b) Good instructions on how to get either of these up and running?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我经常需要同样的东西,据我所知,Python 中的绑定没有好的解决方案。
而且它并不像操纵视频文件的帧那么简单。现代视频文件格式不会一帧一帧地存储帧,而是使用“增量帧”,其中仅存储从一帧到另一帧的变化。其他考虑因素(例如具有可变帧速率的视频)使问题变得更加困难。
过去我使用以下命令从视频生成图像。
其中 30 是目标帧速率,320x240 是图像尺寸,tmp%06d.jpg 是用于存储生成的 jpeg 的模式。然后,您可以使用 PIL 操作每个帧,并使用 mencoder 或 ffmpeg 将图像再次拼接到电影中:
显然,您将丢失音轨。
I've often needed the same thing and as far as I know, there is no good solution with bindings in Python.
Also it is not as simple as it may seem to manipulate frames of a video file. A modern file format for video does not store the frames one frame after the other but instead uses "delta frames", in which only the changes from one frame to the other is stored. Other considerations such as video with variable frame rate makes the problem even harder.
In the past I've used the following command to generate images from video.
Where 30 is the target frame rate, 320x240 the image dimension and tmp%06d.jpg the pattern to use to store the generated jpegs. Then you can use PIL to manipulate each frame and mencoder or ffmpeg to stich the images back again into a movie:
Obviously, you'll lose the audio track.
我向你推荐scikit-video,这是我用过的最简单的Python视频处理库遇见了。
这是 scikit-video 项目网站上的官方介绍:
I recommend scikit-video for you which is the most straightforward python video processing library I have ever met.
This is the official introduction to scikit-video from its project website:
我一直在考虑使用 py.processing 进行类似的工作。它可以完成您要求的所有操作,但它是与处理的混合体。你本身并不是在编写 python 代码。不管怎样,它使得使用起来非常容易,但是有很多程序/解释开销,因此对电影进行实时修改可能会很慢。你说你想编辑文件,这样应该是可行的。
I've been looking at using py.processing for similar work. It does everything you're asking, but is a hybrid with Processing. You're not writing python code per se. Anyway, It makes it quite easy to work with, but there is a lot of program/interpretation overhead so it can be slow to do realtime modification of the movies. You said you wanted to edit to file so that should be workable.
您可以使用我的 VidGear 视频处理 python 库的 WriteGear API 允许我们利用 FFmpeg 支持的几乎所有可用参数(帧率、比特率、编解码器) 、格式和大小、复用器、解复用器等) in 压缩模式,轻松灵活,同时它可以非常安静地稳健地处理错误/警告。
参数:
例如,要使用 H.264 使用编码器 x264 生成高质量视频,我们可以按如下方式调整其参数以生成无损输出视频:
然后将此字典传递给 WriteGear,如下面给出的示例
基本使用示例
查看更多高级使用详细信息此处和完整文档此处
You can use my VidGear video-processing python library's WriteGear API that allows us to exploit almost all available parameters supported by FFmpeg (framerate, bitrate, codecs, format, and size,mux, demux etc.) in Compression Mode, effortlessly and flexibly and while doing that it robustly handles errors/warnings all along very quietly.
Parameters:
For example, To use H.264 for producing a high-quality video using the encoder x264, we can tweak its parameters as following to produce lossless output video:
and then pass this dictionary to WriteGear as the example given below
Basic Usage Example
Checkout more advance usage details here and complete docs here
我发现 imageio 是最容易使用的 Python 视频操作库。
使逐帧读取视频、处理视频并将其保存回磁盘变得轻而易举。如今,作者还提供了一个易于在 Windows 中使用的 pip install ffmpeg 包装器, Linux 或 OSX。这里还有一个活跃的社区,在 [python-imageio] 标签下回答与库相关的问题。
I find imageio the easiest to use video manipulation library for Python.
Makes reading videos frame by frame, their processing and saving back to disk a breeze. Nowadays the authors also provide an easy to pip install wrapper for ffmpeg that can be used in Windows, Linux or OSX. There is also an active community here answering questions related to the library under the [python-imageio] tag.