无法将进程的输出流保存到文件



我使用ffmpeg.exe作为进程并将转换后的视频输出到内存中,然后从内存中将数据保存到视频文件中(这是我不能直接将数据转换后的视频保存到文件的要求(。但是由于某种原因转换不起作用,这是我尝试过的,

var ffmpeg = HttpContext.Current.Server.MapPath("~/FFMpeg/ffmpeg.exe");
var outputDir = HttpContext.Current.Server.MapPath("~/Uploads/converted.mp4");
var inputDir = "https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_10mb.mp4";
var args = "-i " + inputDir + " -c:v libx264 -preset veryslow -crf 26 " +
"-ar 44100 -ac 2 -c:a aac -strict -2 -b:a 128k -";
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = ffmpeg;
process.StartInfo.WorkingDirectory = ffmpeg.Replace("\ffmpeg.exe", "");
process.StartInfo.Arguments = args;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.EnableRaisingEvents = true;
//process.WaitForExit();
Stream output = process.StandardOutput.BaseStream;
process.Exited += (sender, e) =>
{
using (var fileStream = File.Create(outputDir))
{
output.Seek(0, SeekOrigin.Begin);
output.CopyTo(fileStream);
}
};  

输出文件converted.mp4已创建,但其 0 kb。

据我了解,由于过程的长度,IIS 甚至在它能够做任何有价值的事情之前就终止了应用程序。这并不意味着IIS不能触发外部程序来接管你,所以大多数情况下,你正在将进程(.exe(从IIS用户空间移动到更适合的多线程用户空间。您可以推出自己的队列管理系统,但过去,我使用过 HangFire,因为它更适合该任务。使用 hangfire,您可以提交转换文件的作业并让它处理用户的请求,您只需在数据库中放置一个条目,显示 FFMPEG 正在转换的数据的状态。因此,当用户刷新页面时,它将轮询数据库以获取信息,而不是.exe本身的控制台输出(该输出将从刷新中清除(。

https://www.hangfire.io/

最新更新