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


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

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

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

    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)
            {
                GetVideoCapabilities();
                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();
            }
    
            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();
                }
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
    
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    

      

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

  • 相关阅读:
    【http】HTTP请求方法 之 OPTIONS
    【javascript基础】函数前面的一元操作符
    【javascript基础】运算符优先级
    【移动互联网开发】Zepto 使用中的一些注意点 【转】
    【jQuery】IE9 jQuery 1.9.1 报 Syntax error,unrecognized expression 错误
    一月收集几个有用的谷歌Chrome插件
    【Sizzle学习】之关于【初探 jQuery 的 Sizzle 选择器】这篇文章里的小bug
    【第三方类库】underscore.js源码---each forEach 每次迭代跟{}比较的疑惑
    vue-cli脚手架npm相关文件解读(2)webpack.prod.conf.js
    vue-cli脚手架npm相关文件解读(1)webpack.base.conf.js
  • 原文地址:https://www.cnblogs.com/DragonX/p/3751305.html
Copyright © 2020-2023  润新知