faxcon
faxcon

Reputation: 21

Python-Opencv Write x264 video on memory buffer

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()

Upvotes: 2

Views: 4848

Answers (1)

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

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.

Upvotes: 2

Related Questions