gpt4 book ai didi

c# - asp.net ffmpeg 视频编码挂起

转载 作者:行者123 更新时间:2023-12-04 22:48:56 25 4
gpt4 key购买 nike

我有以下方法可以使用 ffmpeg 对上传到网站的视频进行编码。它适用于高达 8-9 MB 的视频,但如果视频大小大于 8-9 MB,它会挂起网站。恢复它的唯一方法是重新启动 iis。

当我观看该过程时,我可以看到 ffmpeg 对视频进行编码并退出。结果视频很好。一旦 ffmpeg 存在,问题就开始了。

应用程序在 win2003 x86 webserver iis6 上运行

任何人都有使用 ffmpeg 从 asp.net Web 应用程序编码大文件的经验?

public EncodedVideo EncodeVideo(VideoFile input, string encodingCommand, string outputFile)
{
EncodedVideo encoded = new EncodedVideo();
Params = string.Format("-i \"{0}\" {1} \"{2}\"", input.Path, encodingCommand, outputFile);
//string output = RunProcess(Params);
string output = RunProcessLargeFile(Params);
encoded.EncodingLog = output;
encoded.EncodedVideoPath = outputFile;

if (File.Exists(outputFile))
{
encoded.Success = true;
}
else
{
encoded.Success = false;
}
//System.Web.HttpContext.Current.Response.Write(Params);
return encoded;

}

private string RunProcessLargeFile(string Parameters)
{
/* The below will be the right solution ....
* The while loop which reads the stream is very improtant
* for FFMPEG as .NET does not provide more memory to FFMPEG.
* When converting large files, FFMPEG's out put stream gets filled...
* And waits for .NET to allocate memory resources but is never done.
* In order to utilize less memory, we are clearing the buffer periodically.
**/

ProcessStartInfo oInfo = new ProcessStartInfo(this.FFmpegPath, Parameters);
oInfo.WorkingDirectory = Path.GetDirectoryName(this.FFmpegPath);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
Process proc = System.Diagnostics.Process.Start(oInfo);
StreamReader srOutput = proc.StandardError;
System.Text.StringBuilder output = new System.Text.StringBuilder();

StreamReader objStreamReader = proc.StandardError;
System.Text.StringBuilder sbOutPut = new StringBuilder();

while (!proc.WaitForExit(1000))
{
sbOutPut.Append(objStreamReader.ReadToEnd().ToString());
}

if (proc.ExitCode == 0)
{
proc.Close();
if (objStreamReader != null)
{
objStreamReader.Close();
}
}
else
{
proc.Close();
if (objStreamReader != null) objStreamReader.Close();
}

return sbOutPut.ToString();
}

最佳答案

闻起来像典型的僵局。

您调用proc.WaitForExit之前 proc.StandardError.ReadToEnd方法,它似乎会导致死锁。

来自 MSDN 的引用:

A deadlock condition can result if the parent process calls WaitForExit before StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.



所以更换你的 while循环到:
string output = objStreamReader.ReadToEnd();
proc.WaitForExit();

应该可以解决您的问题。

关于c# - asp.net ffmpeg 视频编码挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3883853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com