C#+AForge 實現視頻錄制
? 在C#中,使用AForge 庫實現視頻錄制功能是一個比較直接的過程。AForge 是一個開源的.NET框架,提供了許多用于處理圖像和視頻的類庫。
開發步驟
安裝AForge庫
? 首先,確保你的項目中已經安裝了 AForge.Video
和AForge.Video.FFMPEG
.你可以通過NuGet包管理器來實現安裝這些庫:
- 打開你的Visual Studio 項目
- 轉到工具->NuGet包管理器->管理解決方案的NuGet包
- 搜索并安裝
AForge
和AForge.Video
以及AForge.Video.FFMPEG
打開與關閉攝像頭
/// <summary>
/// 打開攝像頭
/// </summary>
private void OpenCamera()
{//設置視頻源并啟動this.videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[this.comboBox1.SelectedIndex].MonikerString);this.videoSourcePlayer1.VideoSource = this.videoCaptureDevice;this.videoSourcePlayer1.Start();isOpen = true;this.btnOpen.Text = "關閉";
}/// <summary>
/// 關閉攝像頭
/// </summary>
private void CloseCamera()
{if (this.videoSourcePlayer1.VideoSource != null){this.videoSourcePlayer1.SignalToStop();this.videoSourcePlayer1.WaitForStop();this.videoSourcePlayer1.VideoSource = null;isOpen = false;this.btnOpen.Text = "打開";}
}
實現拍照
/// <summary>
/// 拍照
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{if (!this.isOpen){MessageBox.Show("請先打開攝像頭!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);return;}try{//拍照bmp = this.videoSourcePlayer1.GetCurrentVideoFrame();this.pictureBox1.Image = bmp;if (!Directory.Exists("images"))Directory.CreateDirectory("images");string fileName = string.Format("images/圖片_{0}.jpg", DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));bmp.Save(fileName);}catch (Exception ex){MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);}
}
實現錄像
/// <summary>
/// 開始錄像
/// </summary>
private void StartRecord()
{if (!Directory.Exists("videos"))Directory.CreateDirectory("videos");string fileName = string.Format("videos/錄像_{0}.avi", DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));this.videoFileName = fileName;// 創建視頻文件寫入器videoWriter = new VideoFileWriter();videoSourcePlayer1.NewFrame += VideoSourcePlayer1_NewFrame;videoWriter.Open(fileName, 1280, 960, 30, VideoCodec.MPEG4, 1000000); // 設置分辨率、幀率和比特率
}/// <summary>
/// 停止錄像
/// </summary>
private void StopRecord()
{this.CloseCamera();if (videoWriter != null){videoWriter.Close();videoWriter = null;}this.FlushVideoPlayList();
}
實現錄屏
/// <summary>
/// 開始錄屏
/// </summary>
/// <param name="path"></param>
public void StartRecording(string path) //該方法需要有一個路徑參數,前面我們設置過了
{var directory = Path.GetDirectoryName(path);if (!Directory.Exists(directory)) //檢查路徑是否存在,不存在就自動創建{Directory.CreateDirectory(directory);}try{FrmMain.videoWriter = new VideoFileWriter();//用于生成視頻文件,參數分別為路徑、視頻的寬度、高度、幀數、編碼格式以及視頻碼率FrmMain.videoWriter.Open(path, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, 25, VideoCodec.MPEG4, 5000000);recordingThread = new Thread(RecordScreen);recordingThread.Start();}catch (Exception err){Console.WriteLine(err.ToString());MessageBox.Show("屏幕錄制開啟失敗: " + err.Message, "提示!");}
}/// <summary>
/// 停止錄屏
/// </summary>
public void StopRecording()
{try{recordingThread.Join();FrmMain.videoWriter.Close();}catch (Exception err){MessageBox.Show("屏幕錄制停止失敗: " + err.Message, "提示!");Console.WriteLine(err.ToString());}
}
視頻播放
/// <summary>
/// 雙擊視頻列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{if (this.listBox1.SelectedItem == null) return;this.axWindowsMediaPlayer1.URL = string.Format("videos/{0}", this.listBox1.SelectedItem.ToString());
}
object sender, MouseEventArgs e)
{
if (this.listBox1.SelectedItem == null) return;
this.axWindowsMediaPlayer1.URL = string.Format("videos/{0}", this.listBox1.SelectedItem.ToString());
}
# 源碼下載
CSDN: [C#+AForge 實現視頻下載](https://download.csdn.net/download/m0_37631902/90477114?spm=1001.2014.3001.5503)