How to writer opencv mats into h264 file with appsrc

I tried to output a h264-encoded video with a series of image captured and analysed by a jetson model. The videoWriter of opencv is not available since i can’t manage to use it with libx264. But the pipeline just output a empty file.

Here is my entire python scripe for testing:

import gi
import cv2
import numpy as np
import time

gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib


class EncodePipe:
    def __init__(self,name) :
        # create element
        self.src = Gst.ElementFactory.make("appsrc","src")
        self.src.set_property("format", Gst.Format.DEFAULT)
        self.conv = Gst.ElementFactory.make("videoconvert","conv")
        self.encoder = Gst.ElementFactory.make("x264enc","encoder")
        self.mux = Gst.ElementFactory.make("mp4mux","mux")
        self.file = Gst.ElementFactory.make("filesink","file")

        # set element properties
        self.file.set_property("location", "./test_file_out.mp4")
        self.src_caps = Gst.caps_from_string("video/x-raw, format=(string)BGR, width=1920, height=1080, framerate=25/1")
        self.src.set_property("caps", self.src_caps)

        # generate pipe and link
        self.pipe =

        # link element

        ret = self.pipe.set_state(Gst.State.PLAYING)


    def ndarray_to_gst_buffer(self,array):

    def put_image_to_src(self,img):
        # input the image and generate video
        buffer = Gst.Buffer.new_wrapped(img.tobytes())
        ret = self.src.emit("push-buffer",buffer)
        return ret

    def despose(self):
        if self.src is not None:
        print("[Gst enc] pipe set to NULL.")

if __name__ == '__main__':

    gstVideoWriter = EncodePipe('test_record')
    for i in range(0,100): 

        gstVideoWriter.put_image_to_src(np.random.randint(0,255,size=(1080,1920,3), dtype=np.uint8))


As the output information of gst-inspect-1.0 --version , the version of gstreamer is 1.16.3.
That is weird, when I tried to replace the src.emit(“push_buffer”) methodm, it return:

'gi.repository.Gst' object has no attribute 'do_push_buffer'


'GstAppSrc' object has no attribute 'push_buffer'

which is no the same as appsrc (

Now I set 40*Gst.MSECOND*i (which the i is the order of frames ) to both the pts and dts of buffers. I did get a mp4 video, but it still can’t be played.
I used ffmpeg to see what happen to the video file. i gave me “moov atom not found”