VideoWriter with nvv4l2h264enc: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

I am trying to encode video stream with nvv4l2h264enc:

int main(){
    Size sz = Size(640, 480);
    string pipe2="appsrc ! video/x-raw,format=(string)BGR "
                 "! videoconvert ! video/x-raw,format=(string)I420 "
                 "! nvvidconv ! 'video/x-raw(memory:NVMM)' "
                 "! nvv4l2h264enc !  h264parse ! matroskamux "
                 "! filesink location=a.mp4 ";
                 //"!  tcpserversink port=8888 host=0.0.0.0  ";
    VideoWriter writer(pipe2,VideoWriter::fourcc('a','v','c','1'),6,sz,true);
    if (!writer.isOpened()) {
        printf("=ERR= can't create video writer\n");
        return -1;
    }
    long ts0=CommonUtils::getTs();
    int fc=0,fpsc=0,fps=0;
    while(1){
         Mat frame = Mat::zeros(640,480,CV_8UC3);
        if (frame.empty())
            break;
        string str= "frame " + to_string(fc) + " fps="+to_string(fps);
        putText(frame,str,Point(200,200),0,1,Scalar(255,200,120),2);
        fc++;
        //imshow( "Frame", frame );
        long ts=CommonUtils::getTs();
        fpsc++;
        if (ts - ts0>1000){
            fps=fpsc;
            fpsc=0;
           ts0=ts;
           cout << " fps="<<fps<<endl;
        }
        writer<<frame;
        char c=(char)waitKey(25);
        if(c==27)
            break;
    }
    destroyAllWindows();
    return 0;
}

but it gives me these errors:

(gstreamer-opencv:31847): GStreamer-CRITICAL **: 11:15:37.684: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

(gstreamer-opencv:31847): GStreamer-CRITICAL **: 11:15:37.696: gst_element_link_pads_filtered: assertion 'GST_IS_BIN (parent)' failed

(gstreamer-opencv:31847): GStreamer-CRITICAL **: 11:15:37.699: gst_element_link_pads_filtered: assertion 'GST_IS_BIN (parent)' failed
OpenCV Error: Unspecified error (GStreamer: cannot find appsrc in manual pipeline
) in CvVideoWriter_GStreamer::open, file /home/nvidia/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp, line 1503
VIDEOIO(cvCreateVideoWriter_GStreamer (filename, fourcc, fps, frameSize, is_color)): raised OpenCV exception:

/home/nvidia/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp:1503: error: (-2) GStreamer: cannot find appsrc in manual pipeline
 in function CvVideoWriter_GStreamer::open

=ERR= can't create video writer

how can I fix it?

Hi,
The pipeline in below link is similar to your case:
https://devtalk.nvidia.com/default/topic/1064032/

FYR.