繁体   English   中英

在C#中使用Aforge框架录制视频?

[英]Recording Video using Aforge framework in C#?

我想记录通过USB端口连接到PC的任何视频捕获设备的视频。 我在c#中使用Aforge框架。 我成功地在应用程序中显示了视频,并且我也想将其录制到硬盘上。

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();
    }*/
}
}

但这没有提供任何视频文件输出。 请事先帮助

我不确切知道这是否是问题,但是您两次声明了VideoFileWriter,另一个错误是将其放在video_NewFrame事件中,这意味着在每个帧中都创建,打开和关闭VideoFileWriter。

您最好一次实例化VideoFileWriter

VideoFileWriter writer = new VideoFileWriter();

然后创建一个用于保存的按钮,并实例化您要开始的保存

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

然后在新框架事件中

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

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

最后创建一个按钮,用于停止保存视频

writer.Close();

对不起,我希望你明白我的意思。 我这里英语不好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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