Gst string to read YV12 camera data

I am using a 3G-SDI camera which is giving output in YV12 format. I can view the image when i run the following command :
gst-launch-1.0 v4l2src io-mode=2 device=/dev/video1 ! video/x-raw,width=1920,height=1080,format=YV12 ! xvimagesink sync=false&

But if i am getting error when i try to do the following :
A.
gst_str = ('v4l2src device=/dev/video1 ! ’
'video/x-raw, width=(int)1920, height=(int)1080, format=(string)YV12 ! ’
‘xvimagesink sync=false !’ ‘appsink’)

return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

Error Message : GStreamer warning: Error opening bin: could not link xvimagesink0 to appsink0

B. gst_str = ('v4l2src device=/dev/video1 ! ’
‘video/x-raw, width=(int)1920, height=(int)1080, format=(string)YV12 ,framerate=(fraction)30/1 ! ’
‘videoconvert ! video/x-raw !’ ’ appsink’)

return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

Error Message : GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
But the above command works for USB camera which is in UYVY format.

C. gst_str = ('v4l2src device=/dev/video1 ! ’
'video/x-raw, width=(int){},height=(int)1920,format=(string)1080,framerate=(fraction)30/1 ! ’
‘nvvidconv !’
'video/x-raw, width=(int)1920, height=(int)1080,format=(string)BGRx! ’
‘videoconvert ! appsink’)
Error: Error opening bin: could not link v4l2src0 to nvvconv0, nvvconv0 can’t handle caps video/x-raw, width=(int)1920, height=(int)1080, format=(string)YV12, framerate=(fraction)30/1

Can someone suggest what should be correct format for gst_str to read the YV12 format camera data ?
Thanks

YV12 format is not supported by nvvidconv, so you would have to use videoconvert on CPU:

gst_str = 'v4l2src io-mode=2 device=/dev/video1 ! video/x-raw,width=1920,height=1080,format=YV12 ! videoconvert ! video/x-raw, format=BGR ! appsink'
cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

Thanks. It worked. I am facing one more issue :

I am trying to save the yolo v4 output in mp4 format. I am using the following gst_str
gst_str = ('appsrc ! videoconvert ! omxh264enc ! mpegtsmux ! ’
‘filesink location=output.mp4’)
writer= cv2.VideoWriter(gst_str, cv2.CAP_GSTREAMER, 0, 6,(1920, 1080))

It throws following error :
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to ‘/tmp/runtime-root’
Framerate set to : 6 at NvxVideoEncoderSetParameterNvMMLiteOpen : Block : BlockType = 4
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4
H264: Profile = 66, Level = 40
[ WARN:0] global /mnt/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (1289) close_ OpenCV | GStreamer warning: Error during VideoWriter finalization

But i am able to save the direct camera input by above method. Can you please help to resolve this ?

First be sure the 6 fps you are setting here are according to the capture framerate, unless you’re managing yourself the videorate. Also note that a float value is expected.

You may try such a pipeline for a ts file:

gst_str = ('appsrc ! video/x-raw, format=BGR ! videoconvert ! video/x-raw, format=BGRx ! nvvidconv ! omxh264enc ! mpegtsmux ! filesink location=output.ts’)
writer= cv2.VideoWriter(gst_str, cv2.CAP_GSTREAMER, 0, float(6),(1920, 1080))

It may sometime help to add queue:

gst_str = ('appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw, format=BGRx ! nvvidconv ! omxh264enc ! h264parse ! mpegtsmux ! filesink location=output.ts’)

If you want a mp4 file, you may use mp4mux or qtmux instead of mpegtsmux:

gst_str = ('appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw, format=BGRx ! nvvidconv ! omxh264enc ! h264parse ! qtmux ! filesink location=output.mp4’)

Thanks a lot. It worked !!