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

C# 将图像写入Process.StandardInput.BaseStream的更快方法,c#,image,ffmpeg,stdin,binarywriter,C#,Image,Ffmpeg,Stdin,Binarywriter,我正在尝试将大量桌面捕获的图像发送到编码器(FFmpeg)标准格式 下面的代码示例有效 CaptureScreen()功能可在5-10毫秒内提供图像 如果我将图像保存在MemoryStream中,几乎不需要时间 但我只能每45毫秒保存一张图像到 proc.StandardInput.BaseStream public void Start(字符串比特率、字符串缓冲区、字符串fps、字符串rtmp、字符串解析、字符串预设) { proc.StartInfo.FileName=myPath+“\\f

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

下面的代码示例有效

CaptureScreen()
功能可在5-10毫秒内提供图像

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

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

public void Start(字符串比特率、字符串缓冲区、字符串fps、字符串rtmp、字符串解析、字符串预设)
{
proc.StartInfo.FileName=myPath+“\\ffmpeg.exe”;
proc.StartInfo.Arguments=“-f image2pipe-i管道:.bmp-vcodec libx264-preset”+preset+“-maxrate”+bitrate+“k-bufsize”+
缓冲区+“k-bt 10-r”+fps+“-an-y test.avi”//+rtmp;
proc.StartInfo.UseShellExecute=false;
proc.StartInfo.RedirectStandardInput=true;
proc.StartInfo.RedirectStandardOutput=true;
proc.Start();
秒表st=新秒表();
BinaryWriter=新的BinaryWriter(proc.StandardInput.BaseStream);
系统图、图像img;
圣重置();
st.Start();
对于(intz=0;z<100;z++)
{
img=ScrCap.CaptureScreen();
保存(writer.BaseStream、System.Drawing.Imaging.ImageFormat.Bmp);
img.Dispose();
}
st.Stop();
System.Windows.Forms.MessageBox.Show(st.elapsedmillesons.ToString());
}
问题是:

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


我试图通过这种方式获得稳定的60 fps,这里的瓶颈是ffmpeg读取数据的速度与压缩数据到.avi的速度相同,这很慢。因此,您的
img.Save
方法会阻塞,直到流的缓冲区中有一些空间来写入其数据


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

您是否尝试过使用
ImageFormat.png
以减少实际读取/写入的数据量?我尝试过。它甚至更慢。。。有没有办法告诉ffmpeg加快速度?我的意思是这应该是可能的,因为我在过去做过实时60帧高清,而ffmpeg只使用了7%的cpu功率。你必须找出是什么阻碍了这条链。拍摄图像?把它编码成BMP?将其发送到ffmpeg?把它压缩成.avi?将生成的视频写入硬盘?必须将其发送到ffmpeg。。我尝试将th bmp写入另一个流,几乎不花时间。使用命名管道会更快吗?好的。也许您可以尝试将
writer.BaseStream
包装在
BufferedStream
中,以简化这件事。不幸的是,这并没有起到多大作用
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());
}