【问题标题】:c# AForge.NET - how to save video from camera to filec# AForge.NET - 如何将视频从相机保存到文件
【发布时间】:2013-09-03 14:09:07
【问题描述】:

我需要一些帮助,将视频从笔记本电脑摄像头保存到文件中。现在我使用代码在pictureBox 上搜索视频设备和以前的视频。 我尝试过使用 AVIFileWrite,但失败了。 如何使用 VideoFileWriter 来实现我的目标??

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 System.IO;
using AForge.Video.VFW;
using System.Drawing.Imaging;

namespace VideoCapture
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo;
        public static Bitmap _latestFrame;
        //private VideoFileWriter writer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            VideoCaptureDevices = new                  FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
        {
            comboBox1.Items.Add(VideoCaptureDevice.Name);
        }
        comboBox1.SelectedIndex = 0;
    }

    private void button1_Click(object sender, EventArgs e)
    {

        FinalVideo = new    VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
        FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
        FinalVideo.Start();
    }
    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(@"c:\video.avi", width, height, 25, VideoCodec.Default, 1000000);

        //for (int i = 0; i < 1000; i++)
        //{
        //    writer.WriteVideoFrame(video);
        //}

        //writer.Close();

        //Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        //writer.WriteVideoFrame(image);                      
        //MessageBox.Show("jest");           
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (FinalVideo.IsRunning)
        {
            FinalVideo.Stop();

        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (FinalVideo.IsRunning == true) FinalVideo.Stop();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = "Device running..." + FinalVideo.FramesReceived.ToString() + " FPS";
    }

}

}

【问题讨论】:

    标签: c# video avi aforge


    【解决方案1】:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using AForge.Video;
    using AForge.Video.DirectShow;
    using AForge.Video.FFMPEG;
    using AForge.Video.VFW;
    
    namespace WindowsFormsApplication12
    {
        public partial class Form1 : Form
        {
            private FilterInfoCollection VideoCaptureDevices;
    
            private VideoCaptureDevice FinalVideo = null;
            private VideoCaptureDeviceForm captureDevice;
            private Bitmap video;
            //private AVIWriter AVIwriter = new AVIWriter();
            private VideoFileWriter FileWriter = new VideoFileWriter();
            private SaveFileDialog saveAvi;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
    
                VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                captureDevice = new VideoCaptureDeviceForm();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (captureDevice.ShowDialog(this) == DialogResult.OK)
                {
    
                    //VideoCaptureDevice videoSource = captureDevice.VideoDevice;
                    FinalVideo = captureDevice.VideoDevice;
                    FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                    FinalVideo.Start();
                }
            }
            void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
            {
                if (butStop.Text == "Stop Record")
                {
                    video = (Bitmap)eventArgs.Frame.Clone();
                    pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
                    //AVIwriter.Quality = 0;
                    FileWriter.WriteVideoFrame(video);
                    //AVIwriter.AddFrame(video);
                }
                else
                {
                    video = (Bitmap)eventArgs.Frame.Clone();
                    pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                saveAvi = new SaveFileDialog();
                saveAvi.Filter = "Avi Files (*.avi)|*.avi";
                if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
                    int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
                    FileWriter.Open(saveAvi.FileName, w, h, 25, VideoCodec.Default, 5000000);
                    FileWriter.WriteVideoFrame(video);
    
                    //AVIwriter.Open(saveAvi.FileName, w, h);
                    butStop.Text = "Stop Record";
                    //FinalVideo = captureDevice.VideoDevice;
                    //FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                    //FinalVideo.Start();
                }
            }
    
            private void butStop_Click(object sender, EventArgs e)
            {
                if (butStop.Text == "Stop Record")
                {
                    butStop.Text = "Stop";
                    if (FinalVideo == null)
                    { return; }
                    if (FinalVideo.IsRunning)
                    {
                        //this.FinalVideo.Stop();
                        FileWriter.Close();
                        //this.AVIwriter.Close();
                        pictureBox1.Image = null;
                    }
                }
                else
                {
                    this.FinalVideo.Stop();
                    FileWriter.Close();
                    //this.AVIwriter.Close();
                    pictureBox1.Image = null;
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                pictureBox1.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (FinalVideo == null)
                { return; }
                if (FinalVideo.IsRunning)
                {
                    this.FinalVideo.Stop();
                    FileWriter.Close();
                    //this.AVIwriter.Close();
                }
            }
    
    
        }
    }
    

    这样;)

    【讨论】:

    • 我正在使用您的代码。有用!!但我有这个问题:如果我使用笔记本电脑集成网络摄像头没问题。如果我选择外部 USB 摄像头,图像是黑色的。你有什么想法吗?
    • 您应该设置正确的按钮名称。会有帮助的
    【解决方案2】:

    这不是一个明确的答案,但是:每次调用 writer.Open(...) 时,它都会使用 Win32.OpenFileMode.Create 而不是 Win32.OpenFileMode.ReadWrite,因此它每次都会覆盖文件而不是添加帧,因此您的结果将是包含 1 帧的视频。我也有同样的问题:my page

    【讨论】:

    • 因为IPcamera没有稳定的帧率,而且作者没有动态的FPS,你可以更好地使用VLC。
    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2015-01-06
    相关资源
    最近更新 更多