有什么好的 Python 库可以处理视频文件的帧吗?

发布于 2024-10-31 06:49:33 字数 723 浏览 4 评论 0原文

我正在寻找一个Python视频处理库,类似于PIL,我可以在其中迭代源视频的所有帧,访问每个帧的像素数据,绘制到每个帧并将结果保存为新的视频文件。

我发现了几个类似的问题,但它们现在已经很老了:

他们推荐 PyMediaPyFFMPEG。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

━╋う一瞬間旳綻放 2024-11-07 06:49:33

我经常需要同样的东西,据我所知,Python 中的绑定没有好的解决方案。

而且它并不像操纵视频文件的帧那么简单。现代视频文件格式不会一帧一帧地存储帧,而是使用“增量帧”,其中仅存储从一帧到另一帧的变化。其他考虑因素(例如具有可变帧速率的视频)使问题变得更加困难。

过去我使用以下命令从视频生成图像。

ffmpeg -i /path/to/file.mpg -an -r 30 -s 320x240 tmp%06d.jpg

其中 30 是目标帧速率,320x240 是图像尺寸,tmp%06d.jpg 是用于存储生成的 jpeg 的模式。然后,您可以使用 PIL 操作每个帧,并使用 mencoder 或 ffmpeg 将图像再次拼接到电影中:

ffmpeg -r 30 -i tmp%06d.jpg output.mpg

显然,您将丢失音轨。

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.

ffmpeg -i /path/to/file.mpg -an -r 30 -s 320x240 tmp%06d.jpg

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:

ffmpeg -r 30 -i tmp%06d.jpg output.mpg

Obviously, you'll lose the audio track.

凝望流年 2024-11-07 06:49:33

我向你推荐scikit-video,这是我用过的最简单的Python视频处理库遇见了。

这是 scikit-video 项目网站上的官方介绍:

Scikit-video 专为使用 Python 轻松进行视频处理而设计。它以其他成功的 scikit(例如 scikit-learn 和 scikit-image)的精神为蓝本。 scikit-video 的开发人员知道存在用于操作视频的库,例如 PyFFmpeg、MoviePy、PyAV、imageIO 和 opencv。然而,尚未发现任何库可以为研究级视频处理工具提供一体化解决方案。

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:

Scikit-video is designed for easy video processing using Python. It is modeled in the spirit of other successful scikits such as scikit-learn and scikit-image. The developers of scikit-video know libraries exist for manipulating videos, such as PyFFmpeg, MoviePy, PyAV, imageIO, and opencv. However, no libraries have been found to provide an all-in-one solution for research-level video processing tools.

永不分离 2024-11-07 06:49:33

我一直在考虑使用 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.

木森分化 2024-11-07 06:49:33

您可以使用我的 VidGear 视频处理 python 库的 WriteGear API 允许我们利用 FFmpeg 支持的几乎所有可用参数(帧率、比特率、编解码器) 、格式和大小、复用器、解复用器等) in 压缩模式,轻松灵活,同时它可以非常安静地稳健地处理错误/警告。

参数:

例如,要使用 H.264 使用编码器 x264 生成高质量视频,我们可以按如下方式调整其参数以生成无损输出视频:

 output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast", "tune": "zerolatency"} 

然后将此字典传递给 WriteGear,如下面给出的示例

基本使用示例

# import libraries
from vidgear.gears import WriteGear
import cv2

output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writer

stream = cv2.VideoCapture(0) #Open live webcam video stream on first index(i.e. 0) device

writer = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' 

# infinite loop
while True:

    (grabbed, frame) = stream.read()
    # read frames

    # check if frame empty
    if not grabbed:
        #if True break the infinite loop
        break


    # {do something with frame here}
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # write a modified frame to writer
        writer.write(gray) 

        # Show output window
    cv2.imshow("Output Frame", frame)

    key = cv2.waitKey(1) & 0xFF
    # check for 'q' key-press
    if key == ord("q"):
        #if 'q' key-pressed break out
        break

cv2.destroyAllWindows()
# close output window

stream.release()
# safely close video stream
writer.close()
# safely close writer

查看更多高级使用详细信息此处和完整文档此处

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:

 output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast", "tune": "zerolatency"} 

and then pass this dictionary to WriteGear as the example given below

Basic Usage Example

# import libraries
from vidgear.gears import WriteGear
import cv2

output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writer

stream = cv2.VideoCapture(0) #Open live webcam video stream on first index(i.e. 0) device

writer = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' 

# infinite loop
while True:

    (grabbed, frame) = stream.read()
    # read frames

    # check if frame empty
    if not grabbed:
        #if True break the infinite loop
        break


    # {do something with frame here}
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # write a modified frame to writer
        writer.write(gray) 

        # Show output window
    cv2.imshow("Output Frame", frame)

    key = cv2.waitKey(1) & 0xFF
    # check for 'q' key-press
    if key == ord("q"):
        #if 'q' key-pressed break out
        break

cv2.destroyAllWindows()
# close output window

stream.release()
# safely close video stream
writer.close()
# safely close writer

Checkout more advance usage details here and complete docs here

夜还是长夜 2024-11-07 06:49:33

我发现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文