Gstreamer-OpenCV的RTSP服务搭建以及推拉流实现

简介

采用边缘设备进行推流,例如RV1126或jetson等,获取检测结果后推流至目的地。在进行视觉任务的程序,无需额外编写独立的串流程序,可以直接将串流和opencv调用驱动结合在一起。

环境配置

sudo apt-get install gstreamer1.0 gstreamer1.0-plugin-bad  gstreamer1.0-plugin-good gstreamer1.0-plugin-ugly 

服务器建设

服务器假设的方法有很多,也可以自行写代码编译运行。这里推荐一个已经编译好的rtsp服务器软件。

https://github.com/bluenviron/mediamtx.git

在release中下载对应服务器架构的软件包,解压即可使用,假设的服务器ip为运行程序的本机地址。

./mediamtx

推送RTSP流

opencv推流非常简单,只需要按照gstreamer的格式编辑好字符串,然后通过视频输出接口cv::VideoWriter进行输出,就可以直接对接软件外部的推流程序。更方便的是,如果含有nvidia设备,可以通过修改字符串的配置直接调用nvidia编解码设备,而不用编写任何代码,进行硬编码。

	int fps = 30;
    std::string enc_cpu = "appsrc ! videoconvert ! video/x-raw,width="+ std::to_string(src.cols) +
    ",height="+ std::to_string(src.rows) +
    ",framerate="+ std::to_string(fps) +
    "/1 ! x264enc speed-preset=veryfast tune=zerolatency bitrate=800 ! rtspclientsink location="+url+" latency=0";
        
    int fourcc = cv::VideoWriter::fourcc('N', 'V', '1', '2');
    cv::Size frameSize(src.cols, src.rows ); // 根据ROS图像消息的分辨率进行调整
    cv::VideoWriter videoWriter(enc_cpu, cv::CAP_GSTREAMER, fourcc, fps, frameSize);
	
	videoWriter.write(src);

抓取RTSP流

VLC方法,延迟高。推荐直接使用gst-launch命令进行拉流。其中不同插件可以根据情况改变。有其实有Nvidia显卡支持的时候,采用nv编解码器会极大的提升效率和速度。

gst-launch-1.0 rtspsrc location=rtsp://192.168.0.166:8554/stream ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! autovideosink
Gstreamer是一个强大的开源多媒体框架,主要用于处理实时音视频流,而OpenCV则是一个计算机视觉库,常用于图像和视频处理。如果你想使用它们组合起来实现实时RTSP推流,你可以这样做: 1. **安装依赖**: 首先确保已经安装了GstreamerOpenCV及相关的插件,比如`gst-rtsp-server`(用于创建RTSP服务器),`opencv-python`(OpenCV的Python接口)。 2. **Gstreamer pipeline**: 设计一个Gstreamer管道,它通常包括`rtspsrc`(RTSP源)用于接收RTSP输入,`appsrc`(应用程序源)作为数据缓冲区,以及`avenc_h264`(H.264编码器)用于压缩视频,最后通过`rtph264pay`封装成RTSP协议并推送到`multicast`或`udp`等网络端口。 ```python gst-launch-1.0 \ rtspsrc location=<your_rtspt_url> ! queue ! appsrc name=source is-live=true do-timestamp=true block=true \ source. ! videoconvert ! h264parse ! avenc_h264 ! rtph264pay config-interval=1 pt=96 ! udpsink host=<your_ip_address> port=<your_port> ``` 替换 `<your_rtspt_url>` 为实际的RTSP源地址,`<your_ip_address>` 和 `<your_port>` 分别为你希望发布的服务器IP和端口。 3. **OpenCV Write**: 在Python中,可以使用OpenCV的VideoWriter将处理过的帧写入到这个Gstreamer管道。你需要获取Gstreamer输出的流,并将其传递给VideoWriter的`fourcc`, `width`, `height`, 和`fps`参数。 ```python import gi gi.require_version('Gst', '1.0') from gi.repository import Gst, GObject # 初始化Gstreamer pipeline_str = ... # 上述Gstreamer pipeline字符串 pipeline = Gst.parse_launch(pipeline_str) sink = pipeline.get_by_name('udpsink') # 获取Gstreamer输出的流信息 caps = sink.get_static_pad("sink").query_caps(Gst.Caps.from_string("application/x-rtp")) stream_format = caps.get_structure(0).get_value("format") # 使用OpenCV的VideoWriter fourcc = {'I420': cv2.VideoWriter_fourcc(*'YUV420P'), 'NV12': cv2.VideoWriter_fourcc(*'NV12')}[stream_format] out = cv2.VideoWriter('<output_file>', fourcc, <fps>, (<video_width>, <video_height>)) ... # 处理每一帧,然后写入out frame = process_frame(frame) # 你的处理函数 out.write(frame) ... # 结束时记得关闭VideoWriter out.release() pipeline.set_state(Gst.State.NULL) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值