使用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