cv2.VideoWriter() using gstreamer not working with RPI camera module 2

Hello everyone, this is my first topic so I hope I will make myself clear.

I have a Raspberry Pi 4 and a Rpi camera module 2 connected to it (working safe and sound). When I launch the command: libcamera-vid --height 480 --width 640 -v -n -t 200000 -o - | gst-launch-1.0 -e -v fdsrc ! h264parse ! rtph264pay config-interval=5 pt=96 ! udpsink port=XXXX everything works just fine.

However, if I want to start gstreamer from opencv python, that pipeline does not work. Here is my code:

import cv2

gst=cv2.VideoWriter('fdsrc ! h264parse ! rtph264pay config-interval=5 pt=96 ! udpsink port=XXXX', cv2.CAP_GSTREAMER, 0, 25, (640,480), True)

if not cap.isOpened():
    print('VideoCapture not opened')

while True:

    if not ret:
        print("Empty frame");break


    if cv2.waitKey(1)&0xFF == ord('q'):


XXX replaced by my ip and port.

Any help please?


This might be an old post, but I had an issue on my Pi 3b. The default opencv installed does not have support for gstreamer. Run the following python code and check if it does.

import cv2

If not, you need to recompile opencv with it enabled.

Hope it helps if you have not resolved the problem yet