MP4或WMV中记录WebCam视频

MP4或WMV中记录WebCam视频

本文介绍了如何使用C#桌面应用程序在MPEG,AVI,MP4或WMV中记录WebCam视频。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发桌面应用程序,它要求我连接到网络摄像头并以MPEG,AVI,MP4和WMV格式录制(保存)视频并刻录到CD / DVD中。该应用程序是在Win Forms中。我对基于Web的Flash / Silver light解决方案不感兴趣我只对免费或开源解决方案或控件感兴趣。



我使用Aforge保存为AVI。净,但它需要更多的尺寸保存(如15sce 320x240视频60-100MB)。我对10sce除了1MB。

C#/ .NET有这样的解决方案吗?

这是代码:

I am working on Desktop Application which requires me to connect to webcam(s) and record(save) the video in MPEG, AVI, MP4 and WMV formats and Burn into the CD/DVD. The application is in Win Forms. I am not interested in Web Based Flash/Silver light solutions I am only interested in free or open source solutions or controls.

I had done saving as AVI using Aforge.Net but its taking more size to save(like 60-100MB for 15sce 320x240 Video ). i am excepting 1MB for 10sce.
Is there such solution available for C#/.NET?
Here is the Code :

using System;
using System.Drawing;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.VFW;

namespace Aforge_Web_Cam
{
    public partial class VideoForm : Form
    {
        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo = null;
        private VideoCaptureDeviceForm captureDevice;
        private Bitmap video;
        private AVIWriter AVIwriter = new AVIWriter();
        private SaveFileDialog saveAvi;

        public VideoForm()
        {
            InitializeComponent();
        }

        private void VideoForm_Load(object sender, EventArgs e)
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            captureDevice = new VideoCaptureDeviceForm();
        }

        private void butStart_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();
                pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
                AVIwriter.Quality = 0;
                AVIwriter.AddFrame(video);
            }
            else
            {
                video = (Bitmap)eventArgs.Frame.Clone();
                pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
            }
        }

        private void butRecord_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;
                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();
                    this.AVIwriter.Close();
                    pbVideo.Image = null;
                }
            }
            else
            {
                this.FinalVideo.Stop();
                this.AVIwriter.Close();
                pbVideo.Image = null;
            }
        }

        private void butCapture_Click(object sender, EventArgs e)
        {
            pbVideo.Image.Save("IMG" + DateTime.Now.ToString("hhmmss") + ".jpg");
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void VideoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FinalVideo == null)
            { return; }
            if (FinalVideo.IsRunning)
            {
                this.FinalVideo.Stop();
                this.AVIwriter.Close();
            }
        }
    }
}

推荐答案


这篇关于如何使用C#桌面应用程序在MPEG,AVI,MP4或WMV中记录WebCam视频。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:04