使用AForge錄製視頻,基於Winform開發
(一)首先導入AForge包
需要先導入
using AForge.Video;
using AForge.Video.FFMPEG;
兩個工具包
(二)初始化需要屬性參數
//初始化設置AForge所需的參數
this.screenWidth = SystemInformation.VirtualScreen.Width;
this.screenHight = SystemInformation.VirtualScreen.Height;
this.frameRate = DEFAULT_FRAME_RATE;
this.isRecording = false;
this.framesCount = default(int);
this.screenArea = Rectangle.Empty;
this.videoWriter = new VideoFileWriter();
this.folderBrowser = new FolderBrowserDialog();
this.videoCodec = (VideoCodec)3;
this.bitRate = 3000000;
(三)開始錄製
//寫入當前電腦上的所有屏幕(可能會有雙屏或多屏)
foreach (Screen screen in Screen.AllScreens)
{
this.screenArea = Rectangle.Union(this.screenArea, screen.Bounds);
}
//打開寫入流
this.videoWriter.Open
(this.fileName, this.screenWidth, this.screenHight,
this.frameRate, this.videoCodec, this.bitRate);
//初始化設置參數
InitializeRecordingParameters(AppDomain.CurrentDomain.BaseDirectory + "aaa.avi");
//開始錄製
this.videoStreamer = new ScreenCaptureStream(this.screenArea);
//設置每幀圖像錄製的回調
this.videoStreamer.NewFrame += new NewFrameEventHandler((object senders, NewFrameEventArgs es) =>
{
this.framesCount++;
//寫入到視頻
this.videoWriter.WriteVideoFrame(es.Frame);
});
this.videoStreamer.Start();
(四)結束錄製
//停止錄像
videoStreamer.Stop();
//關閉寫入器
videoWriter.Close();
支持錄製聲音和暫停以及錄製攝像頭,需要的話可以聯繫我
完整Demo可以去Github上下載:
https://github.com/a935368322/Kogel.Record.Test
如有問題也可以加QQ羣討論:
技術羣 710217654