-->

更快的写入图像Process.StandardInput.BaseStream方式(Faster w

2019-07-31 08:16发布

我试着去送了不少桌面捕捉的图像到编码器(FFmpeg的)标准输入。

下面的代码示例工程。

所述CaptureScreen()函数在5-10ms的提供图像。

如果我将图像保存在一个MemoryStream它几乎不需要花时间。

但我只能保存1个图像每隔45毫秒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帧这样

Answer 1:

这里的瓶颈是ffmpeg的它压缩成.avi相同的速度,这是缓慢的读取数据。 所以,你的img.Save方法块,直到有在流的缓冲区一定的空间来写它的数据。

没有什么可以做。 实时压缩每秒60帧的高清视频需要巨大的处理能力。



文章来源: Faster way to write image to Process.StandardInput.BaseStream