将图像写入Process.StandardInput.BaseStream的更快捷方式

时间:2012-09-04 06:32:44

标签: c# image ffmpeg stdin binarywriter

我试图将大量桌面捕获的图像发送到编码器(FFmpeg)stdin。

以下代码示例有效。

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

1 个答案:

答案 0 :(得分:2)

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

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

相关问题