减少帧率的AVI文件在AForge

本文关键字:文件 AForge AVI 帧率 | 更新日期: 2023-09-27 18:11:27

我对视频输入完全陌生,几天前刚开始使用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();
    }
}

如有其他建议,我将不胜感激。感谢您的宝贵时间。

减少帧率的AVI文件在AForge

通过查看库的其他一些区域,我找到了这个问题的有效解决方案。在这个解决方案中,我忽略了另外两个类: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();
    }
}

感谢您的阅读。