C#读取RTSP流并且录制显示视频(PictrueBox)

下载Nuget包:EMGU.CV
引用Emgu.CV;

public void InitVideo()
        {
            VideoCapture _capture = new VideoCapture("rtsp://admin:123456@192.168.0.198:554");
            Thread.Sleep(100);
            VideoWriter videoWriter = null;
            Mat fram = new Mat();
            Task.Factory.StartNew(() => {
                while (true)
                {
                    try
                    { 
                        _capture.Read(fram);
                        var item = _capture.QueryFrame();
                        Image Imageshow = item.Bitmap;//获取当前帧图片 发到图片控件中
                        pictureBox1.Image = Imageshow;
                        if (videoWriter == null)
                        {
                            videoWriter = new VideoWriter("out.mp4", 0, 24, fram.Size, false);
                            videoWriter.Set(WriterProperty.Framebytes, 10);
                        }
                        videoWriter.Write(fram);//写入帧

                    }
                    catch (Exception ex)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
            });
        } 

参考资料:
https://www.cnblogs.com/LCLBook/p/16649870.html
另一种使用方法:挂载事件操作(速度稍慢)

在这里插入图片描述

/// <summary>
        /// 初始化程序
        /// </summary>
        private void InitializeVariables()
        {
            currentDevice = new VideoCapture("rtsp://admin:***@192.168.0.198:554");
            recording = false;
            videoWidth = currentDevice.Width;
            videoHeight = currentDevice.Height;
        }
        /// <summary>
        /// 图片转换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentDevice_ImageGrabbed(object sender, EventArgs e)
        {
            try
            {

                Mat m = new Mat();
                currentDevice.Retrieve(m, 0);
                VideoPictureBox.Image = m.Bitmap;
                
                if (recording && videoWriter != null)
                {
                    videoWriter.Write(m);
                }
                Thread.Sleep(10);


            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 确认录像记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTStrat_Click(object sender, EventArgs e)
        {
            recording = true;
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = ".mp4";
            dialog.AddExtension = true;
            dialog.FileName =Guid.NewGuid().ToString("N").Substring(0,5);
            DialogResult dialogResult = dialog.ShowDialog();
            if (dialogResult != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            Task.Factory.StartNew(() =>
            {
                videoWriter = new VideoWriter(dialog.FileName, VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
            });
        }
        /// <summary>
        ///  关闭录像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TBClose_Click(object sender, EventArgs e)
        {
            recording = false;
            if (videoWriter != null)
            {
                currentDevice.ImageGrabbed -= CurrentDevice_ImageGrabbed;//解除事件
                currentDevice.Stop();
                videoWriter.Dispose();
            }
        }
        /// <summary>
        /// 按钮预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            currentDevice.ImageGrabbed += CurrentDevice_ImageGrabbed;
            currentDevice.Start();
        }

参考数据:http://t.zoukankan.com/chengNet-p-11724429.html

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值