繁体   English   中英

使用AForge.Video.FFMPEG / AForge.Video.VFW的屏幕截图视频。 帧率问题

[英]Screen capture video using AForge.Video.FFMPEG / AForge.Video.VFW. Frame Rate issue

我的WinForms .NET 4 C#应用程序在用户与之交互时记录桌面。

它使用AForge FFMPEG或VFW包装,具体取决于系统的速度。 捕获是在后台线程中完成的。

在任何一种情况下,包装器都需要提前指定帧速率。 这是有问题的,因为捕获频率是不确定的并且容易受到目标机器的繁忙程度的影响。 在一个好的系统上,我得到最多10 FPS。

这里有两个问题:

  • 如何根据实际捕获帧率对齐帧?
  • 如何通过使用AForge以外的解决方案来提高帧速率?

为清晰起见,下面列出了我使用的代码:

private void ThreadWork ()  
{  
    int count = 0;  
    string filename = "";  
    RECT rect = new RECT();  
    System.Drawing.Bitmap bitmap = null;  
    System.Drawing.Graphics graphics = null;  
    System.DateTime dateTime = System.DateTime.Now;  
    System.IntPtr hWndForeground = System.IntPtr.Zero;  
    AForge.Video.FFMPEG.VideoFileWriter writer = null;  

    filename = @"c:\Users\Raheel Khan\Desktop\Temp\Capture.avi";
    if (System.IO.File.Exists(filename))
        System.IO.File.Delete(filename);

    writer = new AForge.Video.FFMPEG.VideoFileWriter();
    writer.Open
    (
        filename,
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
        10, // FPS
        AForge.Video.FFMPEG.VideoCodec.MPEG4
    );

    bitmap = new Bitmap
    (
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
        System.Drawing.Imaging.PixelFormat.Format24bppRgb
    );
    graphics = System.Drawing.Graphics.FromImage(bitmap);

    dateTime = System.DateTime.Now;

    while (System.DateTime.Now.Subtract(dateTime).TotalSeconds < 10) // 10 seconds.
    {
        graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);

        rect = new RECT();
        hWndForeground = GetForegroundWindow();
        GetWindowRect(hWndForeground, ref rect);
        graphics.DrawRectangle(Pens.Red, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
        graphics.DrawString(count++.ToString(), this.Font, Brushes.Red, 10, 10);

        writer.WriteVideoFrame(bitmap);

        System.Threading.Thread.Sleep(10);
    }

    writer.Close();
    writer.Dispose();

    System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(filename));
}

您可以将帧作为jpeg或mjpeg文件捕获到临时文件夹,然后在用户需要时将其重新处理为avi视频文件。 这将减少您需要“即时”处理并释放资源的处理量 - 并允许您的系统达到更高和更常规的帧速率。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM