微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 GstRtspServer 将 MJPEG 流转换为 RTSP

如何解决使用 GstRtspServer 将 MJPEG 流转换为 RTSP

我正在尝试使用 Python 和 GI 将 MJPEG 流从旧 IP cam 转换为 RTSP 流。

使用命令行,这工作正常:

gst-launch-1.0 souphttpsrc location=http://192.168.2.153/video/mjpg.cgi ! multipartdemux ! jpegdec ! autovideosink

但是,使用 Python 不起作用:

self.cap = cv2.VideoCapture("http://192.168.2.153/video/mjpg.cgi")
self.number_frames = 0
self.fps = 10
self.duration = 1 / self.fps * Gst.SECOND  # duration of a frame in nanoseconds
self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME ' \
                     'caps=video/x-raw,format=BGR,width=640,height=480,framerate={}/1 ' \
                     '! videoconvert ! video/x-raw,format=I420 ' \
                     '! x264enc speed-preset=ultrafast tune=zerolatency ' \
                     '! rtph264pay config-interval=1 name=pay0 pt=96'.format(self.fps)

效果是 RTSP 服务器启动并运行,没有错误消息,但在查看器(Ubuntu:totem)中只有绿屏。
控制台输出为:

pushed buffer,frame 1,duration 100000000.0 ns,durations 0.1 s
pushed buffer,frame 2,frame 3,frame 4,frame 5,frame 6,durations 0.1 s

这里是完整的 Python 代码(复制自 WisdomPill's answer):

import cv2
import gi

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

class SensorFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self,**properties):
        super(SensorFactory,self).__init__(**properties)
        self.cap = cv2.VideoCapture("http://192.168.2.153/video/mjpg.cgi")
        self.number_frames = 0
        self.fps = 10
        self.duration = 1 / self.fps * Gst.SECOND  # duration of a frame in nanoseconds
        self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME ' \
                             'caps=video/x-raw,framerate={}/1 ' \
                             '! videoconvert ! video/x-raw,format=I420 ' \
                             '! x264enc speed-preset=ultrafast tune=zerolatency ' \
                             '! rtph264pay config-interval=1 name=pay0 pt=96'.format(self.fps)

    def on_need_data(self,src,lenght):
        if self.cap.isOpened():
            ret,frame = self.cap.read()
            if ret:
                data = frame.tobytes()
                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)

    def do_create_element(self,url):
        return Gst.parse_launch(self.launch_string)

    def do_configure(self,rtsp_media):
        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):
    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.set_service('9999')
        self.attach(None)

Gst.init(None)
server = GstServer()
loop = GLib.MainLoop()
loop.run()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。