C# Aforge 简单录制

// 首先用nuget 安装Aforge.video、Aforge.Video.FFMPEGAForge.Video.DirectShow
//在Form1上拖个Button、videoPlayer两个控件,在videoPlayer控件中NewFrame方法

using
AForge.Video; using AForge.Video.DirectShow; using AForge.Video.FFMPEG; using System; using System.Drawing; using System.Windows.Forms; namespace VideoRecordingExample { public partial class Form1 : Form { private VideoCaptureDevice videoDevice; private VideoFileWriter videoWriter; private bool isRecording = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Enumerate video devices FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count > 0) { foreach (FilterInfo device in videoDevices) { comboBoxVideoDevices.Items.Add(device.Name); } comboBoxVideoDevices.SelectedIndex = 0; } } private void buttonStart_Click(object sender, EventArgs e) { if (videoDevice == null) { // Get the selected video device videoDevice = new VideoCaptureDevice(videoDevices[comboBoxVideoDevices.SelectedIndex].MonikerString); // Set the video device's resolution videoDevice.VideoResolution = videoDevice.VideoCapabilities[0]; // Start the video preview videoSourcePlayer.VideoSource = videoDevice; videoSourcePlayer.Start(); } if (isRecording) { // Stop recording isRecording = false; buttonStart.Text = "Start Recording"; videoWriter.Close(); videoWriter.Dispose(); } else { // Start recording isRecording = true; buttonStart.Text = "Stop Recording"; // Create the video file writer videoWriter = new VideoFileWriter(); videoWriter.Open("output.avi", videoDevice.VideoResolution.FrameSize.Width, videoDevice.VideoResolution.FrameSize.Height, 25, VideoCodec.MPEG4); } } private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) { if (isRecording) { // Write the current frame to the video file writer videoWriter.WriteVideoFrame(image); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (videoDevice != null) { // Stop the video device and release resources videoDevice.SignalToStop(); videoDevice.WaitForStop(); videoDevice = null; } if (videoWriter != null) { // Close the video file writer videoWriter.Close(); videoWriter.Dispose(); videoWriter = null; } } } }

 

posted @ 2023-05-05 09:38  夏天的西瓜君  阅读(307)  评论(0编辑  收藏  举报