一、右键项目—管理NuGet程序包
二、安装AForge、AForge.Imaging、AForge.Controls、AForge.Math、AForge.Video、AForge.Video.DirectShow、Accord、Accord.Video、Accord.Video.FFMPEG包。
三、使用AForge Controls中VideoSourcePlayer控件
(1)引用WindowsFormsIntegration
(2)添加System.Windows.Forms.Integration命名空间
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
(3)添加AForge.Controls命名空间
xmlns:aforge="clr-namespace:AForge.Controls;assembly=AForge.Controls"
(4)将区域划分四块,分别添加VideoSourcePalyer控件、Image控件
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<!-- 50% of screen height -->
<RowDefinition Height="0.5*"/>
<!-- 50% of screen height -->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<!-- 50% of screen width -->
<ColumnDefinition Width="0.5*"/>
<!-- 50% of screen width -->
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Grid.Row="0" Background="Black">
<wfi:WindowsFormsHost Background="Black">
<aforge:VideoSourcePlayer x:Name="vedioPlayer" />
</wfi:WindowsFormsHost>
</Border>
<Border Grid.Column="1" Grid.Row="0" Background="Black">
<WindowsFormsHost Background="Black">
<aforge:VideoSourcePlayer x:Name="vedioPlayer1" />
</WindowsFormsHost>
</Border>
<Border Grid.Column="0" Grid.Row="1" Background="Black">
<WindowsFormsHost Background="Black">
<aforge:VideoSourcePlayer x:Name="vedioPlayer2" />
</WindowsFormsHost>
</Border>
<Border Grid.Column="1" Grid.Row="1" Background="Black">
<Image HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Border>
</Grid>
四、引用
using System.Drawing;
using System.Diagnostics;
using System.IO;
using AForge;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
五、画面显示,视频录制以及按下“esc”关闭摄像头,结束录制,退出程序。
//检测摄像头视频输入设备列表
private FilterInfoCollection videodevices;
//定义视频流对象
private VideoCaptureDevice[] videoSource=new VideoCaptureDevice[4];
//写入到视频
private VideoFileWriter[] videoWriter=new VideoFileWriter[4];
//视频宽和高
private int vedioWidth = 1280;
private int vedioHeight = 720;
public MainWindow()
{
InitializeComponent();
InitCamera();
InitCamera1();
this.KeyDown += MainWindow_KeyDown;
}
private void InitCamera()
{
//初始化摄像头对象
videodevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//判断是否识别到摄像头设备
if (videodevices.Count == 0)
{
videodevices = null;
return;
}
//通过摄像头名字(设备编号),实例化视频源
videoSource[0] = new VideoCaptureDevice(videodevices[0].MonikerString);
videoSource[0].Start();
string dirpath=System.IO.Path.Combine(Environment.CurrentDirectory,"相机");
if (!Directory.Exists(dirpath))
{
Directory.CreateDirectory(dirpath);
}
string dirpath2 = System.IO.Path.Combine(dirpath, DateTime.Now.ToString("yyyyMMdd_HH_mm_ss"));
if (!Directory.Exists(dirpath2))
{
Directory.CreateDirectory(dirpath2);
}
//string fileName = System.IO.Path.Combine($"{Environment.CurrentDirectory}/相机/{DateTime.Now.ToString("yyyyMMdd")}/DesktopRecord_{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp4");
string fileName = System.IO.Path.Combine(dirpath2, "video1.mp4");
//string outputDirectory = AppDomain.CurrentDomain.BaseDirectory; // 输出目录,这里使用程序的基目录
//string outputVideoPath = System.IO.Path.Combine(outputDirectory, "outputVideo.mp4"); // 输出视频文件路径
videoWriter[0] = new VideoFileWriter();
videoWriter[0].Open(fileName, vedioWidth, vedioHeight, 30, VideoCodec.MPEG4);
vedioPlayer.VideoSource = videoSource[0];
vedioPlayer.NewFrame += VideoSource_NewFrame;
}
private void InitCamera1()
{
//初始化摄像头对象
videodevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//判断是否识别到摄像头设备
if (videodevices.Count == 0)
{
videodevices = null;
return;
}
//通过摄像头名字(设备编号),实例化视频源
videoSource[1]= new VideoCaptureDevice(videodevices[1].MonikerString);
videoSource[1].Start();
string dirpath = System.IO.Path.Combine(Environment.CurrentDirectory, "相机");
if (!Directory.Exists(dirpath))
{
Directory.CreateDirectory(dirpath);
}
string dirpath2 = System.IO.Path.Combine(dirpath, DateTime.Now.ToString("yyyyMMdd_HH_mm_ss"));
if (!Directory.Exists(dirpath2))
{
Directory.CreateDirectory(dirpath2);
}
//string fileName = System.IO.Path.Combine($"{Environment.CurrentDirectory}/相机/{DateTime.Now.ToString("yyyyMMdd")}/DesktopRecord_{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp4");
string fileName = System.IO.Path.Combine(dirpath2, "video2.mp4");
// string outputDirectory = AppDomain.CurrentDomain.BaseDirectory; // 输出目录,这里使用程序的基目录
//string outputVideoPath = System.IO.Path.Combine(outputDirectory, "outputVideo1.mp4"); // 输出视频文件路径
videoWriter[1] = new VideoFileWriter();
videoWriter[1].Open(fileName, vedioWidth, vedioHeight, 15, VideoCodec.MPEG4);
vedioPlayer1.VideoSource = videoSource[1];
vedioPlayer1.NewFrame += VideoSource_NewFrame1;
}
private void VideoSource_NewFrame(object sender, ref System.Drawing.Bitmap image)
{
//image.RotateFlip(RotateFlipType.Rotate180FlipNone);
image.RotateFlip(RotateFlipType.Rotate180FlipY);
vedioWidth = image.Width;
vedioHeight = image.Height;
try
{
//写入到视频
videoWriter[0].WriteVideoFrame(image);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private void VideoSource_NewFrame1(object sender, ref System.Drawing.Bitmap image)
{
//image.RotateFlip(RotateFlipType.Rotate180FlipNone);
image.RotateFlip(RotateFlipType.Rotate180FlipY);
vedioWidth = image.Width;
vedioHeight = image.Height;
try
{
//写入到视频
videoWriter[1].WriteVideoFrame(image);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
for(int i = 0; i < 4; i++)
{
if (videoSource[i] != null && videoSource[i].IsRunning)
{
videoSource[i].SignalToStop();
videoSource[i] = null;
videoWriter[i].Close();
videoWriter[i].Dispose();
videoWriter[i] = null;
}
}
this.KeyDown -= MainWindow_KeyDown;
this.Close();//关闭当前窗口
}
}