为python中的rtsp流设置输出端口
我试图从python生成opencv帧的RTSP流。我已经使用了Gstreamer,并且能够在另一台PC上通过VLC传输帧和读取帧。 我的问题是,我的代码使用默认端口8554(即流位于为python中的rtsp流设置输出端口,python,gstreamer,gstreamer-1.0,Python,Gstreamer,Gstreamer 1.0,我试图从python生成opencv帧的RTSP流。我已经使用了Gstreamer,并且能够在另一台PC上通过VLC传输帧和读取帧。 我的问题是,我的代码使用默认端口8554(即流位于rtsp://hostIp:8554/test),而我想流到端口554 我尝试了许多对启动字符串的添加,例如!格德佩!tcpserversink主机=服务器IP端口=554'。但是,该应用程序因gstreamer错误而崩溃,或者没有任何数据流 这可能吗 我的代码(大部分来自): 导入cv2 导入gi 导入子流程 g
rtsp://hostIp:8554/test
),而我想流到端口554
我尝试了许多对启动字符串的添加,例如!格德佩!tcpserversink主机=服务器IP端口=554'
。但是,该应用程序因gstreamer错误而崩溃,或者没有任何数据流
这可能吗
我的代码(大部分来自):
导入cv2
导入gi
导入子流程
gi.require_版本('Gst','1.0')
gi.require_版本('GstRtspServer','1.0')
从gi.repository导入Gst、GstRtspServer、GObject
类别SensorFactory(GstRtspServer.RTSPMediaFactory):
def uu init uu(self、fps、img_shape、cols、properties={}):
超级(传感器工厂,自).\uuuu初始化(**属性)
self.height=int(img_形状[0])
self.width=int(img_形状[1]*cols)
self.number\u帧=0
fps=int(fps)
self.cap=无
self.duration=1.0/fps*Gst.SECOND#以纳秒为单位的帧持续时间
caps_str='caps=video/x-raw,format=BGR,width={},height={},framerate={}/1'。格式(self.width,
自我高度,
fps)
self.launch\u string='appsrc name=source is live=true block=true format=GST\u format\u TIME'+caps\u str+\
'! 视频转换'+\
'! 视频/x-raw,格式=I420'+\
'! x264enc调谐=零延迟速度预设=快速'+\
'! RTPH264支付配置间隔=1 pt=96 name=pay0'+\
''#在您的GstServer类中尝试使用self.set\u服务(“554”)
。请注意,您需要以提升的权限运行此操作,因为端口为<1024,以及它默认为8554的原因。在GstServer类中尝试使用self.set_service(“554”)
。请注意,您需要以提升的权限运行此操作,因为端口小于1024,这也是它默认为8554的原因。非常感谢。它解决了我的问题,确实需要一个“sudo”来使用端口554。非常感谢。它解决了我的问题,确实需要一个“sudo”来使用端口554。
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()