OpenCV Ffmpg recording using HWenc?

Probably the size is wrong here, as this would mean no compression. @DaneLLL may help you with that. Once you’ll get the correct h264 file, you may use a container such as mp4/qt mov or MKV for storing it. You may do that with ffmpeg as in this example.

The simplest way to do what you want to achieve doesn’t answer your initial question, but probably you may try a videoWriter with gstreamer pipeline such as:

  cv::VideoWriter gst_omxh264_writer ("appsrc ! queue ! videoconvert ! video/x-raw,format=I420 ! queue ! omxh264enc ! video/x-h264,format=byte-stream ! matroskamux ! filesink location=test-omxh264-writer.mkv ", 
                                      cv::CAP_GSTREAMER, 
                                      0, fps, 
                                      cv::Size (width, height));
  if (!gst_omxh264_writer.isOpened ()) {
    std::cout << "Failed to open gst-omxh264 writer." << std::endl;
    return (-1);
  }

For answering to your question, you would have to build jocover’s nvmpi lib, apply the patch to ffmpeg sources, set:

export PKG_CONFIG_PATH=/usr/local/share/pkgconfig:$PKG_CONFIG_PATH

Configure this ffmpeg version with pkg-config support and install in your own location (not tested, but such as):

./configure  --extra-libs='-L/usr/lib/aarch64-linux-gnu/tegra -lnvbuf_utils' --extra-cflags='-I /usr/src/jetson_multimedia_api/include/' --enable-nvmpi --enable-libx264 --prefix=/usr/local/ffmpeg --pkgconfigdir=/usr/local/ffmpeg/lib/pkgconfig

When built and installed, adjust PKG_CONFIG_PATH and LD_LIBRARY_PATH in your environment so that your new ffmpeg version can be found:

export LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/ffmpeg/bin:$PATH
export PKG_CONFIG_PATH=/usr/local/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH

and reconfigure opencv with -D WITH_FFMPEG=ON -DFFMPEG_PREFIX=/usr/local/ffmpeg and check with cmake-gui or looking at CMakeCache.txt that variables starting with pkgcfg_lib_FFMPEG_ are set to correct location. Rebuild and install opencv. Then you should be able to use a videoWriter with HW enc using ffmpeg backend:

  cv::VideoWriter ff_h264_writer ("test-ff_h264-writer.mkv",
                                   cv::CAP_FFMPEG,
                                   cv::VideoWriter::fourcc ('X', '2', '6', '4'), fps,
                                   cv::Size (width, height)); 
  if (!ff_h264_writer.isOpened ()) {
    std::cout << "Failed to open ff_h264-writer." << std::endl;
    return (-2);
  }