首页 > 代码库 > 使用aforg.net 录制摄像头 附源码

使用aforg.net 录制摄像头 附源码

这一篇在上一篇 使用aforg.net 捕获摄像头 的基础上稍加修改 增加录制功能

录制功能使用AForge.Video.FFMPEG 需要添加对 AForge.Video.FFMPEG.dll的引用 并且拷贝AForge.NET\Framework\Externals\ffmpeg\bin路径下的全部dll到Debug目录下

直接上代码了 对上一篇的代码稍有修改

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace CameraCapture
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection filterInfoCollection;
        private VideoCaptureDevice captureDevice;
 
        //AForge.Video.FFMPEG 提供的视频写入类
        private VideoFileWriter videoFileWriter = new VideoFileWriter();
 
        private VideoCapabilities[] videoCapabilities;
        private string fileName;
        private Size frameSize;
        private int framRate;
 
        public Form1()
        {
            InitializeComponent();
            filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo item in filterInfoCollection)
            {
                this.comboBox1.Items.Add(item.Name);
            }
            this.comboBox1.SelectedIndex = 0;
            //先初始化一下 否则在下面判断是否已运行时会报错
            captureDevice = new VideoCaptureDevice();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.AddExtension = true;
                sfd.DefaultExt = ".avi";
                sfd.Filter = "视频文件|*.avi";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    fileName = sfd.FileName;
                }
 
                //文件名 宽度 高度 帧率 编码
                videoFileWriter.Open(fileName, frameSize.Width, frameSize.Height, framRate, VideoCodec.MPEG4);
            }
            if (captureDevice.IsRunning)
                captureDevice.Stop();
 
            captureDevice.NewFrame += captureDevice_NewFrame;
            captureDevice.Start();
            MessageBox.Show(videoFileWriter.FrameRate.ToString());
        }
 
        private void captureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            this.pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            if (this.checkBox1.Checked)
            {
                videoFileWriter.WriteVideoFrame((Bitmap)eventArgs.Frame);
            }
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetVideoCapabilities();
        }
 
        /// <summary>
        ///获取摄像头分辨率
        /// </summary>
        private void GetVideoCapabilities()
        {
            captureDevice = new VideoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonikerString);
            try
            {
                videoCapabilities = captureDevice.VideoCapabilities;
 
                foreach (VideoCapabilities capabilty in videoCapabilities)
                {
                    if (!this.comboBox2.Items.Contains(capabilty.FrameSize))
                    {
                        this.comboBox2.Items.Add(capabilty.FrameSize);
                    }
                }
                if (videoCapabilities.Length == 0)
                {
                    this.comboBox2.Items.Add("Not supported");
                }
                this.comboBox2.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.frameSize = captureDevice.VideoCapabilities[comboBox2.SelectedIndex].FrameSize;
            this.framRate = captureDevice.VideoCapabilities[comboBox2.SelectedIndex].FrameRate;
        }
 
        /// <summary>
        /// 关闭后结束捕获 释放资源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (captureDevice.IsRunning)
            {
                captureDevice.Stop();
            }
            if (videoFileWriter.IsOpen)
            {
                videoFileWriter.Close();
            }
        }
    }
}

  

源代码下载地址:http://files.cnblogs.com/DragonX/CameraCapture2.zip