如何使用C#Aforge.NET框架同时保存和显示视频?
我能够将网络摄像头或任何其他集成设备中的视频显示到picturebox中。此外,我能够保存到一个avi的视频文件使用FFMPEG动态链接库文件。 我想同时做这两件事,即保存在avi文件的视频,以及在同一时间显示现场饲料太。 这是一个监控项目,我想监控直播并保存它们如何使用C#Aforge.NET框架同时保存和显示视频?,c#,ffmpeg,aforge,video-recording,C#,Ffmpeg,Aforge,Video Recording,我能够将网络摄像头或任何其他集成设备中的视频显示到picturebox中。此外,我能够保存到一个avi的视频文件使用FFMPEG动态链接库文件。 我想同时做这两件事,即保存在avi文件的视频,以及在同一时间显示现场饲料太。 这是一个监控项目,我想监控直播并保存它们 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Video.VFW;
using System.Drawing.Imaging;
using System.IO;
namespace cam_aforge1
{
public partial class Form1 : Form
{
private bool DeviceExist = false;
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource = null;
public Form1()
{
InitializeComponent();
}
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture device on your system");
}
}
private void rfsh_Click(object sender, EventArgs e)
{
getCamList();
}
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrameSave);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
label2.Text = "Device running...";
start.Text = "&Stop";
timer1.Enabled = true;
}
else
{
label2.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
timer1.Enabled = false;
CloseVideoSource();
label2.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = img;
}
Bitmap imgsave;
private void video_NewFrameSave(object sender, NewFrameEventArgs eventArgs)
{
imgsave = (Bitmap)eventArgs.Frame.Clone();
}
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseVideoSource();
}
private void Form1_Load(object sender, EventArgs e)
{
}
VideoFileWriter writer;
private void button1_Click(object sender, EventArgs e)
{
int width = 640;
int height = 480;
writer = new VideoFileWriter();
writer.Open("test.avi", width, height, 75, VideoCodec.MPEG4);
for (int i = 0; i < 5000; i++)
{
writer.WriteVideoFrame(imgsave);
}
}
private void button2_Click(object sender, EventArgs e)
{
writer.Close();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用大屏幕视频;
使用forge.Video.DirectShow;
使用forge.Video.FFMPEG;
使用forge.Video.VFW;
使用系统、绘图、成像;
使用System.IO;
名称空间cam_Aforme1
{
公共部分类Form1:Form
{
私有布尔设备存在=错误;
专用过滤器和视频采集设备;
私有VideoCaptureDevice videoSource=null;
公共表格1()
{
初始化组件();
}
私有void getCamList()
{
尝试
{
videoDevices=新的FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
如果(videoDevices.Count==0)
抛出新的ApplicationException();
DeviceExist=true;
foreach(视频设备中的FilterInfo设备)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex=0;//使数据失败到第一个cam
}
捕获(ApplicationException)
{
DeviceExist=false;
comboBox1.Items.Add(“您的系统上没有捕获设备”);
}
}
私有无效rfsh_单击(对象发送方,事件参数e)
{
getCamList();
}
私有无效开始\单击(对象发送者,事件参数e)
{
如果(start.Text==“&start”)
{
如果(设备存在)
{
videoSource=新的VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonitorString);
videoSource.NewFrame+=新的NewFrameEventHandler(视频\新帧);
videoSource.NewFrame+=newnewframeeventhandler(video\u NewFrameSave);
CloseVideoSource();
videoSource.DesiredFrameSize=新大小(160,120);
//videoSource.DesiredFrameRate=10;
videoSource.Start();
标签2.Text=“设备正在运行…”;
start.Text=“&Stop”;
timer1.Enabled=true;
}
其他的
{
label2.Text=“错误:未选择任何设备。”;
}
}
其他的
{
if(videoSource.IsRunning)
{
timer1.Enabled=false;
CloseVideoSource();
label2.Text=“设备已停止。”;
start.Text=“&start”;
}
}
}
私有void video_NewFrame(对象发送方,NewFrameEventArgs eventArgs)
{
位图img=(位图)eventArgs.Frame.Clone();
pictureBox1.Image=img;
}
位图imgsave;
私有void video_NewFrameSave(对象发送方,NewFrameEventArgs eventArgs)
{
imgsave=(位图)eventArgs.Frame.Clone();
}
私有void CloseVideoSource()
{
如果(!(视频源==null))
if(videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource=null;
}
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
label2.Text=“设备正在运行…”+videoSource.FramesReceived.ToString()+“FPS”;
}
私有void Form1\u FormClosed(对象发送方,FormClosedEventArgs e)
{
CloseVideoSource();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
视频文件编写器;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
整数宽度=640;
内部高度=480;
writer=新的VideoFileWriter();
writer.Open(“test.avi”,宽度,高度,75,VideoCodec.MPEG4);
对于(int i=0;i<5000;i++)
{
WriteVideoFrame(imgsave);
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
writer.Close();
}
}
}
提前谢谢