.NET 进程 - 重定向标准输入和标准输出,而不会导致死锁



我正在尝试从程序生成的帧中使用FFmpeg对视频文件进行编码,然后将FFmpeg的输出重定向回我的程序以避免中间视频文件。

但是,在System.Diagnostic.Process中重定向输出时,我遇到了一个似乎相当常见的问题,在此处文档的备注中提到,如果同步运行会导致死锁。

在为此苦苦挣扎了一天,并尝试了网上找到的几种建议的解决方案后,我仍然找不到使其工作的方法。我得到了一些数据,但这个过程总是在完成之前冻结。


下面是产生上述问题的代码片段:

static void Main(string[] args)
{
Process proc = new Process();
proc.StartInfo.FileName = @"ffmpeg.exe";
proc.StartInfo.Arguments = String.Format("-f rawvideo -vcodec rawvideo -s {0}x{1} -pix_fmt rgb24 -r {2} -i - -an -codec:v libx264 -preset veryfast -f mp4 -movflags frag_keyframe+empty_moov -",
16, 9, 30);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
FileStream fs = new FileStream(@"out.mp4", FileMode.Create, FileAccess.Write);
//read output asynchronously
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
{
proc.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
string str = e.Data;
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
fs.Write(bytes, 0, bytes.Length);
}
};
}

proc.Start();
proc.BeginOutputReadLine();
//Generate frames and write to stdin
for (int i = 0; i < 30*60*60; i++)
{
byte[] myArray = Enumerable.Repeat((byte)Math.Min(i,255), 9*16*3).ToArray();
proc.StandardInput.BaseStream.Write(myArray, 0, myArray.Length);
}
proc.WaitForExit();
fs.Close();
Console.WriteLine("Done!");
Console.ReadKey();
}

目前,我正在尝试将输出写入文件以进行调试,但这不是最终使用数据的方式。

如果有人知道解决方案,将不胜感激。

您的进程挂起,因为您永远不会告诉 ffmpeg 您已完成对StandardInput流的写入。所以它仍然坐在那里等待你向它发送更多数据。

就个人而言,我认为使用常规文件作为 ffmpeg 的输入和输出选项(即命令行参数中的infileoutfile(会更可靠、更容易编码。然后,您无需重定向任何流。

但是,如果您确实想要或需要重定向自己(例如,您实际上是独立于某个文件生成输入数据,并且您正在以文件以外的某种方式使用输出数据(,则可以通过正确使用Process类及其属性来使其工作。

特别是,这意味着两件事:

  1. 当程序为流生成原始字节数据时,不能将输出数据视为字符。
  2. 完成后必须关闭输入流,以便程序知道已到达输入流的末尾。

这是实际有效的程序版本:

static void Main(string[] args)
{
Process proc = new Process();
proc.StartInfo.FileName = @"ffmpeg.exe";
proc.StartInfo.Arguments = String.Format("-f rawvideo -vcodec rawvideo -s {0}x{1} -pix_fmt rgb24 -r {2} -i - -an -codec:v libx264 -preset veryfast -f mp4 -movflags frag_keyframe+empty_moov -",
16, 9, 30);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
FileStream fs = new FileStream(@"out.mp4", FileMode.Create, FileAccess.Write);
proc.Start();
var readTask = _ConsumeOutputAsync(fs, proc.StandardOutput.BaseStream);
//Generate frames and write to stdin
for (int i = 0; i < 30 * 60 * 60; i++)
{
byte[] myArray = Enumerable.Repeat((byte)Math.Min(i, 255), 9 * 16 * 3).ToArray();
proc.StandardInput.BaseStream.Write(myArray, 0, myArray.Length);
}
proc.StandardInput.BaseStream.Close();
readTask.Wait();
fs.Close();
Console.WriteLine("Done!");
Console.ReadKey();
}
private static async Task _ConsumeOutputAsync(FileStream fs, Stream baseStream)
{
int cb;
byte[] rgb = new byte[4096];
while ((cb = await baseStream.ReadAsync(rgb, 0, rgb.Length)) > 0)
{
fs.Write(rgb, 0, cb);
}
}

请注意,即使您是动态生成的,您的示例也显示将输出写入文件。如果您真的想要文件中的输出,那么我肯定会使用outfile命令行参数来指定输出文件,而不是自己重定向StandardOutput。当您正在运行的外部进程将为您处理所有工作时,为什么要在数据处理中注入您自己的代码?

我仍然不知道你想用AutoResetEvent对象做什么,因为你从来没有等待过这个对象,而且你的实现被破坏了,因为你在使用它之前就处置了这个对象。因此,上述修订后的示例没有任何内容试图复制该行为。没有它它就可以正常工作。

最新更新