如何使用AForge将IP摄像机实时源保存到avi文件?

时间:2014-05-13 21:30:17

标签: c# video

首先,对不起,如果这个帖子的位置错误,我通常不会在论坛上发帖。我一直陷入困境,无法弄明白,所以我终于决定寻求帮助了。另外,我对C#很新。

好的,所以我有一个来自IP摄像头的直播,我想保存到avi文件。这是我的代码,由于我已经尝试了很多东西,所以很多东西都被注释掉了。代码确实打开了流,因此我可以查看它,并创建一个我选择的名称的avi文件。但是,当您打开该avi文件时,无论您选择多长时间,它都只是黑色,在这种情况下为40秒。我认为我的主要问题是不知道在这段代码中添加的位置:     int width = 320;     int height = 240;

// create instance of video writer
VideoFileWriter writer = new VideoFileWriter( );
// create new video file
writer.Open( "test.avi", width, height, 25, VideoCodec.MPEG4 );
// create a bitmap to save into the video file
Bitmap image = new Bitmap( width, height, PixelFormat.Format24bppRgb );
// write 1000 video frames
for ( int i = 0; i < 1000; i++ )
{
    image.SetPixel( i % width, i % height, Color.Red );
    writer.WriteVideoFrame( image );
}
writer.Close( );

我尝试了很多东西而且只是卡住了!有人可以帮忙吗?这是我的代码,注释掉的东西是我只是尝试一下东西,看看有什么东西可以这么说:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;
using AForge.Video.VFW;
using AForge.Video.FFMPEG;

namespace _11GVideoFeed
{
    public partial class Form1 : Form
    {
        private MJPEGStream jpegSource1 = new MJPEGStream("IpCameraFeed");
        VideoFileWriter writer = new VideoFileWriter();

        private void Form1_Load(object sender, System.EventArgs e)
        {
            jpegSource1.Login = "login";
            jpegSource1.Password = "password";
            Player.VideoSource = jpegSource1;
            //Player.VideoSource.Start();

            //VideoFileWriter writer = new VideoFileWriter();
            //Create new AVI file and open it
            //writer.Open("test102.avi", 320, 240, 25, VideoCodec.MPEG4);
            //Create frame image

            //Bitmap image1 = new Bitmap(320, 240);

            //for (int i = 0; i < 1000; i++)
            //{
            //    //image1.SetPixel(i % 320, i % 240, Color.Red);
            //    //add the image as a new frame of video file
            //    writer.WriteVideoFrame(image1);

            //}
            //writer.Close();


        }

        private void Form1_FormClosing(object sender,System.Windows.Forms.FormClosedEventArgs e)
        {
            Player.VideoSource.Stop();
            jpegSource1.Stop();
        }

        private void jpegSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            Bitmap FrameData = new Bitmap(eventArgs.Frame);
            Player.BackgroundImage = FrameData;

        }

        private void jpegSource_VideoSourceError(object sender, AForge.Video.VideoSourceErrorEventArgs eventArgs)
        {
            Debug.WriteLine(eventArgs.Description);
        }

        public Form1()
        {
            InitializeComponent();
            //this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            this.Load += new EventHandler(Form1_Load);
            jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource_NewFrame);
            jpegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource_VideoSourceError);


        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            Player.VideoSource.Start();
            writer.Open("test1000.avi", 320, 240, 25, VideoCodec.MPEG4);
            Bitmap image1 = new Bitmap(320, 240);

            for (int i = 0; i < 1000; i++)
            {
                //image1.SetPixel(i % 320, i % 240, Color.Red);
                //add the image as a new frame of video file
                writer.WriteVideoFrame(image1);

            }
            //writer.Close();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            Player.VideoSource.Stop();
            writer.Close();
        }


    }
}

0 个答案:

没有答案
相关问题