繁体   English   中英

如何将视频(在磁盘上)转换为 rtsp 流

[英]How to convert a video (on disk) to a rtsp stream

我的本地磁盘上有一个视频文件,我想从中创建一个 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,首先安装 gi python 包,然后安装 Gstreamer RTSP 服务器(它不是标准 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()

注意

  • 此代码将在默认端口 8554 上公开名为stream1的 RTSP 流
  • 我使用qtdemux从 MP4 容器中获取视频。 您也可以扩展上述管道以提取音频(并通过 RTSP 服务器公开它)
  • 要减少 CPU 处理,您只能提取视频而不对其进行解码并将其再次编码为 H264。 但是,如果需要转码,我会留下一个注释行来完成这项工作(但它可能会阻塞功能较弱的 CPU)。

你可以用 VLC 玩这个

vlc -v rtsp://127.0.0.1:8554/stream1

或使用 Gstreamer

gst-launch-1.0 playbin uri=rtsp://127.0.0.1:8554/stream1

但是,如果您实际上不需要 RTSP,而只需要遵循 Gstreamer 管道(使用rtpbin的端到端 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

如何在管道中添加帧率和比特率

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM