# OpenCV Ffmpg recording using HWenc?

**URL:** <https://forums.developer.nvidia.com/t/opencv-ffmpg-recording-using-hwenc/166924>\
**Category:** Jetson Nano\
**Tags:** opencv, ffmpeg, encoder\
**Created:** [January 27, 2021, 11:11am UTC](https://forums.developer.nvidia.com/t/opencv-ffmpg-recording-using-hwenc/166924 "2021-01-27T11:11:03Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Honey\_Patouceul](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/honey_patouceul/32/389777_2.png) [@Honey\_Patouceul](https://forums.developer.nvidia.com/u/Honey_Patouceul)\
**Post date:** [January 28, 2021, 8:22pm UTC](https://forums.developer.nvidia.com/t/opencv-ffmpg-recording-using-hwenc/166924/7 "2021-01-28T20:22:16Z")

</div>

> [@Legion](#):
>
> `outfile.write(image.ptr(),image.rows*image.cols*image.channels());`

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](https://forums.developer.nvidia.com/t/tx2-jetson-multimedia-api-samples-01-video-encode-output-h-264-file-can-not-be-opened-by-windows-media-player/139867/3).

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:

```auto
  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](https://github.com/jocover/jetson-ffmpeg), apply the patch to ffmpeg sources, set:

```auto
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):

```auto
./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:

```auto
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:

```auto
  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);
  }

```

---

_[View the full topic](https://forums.developer.nvidia.com/t/opencv-ffmpg-recording-using-hwenc/166924)._
