C# OpenCvSharp读取rtsp流录制mp4可分段保存

2023-12-28 16:57:12

软件界面:

测试环境:

VS2019

.NET Framework 4.7.2

OpencvSharp4.8.0

输入RTSP流地址即可拉取RTSP流,支持抓拍和录制RTSP流视频,且支持支持按固定时间保存,比如我想5分钟保存一个视频,设置保存间隔为300,代码已经封装成类模块,调用十分简单?,代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FIRC;
using OpenCvSharp;

namespace RtspDemo
{
    public partial class Form1 : Form
    {
        RtspManager rm = new RtspManager();
        bool startRecordVideo = false;
        int totalSaveFrameCount = 0;
        VideoManager vm = null;
        int startIndex = 0;

        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            if(btn_connect.Text=="连接")
            {
                if (string.IsNullOrEmpty(tb_rtsp.Text))
                {
                    tssl_show.Text = "rtsp流地址错误!";
                    return;
                }
                rm.Connect(tb_rtsp.Text);
                if (rm.Connected)
                {
                    tssl_show.Text = "连接成功";
                    btn_connect.Text = "停止";
                    rm.Start();
                }
                else
                {
                    tssl_show.Text = "连接失败";
                }

            }
            else
            {
                rm.Stop();
                rm.DisConnect();
                btn_connect.Text = "连接";
                tssl_show.Text = "连接中断!";
            }
           

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            rm.GetImage += Rm_GetImage;
            rm.GetStatus += Rm_GetStatus;
        }

        private void Rm_GetStatus(string msg)
        {
            if(msg=="disconnect")
            {
                btn_connect.Text = "连接";
                btn_video.Text = "开始录像";
                vm.Dispose();
            }
        }

        private void Rm_GetImage(OpenCvSharp.Mat frame)
        {
            if(startRecordVideo)
            {
                startIndex++;
                if(startIndex>=totalSaveFrameCount)
                {
                    startIndex = 0;
                    var saveFile = GetTimeString() + ".mp4";
                    vm = new VideoManager(saveFile, FourCC.MP4V, rm.Fps, rm.Width, rm.Height);
                }
                vm.WriteFrame(frame);
            }
            pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(frame);
        }

        public string GetTimeString()
        {
            return DateTime.Now.ToString("yyyyMMdd-HHmmss_fff");
        }
        private void btn_pic_Click(object sender, EventArgs e)
        {
            if(pictureBox1.Image!=null)
            {
                var saveFile = GetTimeString() + ".jpg";
                pictureBox1.Image.Save(saveFile,ImageFormat.Jpeg);
                tssl_show.Text = "抓拍成功,保存为" + saveFile;
            }
        }

        private void btn_video_Click(object sender, EventArgs e)
        {
            if(btn_video.Text=="开始录像")
            {
                if(btn_connect.Text=="连接")
                {
                    tssl_show.Text = "请先连接RTSP流!";
                    return;
                }
                btn_video.Text = "停止录像";
                btn_video.Update();
                Task.Run(() => {
                totalSaveFrameCount = Convert.ToInt32(tb_interval.Text) * rm.Fps;
                startIndex = 0;
                var saveFile = GetTimeString() + ".mp4";
                vm = new VideoManager(saveFile,FourCC.MP4V,rm.Fps,rm.Width,rm.Height);
                startRecordVideo = true;
               
                });

            }
            else
            {
                vm.Dispose();
                startRecordVideo = false;
                btn_video.Text ="开始录像";
            }
        }
    }
}

视频演示:

C# OpenCvSharp读取rtsp流录制mp4_哔哩哔哩_bilibiliC# OpenCvSharp读取rtsp流录制mp4, 视频播放量 1、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 未来自主研究中心, 作者简介 未来自主研究中心,相关视频:随机地址生成工具1.3.3使用教程,xml转txt工具终结版1.3.1使用教程voc转yolo工具软件教程,相似图片删除工具使用教程快速删除类似图片软件,使用易语言部署yolov8的tensorrt模型,IDM6.422【毛子重新打包版】免激活直装,小白友好,pyqt5离线安装工具IDLE教程自动配置desginer pyuic到pycharm,轻松教你学会如何内置QQ模块~,OCR区域自动识别重命名工具1.1使用教程,实例分割语义分割数据集自动预标注反标注系统之图片自动标注视频演示,给IDLE或anaconda离线安装pyqt5工具教程傻瓜式操作离线安装pyqt5自动配置designer到pycharmicon-default.png?t=N7T8https://www.bilibili.com/video/BV1AG411r7fW/?vd_source=989ae2b903ea1b5acebbe2c4c4a635ee

源码下载地址:

https://download.csdn.net/download/FL1623863129/88673726

文章来源:https://blog.csdn.net/FL1623863129/article/details/135271320
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。