【问题标题】:FFmpeg stops Process C#FFmpeg 停止进程 C#
【发布时间】:2018-10-23 12:22:11
【问题描述】:

我相信我的“FFmpeg”进程陷入了无限循环,或者它正在等待某些东西,我不知道那是什么。它不会通过WaitForExit 方法。

FFmpeg:

-ss 0 -i output.mp4 -t 10 -an -y test.mp4

C#代码:

using (Process process = new Process())
{
     process.StartInfo.UseShellExecute = false;
     process.StartInfo.RedirectStandardOutput = true;
     process.StartInfo.RedirectStandardError = true;
     process.StartInfo.FileName = FileName; // ffmpeg.exe
     process.StartInfo.Arguments = Arguments; //-ss 0 -i output.mp4 -t 10 -an -y test.mp4
     process.Start();
     process.WaitForExit(); // stops here and waits

     return process.StandardOutput.ReadToEnd();
}

编辑:

-loglevel quiet 添加到我的ffmpeg 查询使我的问题消失了。为什么?如何在不添加 -loglevel quiet 的情况下获得相同的结果?

【问题讨论】:

  • 您是否尝试在终端中使用此参数调用“ffmpeg.exe”以查看可执行文件是否真的退出?
  • @Leonardo Menezes 做到了。在cmd 中工作正常,但是当我在Process 中使用它时,它会停止并等待。
  • 尝试将文件名设置为您的 powershell,例如:“%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe”并将参数设置为“-ExecutionPolicy Bypass -Command”ffmpeg.exe -ss 0 -i output.mp4 -t 10 -an -y test.mp4" " 看看它的表现如何。
  • @Leonardo Menezes 得到:An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: Access is denied
  • 好的,现在我们有两个选择:您的 c# 代码无权访问 powershell 或您的 powershell 无权访问 ffmpeg.exe。也许这个问题与您的原始问题有关,该错误可能是您永无止境的过程的原因。我建议进行一些调试,尝试为您的 Visual Studio 授予管理员权限。

标签: c# ffmpeg


【解决方案1】:

您正在重定向标准和错误输出,但在子进程退出之前您不会读取它们。

有一个与这些输出相关联的大小有限的缓冲区,当这个缓冲区变满时 - 进程(在本例中为 ffmpeg)在尝试在那里写入时会阻塞。所以这个缓冲区就像队列:一侧(一个进程,ffmpeg)将东西推送到那里,另一侧(你的进程,因为你重定向输出)预计将它们从队列中弹出。如果您不这样做 - 队列已满,并且一个进程无法将项目推送到那里,因此它会阻塞等待空间可用。

出于这个原因,添加-loglevel quiet“解决”了问题,减少了 ffmpeg 的输出,以便即使您没有读取它也可以放入缓冲区。

所以最简单的解决方案是——不要重定向错误输出(反正你不是在阅读它),而是阅读标准输出:

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;                
process.StartInfo.FileName = FileName; // ffmpeg.exe
process.StartInfo.Arguments = Arguments; //-ss 0 -i output.mp4 -t 10 -an -y test.mp4
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit(); // stops here and waits
return result;

如果您需要同时读取错误输出和标准输出,或者需要超时 - 这更复杂,但您可以在 Internet 上找到大量解决方案。

【讨论】:

  • 文档工具提示中的每个命令调用后都应该有一些关于清空缓冲区的内容。这修复了我的应用程序中的一个奇怪错误,当它停止工作时。
猜你喜欢
  • 2022-01-14
  • 2011-12-30
  • 2012-04-01
  • 1970-01-01
  • 2011-01-29
  • 2013-06-11
  • 2020-01-30
  • 2020-12-18
  • 2020-01-12
相关资源
最近更新 更多