c# - 将图像写入 Process.StandardInput.BaseStream 的更快方法

标签 c# image ffmpeg stdin binarywriter

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

以下代码示例有效。

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

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

But I can only save 1 image every 45 ms to 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

最佳答案

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

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

关于c# - 将图像写入 Process.StandardInput.BaseStream 的更快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257939/

相关文章:

c# - 在某些情况下,如何在需要空指针的 P/Invoke 签名中使用 SafeHandle?

c# - 一次设置最大运行任务时等待多个异步任务

c# - 使用 C# 打开 Excel 2003 电子表格。找不到可安装的 ISAM。异常(exception)

python - 我试图从我的 S3 存储桶,从测试选项卡播放 Alexa 技能的音频,**它显示 <audio only response> 但事实上,我听不到任何声音

c++ - 使用非英文文件名调用 avio_open 函数无效

c# - 简单的普通 Windows 应用程序——我应该从 WPF 还是 WinForms 开始?

java - 如何在java中进行碰撞检测?

没有 PIL 的 Python windows 7 屏幕截图

html - 图像大小应该在 img 标签的高度/宽度属性中定义还是在 CSS 中定义?

hash - 一次调用处理视频文件中的多个流