【发布时间】:2018-11-13 18:40:12
【问题描述】:
我正在尝试将一些流传输到 ffmpeg 并捕获它的输出,以便我可以在我的代码中传递另一个流。
这是一个代码示例,它只是在我写入其StandardInput.BaseStream 后停止进程继续。
internal class Program
{
private static void Main(string[] args)
{
var inputFile = @"C:\Temp\test.mp4";
var outputFile = @"C:\Temp\test.mp3";
var process = new Process
{
StartInfo = new ProcessStartInfo
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
Arguments = "-i - -f mp3 -",
FileName = "ffmpeg.exe"
},
EnableRaisingEvents = true
};
process.ErrorDataReceived += (sender, eventArgs) => Console.WriteLine(eventArgs.Data);
process.Start();
process.BeginErrorReadLine();
using (var input = new FileStream(inputFile, FileMode.Open))
using (var output = new FileStream(outputFile, FileMode.Create))
{
input.CopyTo(process.StandardInput.BaseStream);
process.StandardOutput.BaseStream.CopyTo(output);
}
process.WaitForExit();
Console.WriteLine("done");
Console.ReadLine();
}
}
这个例子和这个问题的答案几乎一样:https://stackoverflow.com/a/8999542/2277280
我做错了什么? 为什么这个过程不继续? 是 ffmpeg 特有的吗?
【问题讨论】:
-
您可能需要尝试读取标准输出,类似于您获得错误输出的方式 - 基于事件处理程序。我的猜测是输出缓冲区已满,因为您的代码直到输入完成后才会读取它。它可能处于死锁状态,无法继续输入,也无法向输出缓冲区输出更多数据。