輸出為MP4需要用到ffmpeg相關的文件,我打包的庫已經帶了,去官網找的庫可以在這個目錄找到:

2:
添加這些引用:

3:
兩個全局變量:
//用來操作攝像頭 private VideoCaptureDevice Camera = null; //用來把每一幀圖像編碼到視頻文件 private VideoFileWriter VideoOutPut = new VideoFileWriter();
開始代碼:
//獲取攝像頭列表var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice);//實例化設備控制類(我選了第1個)Camera = new VideoCaptureDevice(devs[0].MonikerString);//配置錄像參數(寬,高,幀率,比特率等參數)VideoCapabilities這個屬性會返回攝像頭支持哪些配置,從這里面選一個賦值接即可,我選了第1個Camera.VideoResolution = Camera.VideoCapabilities[0];//設置回調,aforge會不斷從這個回調推出圖像數據Camera.NewFrame += Camera_NewFrame;//打開攝像頭Camera.Start();//打開錄像文件(如果沒有則創建,如果有也會清空),這里還有關于VideoOutPut.Open("E:/VIDEO.MP4",     Camera.VideoResolution.FrameSize.Width,     Camera.VideoResolution.FrameSize.Height,     Camera.VideoResolution.AverageFrameRate,     VideoCodec.MPEG4,     Camera.VideoResolution.BitCount);給AForge輸出圖像數據的回調方法:
//圖像緩存private Bitmap bmp = new Bitmap(1, 1);//攝像頭輸出回調private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs){  //寫到文件  VideoOutPut.WriteVideoFrame(eventArgs.Frame);  lock (bmp)  {    //釋放上一個緩存    bmp.Dispose();    //保存一份緩存    bmp = eventArgs.Frame.Clone() as Bitmap;  }}結束代碼:
//停攝像頭 Camera.Stop(); //關閉錄像文件,如果忘了不關閉,將會得到一個損壞的文件,無法播放 VideoOutPut.Close();
4:
修改App.config,兼容net2.0的一些東西:
<?xml version="1.0" encoding="utf-8"?><configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> </startup> <supportedRuntime version="v2.0.50727"/></configuration>

新聞熱點
疑難解答