【问题标题】:Faster way to write image to Process.StandardInput.BaseStream将图像写入 Process.StandardInput.BaseStream 的更快方法
【发布时间】:2012-08-28 18:51:37
【问题描述】:

我正在尝试将大量桌面捕获的图像发送到编码器 (FFmpeg) 标准输入。

以下代码示例有效。

CaptureScreen() 函数在 5-10 毫秒内提供图像。

如果我将图像保存在 MemoryStream 中,几乎不需要任何时间。

但我只能每 45 毫秒保存 1 张图像到 proc.StandardInput.BaseStream.

public void Start(string bitrate, string buffer, string fps, string rtmp, string resolution, string preset)
{
    proc.StartInfo.FileName = myPath + "\\ffmpeg.exe";
    proc.StartInfo.Arguments = "-f image2pipe -i pipe:.bmp -vcodec libx264 -preset " + preset + " -maxrate " + bitrate + "k -bufsize " +
    buffer + "k -bt 10 -r " + fps + " -an -y test.avi"; //+ rtmp;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardInput = true;
    proc.StartInfo.RedirectStandardOutput = true;

    proc.Start();

    Stopwatch st = new Stopwatch();
    BinaryWriter writer = new BinaryWriter(proc.StandardInput.BaseStream);
    System.Drawing.Image img;

    st.Reset();
    st.Start();

    for (int z = 0; z < 100; z++)
    {
        img = ScrCap.CaptureScreen();
        img.Save(writer.BaseStream, System.Drawing.Imaging.ImageFormat.Bmp);
        img.Dispose();
    }

    st.Stop();
    System.Windows.Forms.MessageBox.Show(st.ElapsedMilliseconds.ToString());
}

问题是:

我可以更快地完成保存过程吗?

我尝试通过这种方式获得稳定的 60 fps

【问题讨论】:

  • 您是否尝试过使用ImageFormat.png 以减少实际读取/写入的数据量?
  • 我试过了。它甚至更慢......像 130 毫秒

标签: c# image ffmpeg stdin binarywriter


【解决方案1】:

这里的瓶颈是ffmpeg读取数据的速度和压缩成.avi的速度一样,速度很慢。因此,您的 img.Save 方法会阻塞,直到流的缓冲区中有一些空间可以写入其数据。

你无能为力。实时压缩 60 fps 高清视频需要强大的处理能力。

【讨论】:

  • 有没有办法告诉 ffmpeg 加快速度?我的意思是这应该是可能的,因为我过去做过实时 60 fps 高清,而 ffmpeg 只使用了 7% 的 cpu 功率。
  • 你必须找出阻塞链的原因。捕获图像?将其编码为 BMP ?将其发送到 ffmpeg ?将其压缩为 .avi ?将生成的视频写入硬盘?
  • 它必须将其发送到 ffmpeg.. 我尝试将 bmp 写入另一个流,几乎没有时间。使用命名管道会更快吗?
  • 好的。也许您可以尝试将writer.BaseStream 包裹在BufferedStream 中以缓解问题。
  • 不幸的是没有做太多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2011-03-01
  • 1970-01-01
  • 2013-09-02
  • 2012-01-03
  • 1970-01-01
相关资源
最近更新 更多