【问题标题】:Setting output port for rtsp stream from python从python设置rtsp流的输出端口
【发布时间】:2018-10-05 09:13:49
【问题描述】:

我正在尝试从 python 生成 opencv 帧的 RTSP 流。为此,我使用了 Gstreamer,并且能够在另一台 PC 上流式传输帧并使用 VLC 读取它们。 我的问题是我的代码使用默认端口8554(即流在rtsp://hostIp:8554/test),而我想流到端口554

我尝试了很多对launch_string 的添加,例如'! gdppay ! tcpserversink host=serverIp port=554'。但是,应用程序会因 gstreamer 错误而崩溃,或者根本没有流式传输。

这可能吗?

我的代码(大部分来自here):

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


class SensorFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self, fps, img_shape, cols, properties={}):
        super(SensorFactory, self).__init__(**properties)
        self.height = int(img_shape[0])
        self.width = int(img_shape[1] * cols)
        self.number_frames = 0
        fps = int(fps)
        self.cap = None
        self.duration = 1.0 / fps * Gst.SECOND  # duration of a frame in nanoseconds
        caps_str = 'caps=video/x-raw,format=BGR,width={},height={},framerate={}/1 '.format(self.width,
                                                                                       self.height,
                                                                                       fps)
        self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME ' + caps_str + \
                         '! videoconvert ' + \
                         '! video/x-raw,format=I420 ' + \
                         '! x264enc tune=zerolatency speed-preset=fast ' + \
                         '! rtph264pay config-interval=1 pt=96 name=pay0 ' + \
                         '' # <<<=== add more configs here
        print(self.launch_string)

    def set_cap(self, cap):
        self.cap = cap

    def on_need_data(self, src, length):
        if self.cap.isOpened():
            ret, frame = self.cap.read()
            if ret:
                if frame.shape[:2] != (self.height, self.width):
                    frame = cv2.resize(frame, (self.width, self.height))
                data = frame.tostring()
                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 = self.appsrc.emit('push-buffer', buf)
                print('[INFO]: frame {}'.format(self.number_frames))
                if retval != Gst.FlowReturn.OK:
                    print("[INFO]: retval not OK: {}".format(retval))

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

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


class GstServer(GstRtspServer.RTSPServer):
    def __init__(self, fps, img_shape, cols, properties={}):
        GObject.threads_init()
        Gst.init(None)
        super(GstServer, self).__init__(**properties)
        self.factory = SensorFactory(fps=fps, img_shape=img_shape, cols=cols)
        self.factory.set_shared(True)
        self.get_mount_points().add_factory('/test', self.factory)
        self.attach(None)
        self.loop = None

    def run(self):
        self.loop = GObject.MainLoop()
        self.loop.run()

谢谢。

【问题讨论】:

    标签: python gstreamer gstreamer-1.0


    【解决方案1】:

    在您的 GstServer 类中尝试使用 self.set_service("554")。请注意,您需要以提升的权限运行它,因为端口

    【讨论】:

    • 非常感谢。它解决了我的问题,确实需要“sudo”才能使用端口 554。
    猜你喜欢
    • 2015-10-05
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多