首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将视频(磁盘上)转换为rtsp流

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

Stack Overflow用户
提问于 2020-01-22 11:37:07
回答 2查看 16.4K关注 0票数 9

我在我的本地磁盘上有一个视频文件,我想从它创建一个rtsp流,我将在我的一个项目中使用它。一种方法是从vlc创建rtsp流,但是我想用代码来实现它(python会更好)。我像这样尝试过opencv的VideoWritter

代码语言:javascript
复制
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

代码语言:javascript
复制
[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是另一种选择,但由于我从未使用过它,所以如果有人指点我的正确方向,那就太好了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-23 09:01:05

您试图通过TCP服务器公开RTP协议,但请注意,RTP不是RTSP,RTP (和RTCP)只能是RTP的一部分。

无论如何,有一种方法可以使用GstRtspServer (gi包)来使用GStreamer的GstRtspServer和Python (gi包)来创建GstRtspServerRTSP服务器。

假设您已经在您的计算机上有Gstreamer,首先是安装gi python软件包,然后是安装Gstreamer服务器 (这不是标准Gstreamer安装的一部分)。

Python代码通过简单的RTSP服务器公开mp4容器文件

代码语言:javascript
复制
#!/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来玩这个

代码语言:javascript
复制
vlc -v rtsp://127.0.0.1:8554/stream1

或者用Gstreamer

代码语言:javascript
复制
gst-launch-1.0 playbin uri=rtsp://127.0.0.1:8554/stream1

但是,如果您实际上不需要RTSP,而只需要使用Gstreamer管道(利用)的端到端RTP就可以完成这项工作。

代码语言:javascript
复制
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可以播放它

代码语言:javascript
复制
vlc -v rtp://127.0.0.1:5000
票数 13
EN

Stack Overflow用户

发布于 2022-05-25 10:21:34

如何在流水线中添加帧率和比特率

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59858898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档