1

我对视频输入完全陌生,几天前才开始使用 AForge。使用实时视频很舒服,但我现在需要对项目的文件做一些事情。

使用 Windows Media Video 9 VCM 编解码器,保存不是问题。输出文件在我拥有的每个播放器上都能正常工作,但我的程序总是以大约两倍的帧速率播放它。这特别奇怪,因为从来没有任何迹象表明帧速率发生了变化:保存视频的默认值和新播放器都表明帧速率是 25 fps。

我发现的唯一建议是在捕获视频之前更改帧速率,但这似乎无济于事。

在 AVIFileVideoSource 文档中环顾四周,我发现 FrameIntervalFromSource 和 FrameInterval 属性,它们一起应该给我我正在寻找的结果,但我也无法让它们工作。其他一切都是死胡同,我没有想法。这是我用来读取文件的代码:

    public partial class Form1 : Form
{
    AVIReader input = new AVIReader();
    AVIFileVideoSource source = new AVIFileVideoSource("test.avi");

    public Form1()
    {
        InitializeComponent();
    }

    public void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        input.Open("test.avi");
        for (int i = 0; i < input.Length; i++)
        {
            pictureBox1.Image = input.GetNextFrame();
        }
        source.Stop();
        input.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        source.NewFrame += new NewFrameEventHandler(cam_NewFrame);
        source.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        source.Stop();
        input.Close();

    }
}

任何其他建议将不胜感激。感谢您的时间。

4

1 回答 1

1

通过查看图书馆的其他一些区域,我找到了一个可行的解决方案。在这个解决方案中,我忽略了另外两个类:DirectShow(已经被引用)和 Control。具体来说,我需要使用 FileVideoSource 和 VideoSourcePlayer 的实例来将视频变成我可以使用的东西。

此版本与上述版本不同之处在于,将读取和写入功能合并为一个程序。此外,我急于完成这项工作,所以它仍然很脆弱。不过,这是我的解决方案:

    public partial class Form1 : Form
{
    public Bitmap newBitmap;
    public VideoCaptureDevice cam = null;
    public FilterInfoCollection usbCams;

    AVIReader reader = new AVIReader();
    AVIWriter writer = new AVIWriter("wmv3");
    AVIFileVideoSource source = new AVIFileVideoSource("test.avi");

    FileVideoSource normSource = new FileVideoSource("test.avi");
    VideoSourcePlayer player = new VideoSourcePlayer();

    public Form1()
    {
        InitializeComponent();
    }

    public void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap image = (Bitmap)eventArgs.Frame.Clone();
        writer.AddFrame(image);

        pictureBox1.Image = image;
    }

    public void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        newBitmap = (Bitmap)eventArgs.Frame.Clone();
        pictureBox1.Image = newBitmap;
    }

    private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
    {
        videoSourcePlayer1.VideoSource = normSource;
        videoSourcePlayer1.GetCurrentVideoFrame();

        videoSourcePlayer1.DrawToBitmap(newBitmap,
            new Rectangle(0, 0, image.Width, image.Height));
    }

    private void button1_Click(object sender, EventArgs e)
    {
        source.NewFrame += new NewFrameEventHandler(video_NewFrame);
        source.Start();
        videoSourcePlayer1.NewFrame += new AForge.Controls.VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
        videoSourcePlayer1.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (source.IsRunning == true)
        {
            source.Stop();
            videoSourcePlayer1.Stop();
        }

        if (cam != null)
        {
            cam.Stop();
            writer.Close();
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        usbCams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        cam = new VideoCaptureDevice(usbCams[0].MonikerString);
        cam.DesiredFrameSize = new Size(320, 240);

        writer.Open("test.avi", 320, 240);

        cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
        cam.DesiredFrameRate = 25;
        cam.Start();
    }
}

感谢您的阅读。

于 2011-10-19T01:46:00.233 回答