Gstreamer-python video and audio rtsp-tcp

ikenetik ikenetik at mail.ru
Thu Mar 25 11:20:00 UTC 2021


Hello, I am new to this aspect, so I can do stupid things. I am unable to
adequately combine the video and audio track for output to the stream.
Output video, but no audio. Please, help)
It's code:



import cv2
import gi

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


class SensorFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self, **properties):
        super(SensorFactory, self).__init__(**properties)
        self.cap = cv2.VideoCapture("test.mp4")
        self.number_frames = 0
        self.fps = 30
        self.duration = 1 / self.fps * Gst.SECOND  # duration of a frame in
nanoseconds
        self.launch_string = "! video/x-raw-yuv, framerate={}/1," \
                            "! theoraenc bitrate=400 ! mux. " \
                            "audiotestsrc ! audiorate ! legacyresample ! " \
                            "audioconvert ! audio/x-raw-float,channels=2 " \
                            "! vorbisenc bitrate=64000 ! mux. " \
                            "oggmux name=mux ! fgdpsink ".format(self.fps)
        # streams to gst-launch-1.0 rtspsrc
location=rtsp://localhost:8554/test latency=50 ! decodebin ! autovideosink

    def on_need_data(self, src, lenght):
        if self.cap.isOpened():
            ret, frame = self.cap.read()
            if ret:
                data = frame.tostring()
                #print(data)
                buf = Gst.Buffer.new_allocate(None, len(data), None)
                buf.fill(0, data)
                buf.duration = self.duration
                timestamp = self.number_frames * self.duration
                buf.pts = buf.dts = int(timestamp)
                buf.offset = timestamp
                self.number_frames += 1
                retval = src.emit('push-buffer', buf)
                #print('pushed buffer, frame {}, duration {} ns, durations
{} s'.format(self.number_frames,
                #                                                                      
self.duration,
                #                                                                      
self.duration / Gst.SECOND))
                if retval != Gst.FlowReturn.OK:
                    print(retval)
        else:
            print('file is not opened,use normal path!')

    def do_create_element(self, url):
        print('do_create is working!')
        return Gst.parse_launch(self.launch_string)

    def do_configure(self, rtsp_media):
        print('do_configure working too!')
        self.number_frames = 0
        appsrc = rtsp_media.get_element().get_child_by_name('source')
        appsrc.connect('need-data', self.on_need_data)


class GstServer(GstRtspServer.RTSPServer):
    print('gi correctly')
    def __init__(self, **properties):
        super(GstServer, self).__init__(**properties)
        self.factory = SensorFactory()
        self.factory.set_shared(True)
        self.get_mount_points().add_factory("/test", self.factory)
        self.attach(None)


Gst.init(None)

server = GstServer()

loop = GLib.MainLoop()
loop.run()



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/


More information about the gstreamer-devel mailing list