gpt4 book ai didi

c# - 在winforms中多次运行 "FFMPEG"

转载 作者:行者123 更新时间:2023-12-04 23:16:30 26 4
gpt4 key购买 nike

在 C# Windows 应用程序中,我尝试调用“ffmpeg”来复用视频和音频。它可能会被多次调用。在第一次通话中,一切都很好,但在下一次通话中,我遇到了一些问题。一个问题是早期的“ffmpeg”进程没有关闭。所以,如果它存在,我试图杀死它。但现在我在以下代码中收到了已处置对象的错误:

   public static void FFMPEG3(string exe_path, string avi_path, string mp3_path, string output_file)
{
const int timeout = 2000;
Kill(exe_path);
using (Process process = new Process())
{
process.StartInfo.FileName = exe_path;
process.StartInfo.Arguments = string.Format(@"-i ""{0}"" -i ""{1}"" -acodec copy -vcodec copy ""{2}""",
avi_path, mp3_path, output_file);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();

using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};

process.Start();

process.BeginOutputReadLine();
process.BeginErrorReadLine();

if (process.WaitForExit(timeout) &&
outputWaitHandle.WaitOne(timeout) &&
errorWaitHandle.WaitOne(timeout))
{
// Process completed. Check process.ExitCode here.
process.Close();
}
else
{
// Timed out.
process.Close();
}
}
}
}

我得到 ObjectDisposedException对于 ErrorDataRecieved errorWaitHandle.Set(); 上的事件

首先,我想解决这个错误,但是如果你知道任何更好的解决方案来多次运行“ffmpeg”,请建议我。

最佳答案

问题是第二次,“ffmpeg”必须覆盖之前生成的视频文件。然后,它会提出一个问题并等待用户允许覆盖。由于我使用了CreateNoWindow ,用户无法回复此消息。为了解决这个问题,我使用了选项 -y自动覆盖任何以前的文件。

 process.StartInfo.Arguments 
= string.Format(@"-i ""{0}"" -i ""{1}"" -y -acodec copy -vcodec copy ""{2}""",
avi_path, mp3_path, output_file);

关于c# - 在winforms中多次运行 "FFMPEG",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688584/

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