我的本地磁盘上有一个视频文件,我想从中创建一个rtsp流,我将在我的一个项目中使用它。一种方法是从vlc创建rtsp流,但是我想用代码(python会更好)。我已经像这样尝试过opencv的VideoWritter
import cv2
_dir = "/path/to/video/file.mp4"
cap = cv2.VideoCapture(_dir)
framerate = 25.0
out = cv2.VideoWriter(
"appsrc ! videoconvert ! x264enc noise-reduction=10000 speed-preset=ultrafast tune=zerolatency ! rtph264pay config-interval=1 pt=96 ! tcpserversink host=127.0.0.1 port=5000 sync=false",
0,
framerate,
(1920, 1080),
)
counter = 0
while cap.isOpened():
ret, frame = cap.read()
if ret:
out.write(frame)
print(f"Read {counter} frames",sep='',end="\r",flush=True)
counter += 1
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
out.release()
但是当我像这样在vlc上流式传输时
vlc -v rtsp://127.0.0.1:5000我得到
[00007fbb307a3e18] access_realrtsp access error: cannot connect to 127.0.0.1:5000
[00007fbb2c189f08] core input error: open of `rtsp://127.0.0.1:5000' failed
[00007fbb307a4278] live555 demux error: Failed to connect with rtsp://127.0.0.1:5000
Gstreamer是另一种选择,但由于我从未使用过,所以如果有人指出我正确的方向,那将很好。
[您试图通过TCP服务器公开RTP协议,但请注意RTP不是RTSP,并且RTP(和RTCP)只能是RTSP的一部分。
无论如何,有一种方法可以使用GStreamer的GstRtspServer和Gstreamer的Python接口(gi
包)来创建使用GStreamer和Python的RTSP服务器。
假设您已经在计算机上安装了Gstreamer,请先单击install gi python package,然后再单击install Gstreamer RTSP server(这不是标准Gstreamer安装的一部分)。
通过简单的RTSP服务器公开mp4容器文件的Python代码
#!/usr/bin/env python import sys import gi gi.require_version('Gst', '1.0') gi.require_version('GstRtspServer', '1.0') from gi.repository import Gst, GstRtspServer, GObject, GLib loop = GLib.MainLoop() Gst.init(None) class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory): def __init__(self): GstRtspServer.RTSPMediaFactory.__init__(self) def do_create_element(self, url): #set mp4 file path to filesrc's location property src_demux = "filesrc location=/path/to/dir/test.mp4 ! qtdemux name=demux" h264_transcode = "demux.video_0" #uncomment following line if video transcoding is necessary #h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc" pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode) print ("Element created: " + pipeline) return Gst.parse_launch(pipeline) class GstreamerRtspServer(): def __init__(self): self.rtspServer = GstRtspServer.RTSPServer() factory = TestRtspMediaFactory() factory.set_shared(True) mountPoints = self.rtspServer.get_mount_points() mountPoints.add_factory("/stream1", factory) self.rtspServer.attach(None) if __name__ == '__main__': s = GstreamerRtspServer() loop.run()
请注意
stream1
的RTSP流qtdemux
从MP4容器中获取视频。您也可以扩展管道以提取音频(并通过RTSP服务器公开音频)您可以使用VLC播放它
vlc -v rtsp://127.0.0.1:8554/stream1
或使用Gstreamer
,则遵循Gstreamer管道(利用rtpbin)将完成此工作gst-launch-1.0 playbin uri=rtsp://127.0.0.1:8554/stream1
但是,如果您实际上不需要RTSP,而只是端到端RTP
gst-launch-1.0 -v rtpbin name=rtpbin \ filesrc location=test.mp4 ! qtdemux name=demux \ demux.video_0 ! decodebin ! x264enc ! rtph264pay config-interval=1 pt=96 ! rtpbin.send_rtp_sink_0 \ rtpbin.send_rtp_src_0 ! udpsink host=127.0.0.1 port=5000 sync=true async=false
并且VLC可以和它一起玩
vlc -v rtp://127.0.0.1:5000