EmguCV VideoWriter创建了巨大的保存文件?

时间:2017-06-08 16:56:48

标签: c# video-capture emgucv video-encoding webcam-capture

使用以下代码我正在录制并将视频从网络摄像头保存到磁盘。但即使是3秒钟的视频也会保存,文件大小约为50 MB。我假设我误用了VideoWriter类。有什么建议?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;

namespace EmguCV_Webcam
{
    public partial class Form1 : Form
    {

        #region Private variables

        private Capture currentDevice;
        private VideoWriter videoWriter;
        private bool recording;
        private int videoWidth;
        private int videoHeight;

        #endregion

        #region Constructors

        public Form1()
        {
            InitializeComponent();
            InitializeVariables();
            AttachButtonMacros();
            StartVideoFeed();
        }


        #endregion

        #region Methods

        private void InitializeVariables()
        {
            currentDevice = new Capture(0);
            recording = false;
            videoWidth = currentDevice.Width;
            videoHeight = currentDevice.Height;            
        }

        private void StartVideoFeed()
        {
            currentDevice.Start();
            currentDevice.ImageGrabbed += CurrentDevice_ImageGrabbed;
        }

        private void AttachButtonMacros()
        {
            StartRecordingButton.Click += StartRecordingButton_Click;
            StopRecordingButton.Click += StopRecordingButton_Click;
        }

        private void CurrentDevice_ImageGrabbed(object sender, EventArgs e)
        {
            Mat m = new Mat();
            currentDevice.Retrieve(m);
            VideoPictureBox.Image = m.ToImage<Bgr, byte>().Bitmap;
            if (recording && videoWriter != null)
            {
                videoWriter.Write(m);
            }
        }

        private void StartRecordingButton_Click(object sender, EventArgs e)
        {
            recording = true;
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = ".avi";
            dialog.AddExtension = true;
            dialog.FileName = DateTime.Now.ToString();
            DialogResult dialogResult = dialog.ShowDialog();
            if(dialogResult != DialogResult.OK)
            {
                return;
            }
            videoWriter = new VideoWriter(dialog.FileName, 30, new Size(videoWidth, videoHeight), true);
        }

        private void StopRecordingButton_Click(object sender, EventArgs e)
        {
            recording = false;
            if(videoWriter != null)
            {
                videoWriter.Dispose();
            }           
        }

        #endregion

    }
}

1 个答案:

答案 0 :(得分:1)

你应该使用压缩。试试这个

VideoWriter.Fourcc('M', 'P', '4', 'V')

所以改变

videoWriter = new VideoWriter(dialog.FileName, 30, new Size(videoWidth, videoHeight), true); 

videoWriter = new VideoWriter(dialog.FileName, VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new Size(videoWidth, videoHeight), true);