我试图将大量桌面捕获的图像发送到编码器 (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/