基于Aforge的视频录像

时间:2022-07-01 15:58:30

最近做了一个基于Aforge,MMPEG 的 基于摄像头的,视频录像小软件

软件实现的工能如下:

1.自动开机录像。这里自动把摄像头的摄像大小设置成 320 * 320

 try
            {

                Common.Setting set = new Common.Setting();
                XmlSerialize xml = new XmlSerialize();
                set = xml.DeSerialize();
                this.Path = set.Path;
                this.maxSize = set.MaxSize;
                this.frameRate = set.FrameRate;
                try
                {
                    string dic = System.DateTime.Now.AddMonths(-2).ToString("yyyyMMdd") + "\\";
                    string mypath = @Path + dic;
                    System.IO.Directory.Delete(mypath,true);
                }
                catch (Exception ex) { }

                // enumerate video devices
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)
                    throw new ApplicationException();

               
                device = videoDevices[0].MonikerString;

                // create video source
                VideoCaptureDevice videoSource = new VideoCaptureDevice(this.VideoDevice);
                //videoSource.DesiredFrameRate = this.frameRate;
                videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
                this.videoSourcePlayer.Size = new System.Drawing.Size(320, 240);

                // open it
                OpenVideoSource(videoSource);


            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.ToString());
                this.Close();
            }

2. 录像过程

bool StopREC = true;
        bool CreateNewFile = true;
        string myfile = "";
        int FrameCountSame = 0;
        private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            count++;
           
           
            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(image);
           
            SolidBrush drawBrush = new SolidBrush(Color.Red);
           
            Font drawFont = new Font("Arial", 6, FontStyle.Bold, GraphicsUnit.Millimeter);

            int xPos = image.Height - (image.Height - 25);

            int yPos = 3;

            string date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//得到写到屏上的时间
            string dic = System.DateTime.Now.ToString("yyyyMMdd")+"\\";
            string dateFile = System.DateTime.Now.ToString("HH.mm.ss") + ".avi";
           
          

            string mypath = @Path+dic;
           
            System.IO.Directory.CreateDirectory(mypath);

            gr.DrawString(date, drawFont, drawBrush, xPos, yPos);

#region 这里使用 wmv3的格式
 //if (StopREC)
            //{
            //    StopREC = false;

            //    writer = new AVIWriter("wmv3");
            //    writer.FrameRate = 8;
            //    writer.Open(@"d:\log" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".avi", 640, 480);
            //    writer.AddFrame(image);
            //}
            //else
            //{
            //    writer.AddFrame(image);
               
               
            //}
#endregion
            if (count > this.frameRate) { return; }
            if (ifCreateFilePath(myfile)) { CreateNewFile = true; myfile = mypath + dateFile; }
           
            if (StopREC)
            {
                StopREC = false;
                this.CreateNewFile = false;

                int width = 320;
                int height = 240;
                writer1 = new VideoFileWriter();
                writer1.Open(@myfile, width, height, this.frameRate, VideoCodec.MPEG4);
                //writer1.Open(@myfile, width, height);
                try
                {
                
                    writer1.WriteVideoFrame(image);
                }
                catch (Exception ex) {
                    LogErr(ex.ToString());
                }
            
               
            }
            else {
               
                if (this.CreateNewFile)
                {
                    this.CreateNewFile = false;

                    if (writer1 != null)
                    {
                    
                        writer1.Close();
                        writer1.Dispose();
                    }
                    int width = 320;
                    int height = 240;
                    writer1 = new VideoFileWriter();

                    writer1.Open(@myfile, width, height, this.frameRate, VideoCodec.MPEG4);
                    //writer1.Open(@myfile, width, height);
                    writer1.WriteVideoFrame(image);
                   
                }
                else
                {
                   
                    writer1.WriteVideoFrame(image);
                   
                }

               
            }

基本上蛮足需要。

1.通过Aforge 录像时,如果突然关机。不能接上一文件继续录像(我没找到相应办法)。

2.frameRate的问题,我看网上说,要达到 30 时才是最好的,但我的摄像头,在笔记本上就是达不到,只能到 10。所以我在录像时必须设置 这个值为 8 ,否则录像时,不是像快进,就是在放慢动作,而且超过8的帧数不要了(感觉这样不是很对,但先只能这样了)。

上面两个问题记录下来,以后找找资料再处理。