降低 AForge 中 AVI 文件的帧速率

发布于 2024-12-10 05:18:47 字数 1228 浏览 0 评论 0原文

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

    }
}

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

I am completely new to video input, and just started working with AForge a few days ago. Working with live video is comfortable, but I need to do something with files for a project now.

Using the Windows Media Video 9 VCM codec, saving has not been a problem. The output file works normally with every player I have, but my program always plays it back at about double the frame rate. This is especially odd since there is never any indication that the frame rate is changed: both the default the video was saved with and the new player indicate that the frame rate is 25 fps.

The only suggestions I have found are to change the frame rate before the video is captured, but this seems to do nothing.

Looking around in the AVIFileVideoSource documentation, I found the FrameIntervalFromSource and FrameInterval properties which, together, should give me the results I am looking for, but I can't get them to work, either. Everything else has been a dead end, and I am out of ideas. Here is the code that I am using to read the file:

    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();

    }
}

Any other suggestions would be greatly appreciated. Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-17 05:18:47

通过研究图书馆的其他一些区域,我找到了解决该问题的可行方法。在此解决方案中,我忽略了另外两个类:已经引用的 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();
    }
}

感谢您的阅读。

I found a working solution to the problem by looking into some other areas of the library. In this solution, there were two other classes that I was overlooking: DirectShow, which was already referenced, and Control. Specifically, I needed to use instances of FileVideoSource and VideoSourcePlayer to get the video into something I could work with.

This version is different from the above in that both the read and write functions have been combined into one program. Furthermore, I was in something of a rush to get this done, so it is still quite fragile. Nevertheless, here is my solution:

    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();
    }
}

Thank you for reading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文