2014-03-31 36 views
1

我想記錄通過USB端口連接到PC的任何視頻抓取設備的視頻。我在C#中使用Aforge框架。我成功地在應用程序中顯示視頻,並且我想將其記錄到我的硬盤上。在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

我不知道這是否是問題,但你聲明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(); 

對不起,我希望你明白我的意思。我在這裏遇到了不好的英語