简体   繁体   中英

Recording Video using Aforge framework in C#?

I want to record the video from any of the video grabbing devices connected to my PC through USB ports. I am using the Aforge Framework in c#. I am successful in displaying the video in the application and also i want to record the same to my hard disk.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Video.VFW;
using System.Drawing.Imaging;

namespace cam_aforge1
{
public partial class Form1 : Form
{
    //private AVIWriter writer = new AVIWriter("MSVC");
    //Queue<Bitmap> frames = new Queue<Bitmap>();
    VideoFileWriter writer;

    private bool DeviceExist = false;
    private FilterInfoCollection videoDevices;
    private VideoCaptureDevice videoSource = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void getCamList()
    {
        try
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            comboBox1.Items.Clear();
            if (videoDevices.Count == 0)
                throw new ApplicationException();

            DeviceExist = true;
            foreach (FilterInfo device in videoDevices)
            {
                comboBox1.Items.Add(device.Name);
            }
            comboBox1.SelectedIndex = 0; //make dafault to first cam
        }
        catch (ApplicationException)
        {
            DeviceExist = false;
            comboBox1.Items.Add("No capture device on your system");
        }
    }

    private void rfsh_Click(object sender, EventArgs e)
    {
        getCamList();
    }

    private void start_Click(object sender, EventArgs e)
    {
        if (start.Text == "&Start")
        {
            if (DeviceExist)
            {
                videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                CloseVideoSource();
                videoSource.DesiredFrameSize = new Size(160, 120);
                //videoSource.DesiredFrameRate = 10;
                videoSource.Start();
                label2.Text = "Device running...";
                start.Text = "&Stop";
                timer1.Enabled = true;
                videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                videoSource.Start();
            }
            else
            {
                label2.Text = "Error: No Device selected.";
            }
        }
        else
        {
            if (videoSource.IsRunning)
            {
                timer1.Enabled = false;
                CloseVideoSource();
                label2.Text = "Device stopped.";
                start.Text = "&Start";                    
            }
        }
    }
    Bitmap img;
    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        img = (Bitmap)eventArgs.Frame.Clone();
        pictureBox1.Image = img;
        int width = 640;
        int height = 480;

        VideoFileWriter writer = new VideoFileWriter();
        writer.Open(@"d:\\video.avi", width, height, 25, VideoCodec.Default, 1000000);

        for (int i = 0; i < 1000; i++)
        {
            writer.WriteVideoFrame(img);
        }

        writer.Close();


        Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        writer.WriteVideoFrame(image);
        MessageBox.Show("jest");
    }

    private void CloseVideoSource()
    {
        if (!(videoSource == null))
            if (videoSource.IsRunning)
            {
                videoSource.SignalToStop();
                videoSource = null;
            }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS";
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        CloseVideoSource();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {

        //Bitmap video = (Bitmap)eventArgs.Frame.Clone();

        //pictureBox1.Image = video;
        /*int width = 640;
        int height = 480;

        VideoFileWriter writer = new VideoFileWriter();
        writer.Open(@"d:\video.avi", width, height, 25, VideoCodec.Default, 1000000);

        for (int i = 0; i < 1000; i++)
        {
            writer.WriteVideoFrame(img);
        }

        writer.Close();


        Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        writer.WriteVideoFrame(image);                      
        MessageBox.Show("jest");           */
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //FinalVideo_NewFrame();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //writer.Close();
    }

    /*private void DoRecording()
    {
        writer.FrameRate = 25;
        string savingPath=("D:/");
        string videoName = "ICS_";
        writer.Open(savingPath + videoName, 1278, 958);
        DateTime start = DateTime.Now;
        while(DateTime.Now.Subtract(start).Seconds<30)
        {
            if (frames.Count > 0)
            {
                Bitmap image = (Bitmap)pictureBox1.();
                Bitmap bmp = frames.Dequeue();
                writer.AddFrame(image);
                bmp.Dispose();
            }
        }
        writer.Close();
    }*/
}
}

But this is not giving any video file output. Please help Thanks in advance

I don't know exactly if this is the problem but you declare the VideoFileWriter twice and another mistake is you put it inside the video_NewFrame event which means in every frame, you create, open and close the VideoFileWriter.

You better instantiate the VideoFileWriter once

VideoFileWriter writer = new VideoFileWriter();

Then create a button for saving and instantiate that you want to start the saving

writer.Open(@"d:\\video.mp4", width, height, 25, VideoCodec.MPEG4, 1000000);

then in the new frame event

   img = (Bitmap)eventArgs.Frame.Clone();

   for (int i = 0; i < 1000; i++)
        {
            writer.WriteVideoFrame(img);
        }

Lastly create a button for stopping the saving of the video

writer.Close();

Sorry, I hope you understand what i mean. I got a bad english here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM