本文介绍了Python-Opencv在内存缓冲区上写入x264视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将x264视频(或单帧)写入内存缓冲区时遇到问题.在用于图像的opencv中,imencode和imdecode执行此任务.但是我想保存x264视频帧以降低内存使用量并在Internet上发送.我可以使用jpeg,但jpeg的大小大于x264视频帧,并且质量更差.我进行了搜索,但是找不到如何在缓冲区上写入视频帧.

I have a problem for writing x264 video(or single frame) on memory buffer. In opencv for images, imencode and imdecode do this task. But i want save x264 video frame for lower memory usage and sending on internet. I am able to with jpeg but jpeg size bigger than x264 video frame and quality much worser. I searched but i can't find how i write video frame on buffer.

这是在网络摄像头拍摄照片的示例代码

Here is the example code to taking frames on webcam

        import numpy as np
        import cv2


        cap = cv2.VideoCapture(0)
        cap.set(3,320)
        cap.set(4,240)
        cap.set(5,30)
        # Define the codec and create VideoWriter object
        fourcc = cv2.VideoWriter_fourcc(*'x264')
        out = cv2.VideoWriter('.....sample.avi',fourcc, 30.0, (320,240))

        while(cap.isOpened()):
            ret, frame = cap.read()
            if ret==True:
                cv2.imshow('frame',frame)
                out.write(frame) #I want to write memory not disk
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                break

        # Release everything if job is finished
        cap.release()
        out.release()
        cv2.destroyAllWindows()

推荐答案

不幸的是,与 cv2.VideoWriter 无关,因为您无法在之前到达编码的视频帧.out.release().

Unfortunately, there is no way to do with cv2.VideoWriter because you can't reach the encoded video frames before out.release().

我为我的项目找到的方法是实现 D:\ your_directory \ opencv \ sources \ modules \ highgui \ src > cap_ffmpeg_impl.hpp 并将您捕获的帧发送到该库中.您将通过UDP或TCP/IP发送已编码的帧,并使用同一库对它们进行解码.还要记住,您需要编译正确的 ffmpeg 版本才能使用它.

The way I have found for my project is implementing cap_ffmpeg_impl.hpp from D:\your_directory\opencv\sources\modules\highgui\src and send your captured frames in that library. You will send encoded frames via UDP or TCP/IP and decode where they reach with the same library. Also remember, you need to compile right ffmpeg version to use it.

这篇关于Python-Opencv在内存缓冲区上写入x264视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 23:30