Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on Desktop Application which requires me to connect to webcam(s) and record(save) the video in MPEG, AVI, MP4 and WMV formats and Burn into the CD/DVD. The application is in Win Forms. I am not interested in Web Based Flash/Silver light solutions I am only interested in free or open source solutions or controls.

I had done saving as AVI using Aforge.Net but its taking more size to save(like 60-100MB for 15sce 320x240 Video ). i am excepting 1MB for 10sce.
Is there such solution available for C#/.NET?
Here is the Code :
C#
using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.VFW;

namespace Aforge_Web_Cam
{
    public partial class VideoForm : Form
    {
        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo = null;
        private VideoCaptureDeviceForm captureDevice;
        private Bitmap video;
        private AVIWriter AVIwriter = new AVIWriter();
        private SaveFileDialog saveAvi;

        public VideoForm()
        {
            InitializeComponent();
        }

        private void VideoForm_Load(object sender, EventArgs e)
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            captureDevice = new VideoCaptureDeviceForm();
        }

        private void butStart_Click(object sender, EventArgs e)
        {
            if (captureDevice.ShowDialog(this) == DialogResult.OK)
            {
                VideoCaptureDevice videoSource = captureDevice.VideoDevice;
                FinalVideo = captureDevice.VideoDevice;
                FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                FinalVideo.Start();
            }
        }

        void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            if (butStop.Text == "Stop Record")
            {
                video = (Bitmap)eventArgs.Frame.Clone();
                pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
                AVIwriter.Quality = 0;
                AVIwriter.AddFrame(video);
            }
            else
            {
                video = (Bitmap)eventArgs.Frame.Clone();
                pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
            }
        }

        private void butRecord_Click(object sender, EventArgs e)
        {
            saveAvi = new SaveFileDialog();
            saveAvi.Filter = "Avi Files (*.avi)|*.avi";
            if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
                int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
                AVIwriter.Open(saveAvi.FileName, w, h);
                butStop.Text = "Stop Record";
                //FinalVideo = captureDevice.VideoDevice;
                //FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                //FinalVideo.Start();
            }
        }

        private void butStop_Click(object sender, EventArgs e)
        {
            if (butStop.Text == "Stop Record")
            {
                butStop.Text = "Stop";
                if (FinalVideo == null)
                { return; }
                if (FinalVideo.IsRunning)
                {
                    //this.FinalVideo.Stop();
                    this.AVIwriter.Close();
                    pbVideo.Image = null;
                }
            }
            else
            {
                this.FinalVideo.Stop();
                this.AVIwriter.Close();
                pbVideo.Image = null;
            }
        }

        private void butCapture_Click(object sender, EventArgs e)
        {
            pbVideo.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void VideoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FinalVideo == null)
            { return; }
            if (FinalVideo.IsRunning)
            {
                this.FinalVideo.Stop();
                this.AVIwriter.Close();
            }
        }
    }
}
Posted
Updated 5-May-14 3:35am
v6
Comments
SoMad 5-May-14 1:22am    
Is your goal actually to save the video in all those formats at the same time or are you simply trying to save the video in the format the cameras are sending it in?

Soren Madsen
Pavan Kumar 5-May-14 1:44am    
no, not at same time
the user can save his preferred one and i need it with less memory taking.
Member 14152468 3-Mar-19 13:03pm    
can you share your project to me please? for my thesis, your revise version

1 solution

AVIWriter writes uncompressed full frame video. Use the VideoFileWriter[^] class instead.
 
Share this answer
 
Comments
Pavan Kumar 12-May-14 0:51am    
with video File Writer i got my requirement but i am get unknown exceptions when recording time goes more than 5mins. do you have any idea to record minimum 20mins. thanks for answer.

AccessViolationException was Unhanded : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Dave Kreskowiak 12-May-14 8:36am    
Nope. I've never used the library.
Pavan Kumar 12-May-14 9:12am    
ok fine, i got it with wmv1 codec.
Pavan Kumar 12-May-14 1:02am    
here is my changed code :

private void VideoRecorderForm_Load(object sender, EventArgs e)
{
timerVideo.Start();

VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
captureDevice = new VideoCaptureDeviceForm();
}

private void butStart_Click(object sender, EventArgs e)
{
if (captureDevice.ShowDialog(this) == DialogResult.OK)
{
VideoCaptureDevice videoSource = captureDevice.VideoDevice;
FinalVideo = captureDevice.VideoDevice;
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
}

private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
//writer = new VideoFileWriter();
writer.WriteVideoFrame(video);
}

private void butRecord_Click(object sender, EventArgs e)
{
try
{
path = Path.GetDirectoryName(Application.ExecutablePath) + @"\Videos\";
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}

int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;

writer.Open(path + "VID" + DateTime.Now.ToString("hhmmss") + ".mp4", w, h, 25, VideoCodec.MPEG4);

//writer.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void butStop_Click(object sender, EventArgs e)
{
if (FinalVideo == null)
{ return; }
if (FinalVideo.IsRunning)
{
writer.Close();
pbVideo.Image = null;
}
else
{
this.FinalVideo.Stop();
writer.Close();
pbVideo.Image = null;
}
}
Member 11112880 28-Sep-14 18:21pm    
Hi!
First, thank you for your first code. It's very useful.
But i am new in c#. When i use first code, my avi file is 0 KB and 1 sec. When i use VideoFileWriter() , ı get this mistake: "An unhandled exception of type 'System.IO.FileNotFoundException' " Can you share solve?
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900