As mentionned by @bka, you may use a container for storing your encoded video with details about resolution, framerate, encoding… For AVI files, use avimux, for mp4 files use qtmux (better than mp4mux), for MKV files use matroskamux, …
Note that H264 can have different stream-formats, so you may add h264parse for doing conversion if needed.
Also note that for gstreamer backend, opencv VideoWriter would use RAW (0) fourcc.
So you may try:
# Writer encoding into H264 and storing into AVI container file with your PC
cv::VideoWriter video_writer("appsrc ! video/x-raw, format=BGR ! videoconvert ! nvh264enc bitrate=4000 ! h264parse ! avimux ! filesink location=out.avi", cv::CAP_GSTREAMER, 0, float(fps), cv::Size(width, height));
# Writer encoding into H264 and storing into MP4 container file with Jetson HW encoder
cv::VideoWriter video_writer("appsrc ! video/x-raw, format=BGR ! videoconvert ! nvv4l2h264enc bitrate=4000000 ! h264parse ! qtmux ! filesink location=out.mp4", cv::CAP_GSTREAMER, 0, float(fps), cv::Size(width, height));