首页 新闻 搜索 专区 学院

C# RTSP 录制视频产生的文件大小 0 kb

0
悬赏园豆:5 [已关闭问题] 关闭于 2020-11-13 14:07

做了个从相机读取RTSP流,并保存成MP4视频的软件,但是保存的视频大小是0KB.Emgu的版本应该不高,3.0.0.****

工具类代码

    /// <summary>
    /// RTSP视频的工具类
    /// </summary>
    public class RTSPUtils
    {
        /// <summary>
        /// 传过来的播放视频的组件
        /// </summary>
        private PictureBox pictureBox;
        /// <summary>
        /// 相机信息
        /// </summary>
        private CameraConfig camera;
        /// <summary>
        /// 用于播放视频的Emgu对象
        /// </summary>
        private Capture capture;
        /// <summary>
        /// Emgu 的视频流写
        /// </summary>
        private VideoWriter videoWriter = null;
        /// <summary>
        /// 视频尺寸
        /// </summary>
        private int videoWidth;
        private int videoHeight;
        /// <summary>
        /// 是否在录制
        /// </summary>
        private bool isRecord =false;

        public RTSPUtils(PictureBox pictureBox, CameraConfig camera)
        {
            this.pictureBox = pictureBox;
            this.camera = camera;
        }

        /// <summary>
        /// 打开视频
        /// </summary>
        /// <returns></returns>
        public bool OpenVideo()
        {
            try
            {
                capture = new Capture(camera.rtspUrl);
                capture.ImageGrabbed += capture_ImageGrabbed;

                capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 0);
                capture.Start();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// ImageGrabbed事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void capture_ImageGrabbed(object sender,EventArgs e)
        {
            try
            {
                Mat mat = new Mat();
                capture.Retrieve(mat, 0);
                pictureBox.Image = mat.Bitmap;
                if (isRecord && videoWriter != null)
                {
                    videoWriter.Write(mat);
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 关闭视频
        /// </summary>
        /// <returns></returns>
        public bool CloseVideo()
        {
            try
            {
                capture.Stop();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 开始录制
        /// </summary>
        public bool StartRecord(string file)
        {
            if (capture != null)
            {
                videoWidth = capture.Width;
                videoHeight = capture.Height;
                isRecord = true;
                videoWriter = new VideoWriter(file, VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 停止录制
        /// </summary>
        public void StopRecord()
        {
            isRecord = false;
            if (videoWriter != null)
            {
                videoWriter.Dispose();
                videoWriter = null;
            }
        }
    }


echo_lovely的主页 echo_lovely | 小虾三级 | 园豆:662
提问于:2020-11-12 15:35
< >
分享
所有回答(1)
0

Emgu.dll的问题

echo_lovely | 园豆:662 (小虾三级) | 2020-11-13 14:06

换个版本的dll

支持(0) 反对(0) echo_lovely | 园豆:662 (小虾三级) | 2020-11-13 14:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册