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 host=xxx.xxx.xxx.xxx 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

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

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

while True:
    ret,frame=cap.read()

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

    gst.write(frame)

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

cap.release()
gst.release()

XXX replaced by my ip and port.

Any help please?

Thanks