Gsteramer on TX2 for video endoce and decode

I’d like to program with Gsteramer on TX2 for video encode and decode.
I will receive video from a carema and then I want to encode and save it as *.mp4.
After that I will realize a video player ,so I need to decode the above *.mp4 file.
I find that there is only command line for reference.I wonder if there is also some sample code.If any,I will appreciate it you send me a link.

You may first proptype from terminal with gst-launch.
You would have a pipeline such as :

  1. CSI onboard camera, H265 codec and matroska container:
gst-launch-1.0 nvcamerasrc num-buffers=300 ! omxh265enc ! h265parse ! matroskamux ! filesink location=./CSI_capture_300frames.mkv
  1. USB camera (here providing YUY2 format), H265 codec and matroska container:
gst-launch-1.0 v4l2src device=/dev/video1 num-buffers=300 ! nvvidconv ! omxh265enc ! h265parse ! matroskamux ! filesink location=./USB_capture_300frames.mkv

If your camera only provides bayer format, you may have to use plugin bayer2rgb.

You may also try H264 codec and mp4mux.

When you get your pipeline running fine with your camera, you can start programming. You may look at this post…The user is getting bayer frames from camera, encoding in H264 and then muxing into a rtp stream, you would just change the last part for using the matroska or mp4 muxer and finally send to filesink.

Another project you can look at is https://github.com/pcgamelore/SingleCameraPlaceholder.
You may get further help on gstreamer users groups.

Thanks for your reply.
I have try by the following command lines:

gst-launch-1.0 videotestsrc ! 'video/x-raw, format=(string)I420,
width=(int)640, height=(int)480' ! omxh264enc ! 'video/x-h264, stream format=(string)byte-stream' ! h264parse ! qtmux ! filesink
location=test.mp4 -e

As a result it succeed save test.mp4,but when I try to translate these commands to my program.
I have no idea what should I do,and the post you offered me helped me get to know a little about how to translate.
Further more,Are there any sample code which just match the commandlines?