Hardware Accelerated Encoding with Gstreamer in CPP Fails with Segmentation Fault

Please try the samples and see if you can run them successfully:
GStreamer freeze when using qtmux and NVIDIA-accelerated h264/h265 encoding - #7 by DaneLLL
Latency issue: nvv4l2h265enc accumulates four images before releasing the first - #3 by DaneLLL

And then you can implement your use-case by referring to the samples.