Jetson nano ,opencv ,Gstreamer ,h265 ,mp4

hi i develop following opencv code to capture frame from mipi camera the show it and save the video but i encounter some errors that can not save video file

std::string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method) {
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + std::to_string(capture_width) + ", height=(int)" +
           std::to_string(capture_height) + ", framerate=(fraction)" + std::to_string(framerate) +
           "/1 ! nvvidconv flip-method=" + std::to_string(flip_method) + " ! video/x-raw, width=(int)" + std::to_string(display_width) + ", height=(int)" +
           std::to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
};
int main(){
   int capture_width  = 1024 ;
    int capture_height = 720 ;
    int framerate      = 30 ;
    int flip_method    = 0 ;
    
    cv::VideoWriter writer;
   writer.open("nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=1024, height=720,format=NV12, 
   framerate=30/1' ! omxh265enc ! qtmux ! filesink location=test.mp4 -e", cv::CAP_GSTREAMER 
   ,30,cv::Size(1024,720));
  
     std::string pipeline;
   pipeline = gstreamer_pipeline(capture_width,
	                              capture_height,
	                              capture_width,
	                              capture_height,
	                              framerate,
	                              flip_method);   
    cv::VideoCapture capture(pipeline, cv::CAP_GSTREAMER);
    
    if(!capture.isOpened()) {
	   std::cout<<"Failed to open camera."<<std::endl;
	   return (-1);
    }
   while (1) {
          if (!capture.read(rgbFrame)) {
            std::cout << "Cannot grab a frame." << std::endl;
            break;
        }
       cv::rectangle(rgbFrame, myRect, cv::Scalar(0, 255, 0), 1, 1);
        writer<<rgbFrame; 
	cv::imshow("Video",rgbFrame);
        int key=cv::waitKey(1);
         if(key=='q')
            break;
         }//while
    
     writer.release();
    capture.release();

above source code show video but in saving video file encounter these errors and nothing saved!!!

GStreamer-CRITICAL **: 15:04:50.586: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed
[ WARN:0@2.833] global cap_gstreamer.cpp:2436 open OpenCV | GStreamer warning: error opening writer pipeline: syntax error
GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 4032 x 3040 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 22.250000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3840 x 2160 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 22.250000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 22.250000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0 
   Camera mode  = 2 
   Output Stream W = 1920 H = 1080 
   seconds to Run    = 0 
   Frame Rate = 59.999999 
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
[ WARN:0@3.581] global cap_gstreamer.cpp:1777 open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Using pipeline: 
	nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1024, height=(int)720, framerate=(fraction)30/1 ! nvvidconv flip-method=0 ! video/x-raw, width=(int)1024, height=(int)720, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink
FPS: 30
Video width: 1024
Video height: 720

i check gstreamer expression on ubuntu command line
gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=1024, height=720,format=NV12, framerate=30/1' ! omxh265enc ! qtmux ! filesink location=test.mp4 -e
it is ok and save video file(test.mp4) but why in my opencv code video file not generate?!!!

Hi,
Please try nvv4l2h265enc and matroskamux plugins. Here is a python sample for reference:
Displaying to the screen with OpenCV and GStreamer - #9 by DaneLLL

Hello,

I suggest removing '' and -e, try with this string

"nvarguscamerasrc ! video/x-raw(memory:NVMM),width=1024,height=720,format=NV12,framerate=30/1 ! omxh265enc ! qtmux ! filesink location=test.mp4"

Hi
i used nvv4l2h265enc and matroskamux plugins but nothing saved!!

std::string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method) {
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + std::to_string(capture_width) + ", height=(int)" +
           std::to_string(capture_height) + ", framerate=(fraction)" + std::to_string(framerate) +
           "/1 ! nvvidconv flip-method=" + std::to_string(flip_method) + " ! video/x-raw, width=(int)" + std::to_string(display_width) + ", height=(int)" +
           std::to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
};

 

int main(int argc, char **argv){
    int capture_width  = 1024 ;
    int capture_height = 720 ;
    int framerate      = 30 ;
    int flip_method    = 0 ;
    std::string pipeline;
 
    pipeline = gstreamer_pipeline(capture_width,
	                              capture_height,
	                              capture_width,
	                              capture_height,
	                              framerate,
	                              flip_method);
        
    cv::VideoWriter writer;
    writer.open("nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=1080, height=720,format=NV12, framerate=30/1' ! nvv4l2h265enc ! matroskamux ! filesink location=test.mp4 -e", cv::CAP_GSTREAMER ,30,cv::Size(1080,720));
    cv::VideoCapture capture(pipeline, cv::CAP_GSTREAMER);
    if(!capture.isOpened()) {
	   std::cout<<"Failed to open camera."<<std::endl;
	   return (-1);
    }
    while (1) {
        if (!capture.read(rgbFrame)) {
            std::cout << "Cannot grab a frame." << std::endl;
            break;
        }
        cv::rectangle(rgbFrame, myRect, cv::Scalar(0, 255, 0), 1, 1);
        writer<<rgbFrame; 
	    cv::imshow("Video",rgbFrame);
         
        int key=cv::waitKey(1);
         if(key=='q')
            break;
              
    }//while
     writer.release();
     capture.release();

   
}

live camera frame is shown but “test.mp4” was not saved with these erros !!

GStreamer-CRITICAL **: 15:04:15.051: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (1422) open OpenCV | GStreamer warning: error opening writer pipeline: syntax error
GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 4032 x 3040 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 22.250000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3840 x 2160 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 22.250000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 22.250000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0 
   Camera mode  = 2 
   Output Stream W = 1920 H = 1080 
   seconds to Run    = 0 
   Frame Rate = 59.999999 
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

Your OpenCV VideoWriter pipeline needs to specify appsrc not nvarguscamerasrc since you are writing an OpenCV Mat to the object and not capturing from the camera directly (since the camera is opened and owned by the VideoCapture object/pipeline)

Additionally, OpenCV VideoWriter only supports images in a BGR format, however nvv4l2h265enc does not. You will therefore need to use videoconvert and nvvidconv to get it into NV12 format or other format the nvv4l2h265enc supports.

I suggest making liberal use of the command gst-inspect-1.0 <gstreamer plugin name> to figure out the compatible input and output formats of different gstreamer elements. For example gst-inspect-1.0 nvv4l2h265enc will show you that it only accepts the following formats:format: { (string)I420, (string)NV12, (string)P010_10LE, (string)NV24 }

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.