我想从通过USB端口连接到我的电脑的任何视频抓取设备录制视频。我在c#中使用Aforge框架。我成功地在应用程序中显示视频,并且我想将其记录到我的硬盘中。
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;
namespace cam_aforge1
{
public partial class Form1 : Form
{
//private AVIWriter writer = new AVIWriter("MSVC");
//Queue<Bitmap> frames = new Queue<Bitmap>();
VideoFileWriter writer;
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);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
label2.Text = "Device running...";
start.Text = "&Stop";
timer1.Enabled = true;
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.Start();
}
else
{
label2.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
timer1.Enabled = false;
CloseVideoSource();
label2.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
Bitmap img;
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
img = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = img;
int width = 640;
int height = 480;
VideoFileWriter writer = new VideoFileWriter();
writer.Open(@"d:\\video.avi", width, height, 25, VideoCodec.Default, 1000000);
for (int i = 0; i < 1000; i++)
{
writer.WriteVideoFrame(img);
}
writer.Close();
Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
writer.WriteVideoFrame(image);
MessageBox.Show("jest");
}
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)
{
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
//Bitmap video = (Bitmap)eventArgs.Frame.Clone();
//pictureBox1.Image = video;
/*int width = 640;
int height = 480;
VideoFileWriter writer = new VideoFileWriter();
writer.Open(@"d:\video.avi", width, height, 25, VideoCodec.Default, 1000000);
for (int i = 0; i < 1000; i++)
{
writer.WriteVideoFrame(img);
}
writer.Close();
Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
writer.WriteVideoFrame(image);
MessageBox.Show("jest"); */
}
private void button1_Click(object sender, EventArgs e)
{
//FinalVideo_NewFrame();
}
private void button2_Click(object sender, EventArgs e)
{
//writer.Close();
}
/*private void DoRecording()
{
writer.FrameRate = 25;
string savingPath=("D:/");
string videoName = "ICS_";
writer.Open(savingPath + videoName, 1278, 958);
DateTime start = DateTime.Now;
while(DateTime.Now.Subtract(start).Seconds<30)
{
if (frames.Count > 0)
{
Bitmap image = (Bitmap)pictureBox1.();
Bitmap bmp = frames.Dequeue();
writer.AddFrame(image);
bmp.Dispose();
}
}
writer.Close();
}*/
}
}
但这并没有给出任何视频文件输出。请帮忙 提前致谢
答案 0 :(得分:0)
我不确切地知道这是否是问题,但你声明VideoFileWriter两次,另一个错误是你把它放在video_NewFrame事件中,这意味着在每一帧中,你创建,打开和关闭VideoFileWriter。
您最好一次实例化VideoFileWriter
VideoFileWriter writer = new VideoFileWriter();
然后创建一个按钮,用于保存和实例化您要开始保存
writer.Open(@"d:\\video.mp4", width, height, 25, VideoCodec.MPEG4, 1000000);
然后在新的帧事件中
img = (Bitmap)eventArgs.Frame.Clone();
for (int i = 0; i < 1000; i++)
{
writer.WriteVideoFrame(img);
}
最后创建一个停止保存视频的按钮
writer.Close();
抱歉,我希望你明白我的意思。我这里的英语不好