Error opening v4l2 source on GStreamer with OpenCV

I am facing issues using GStreamer with OpenCV on Jetson Xavier NX.
I am trying to run a GStreamer pipeline through C++ using OpenCV. The following pipeline works using gst-launch-1.0:

gst-launch-1.0 v4l2src device=/dev/pal5 io-mode=2 ! 'image/jpeg,width=3840,height=2160' ! nvv4l2decoder mjpeg=1 ! 'video/x-raw(memory:NVMM)' ! nvvidconv ! video/x-raw ! fpsdisplaysink video-sink=fakesink sync=false text-overlay=false -v

This is working good and giving me 30fps. However, trying to run the same through C++ and OpenCV with the following:

        cv::VideoCapture cap;
        string pipeline;
        pipeline = "v4l2src device=/dev/pal5 io-mode=2 ! 'image/jpeg,width=3840,height=2160' ! nvv4l2decoder mjpeg=1 ! 'video/x-raw(memory:NVMM)' ! nvvidconv ! video/x-raw !  appsink";   , cv::CAP_GSTREAMER);

Using appsink instead of fakesink here.

is giving me the following errors:

(frame_capture:19071): GStreamer-CRITICAL **: 17:37:02.051: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

(frame_capture:19071): GStreamer-CRITICAL **: 17:37:02.084: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed
[ WARN:0] global /home/dreamvu/opencv/modules/videoio/src/cap_gstreamer.cpp (713) open OpenCV | GStreamer warning: Error opening bin: syntax error
[ WARN:0] global /home/dreamvu/opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

I suspect that the issue is in getting to the v4l2 source.
I have also tried running this with root access but still got the same issue.

You would need to convert frame data to BGR and then send to appsink. Please try

pipeline = "v4l2src device=/dev/pal5 io-mode=2 ! 'image/jpeg,width=3840,height=2160' ! nvv4l2decoder mjpeg=1 ! 'video/x-raw(memory:NVMM)' ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR !  appsink"; 

May refer to this post:
Set camera decoder in OpenCV on Jetson Nano - #8 by DaneLLL

I still got the same error.
Do I need to use instead of for this pipeline as mentioned in the linked post?

Please try without ' ':

changing 'image/jpeg,width=3840,height=2160' to image/jpeg,width=3840,height=2160

Not sure but it probably makes the gstreamer pipeline not being parsed correctly.

Yeah this was the issue
Thanks for your help!