Reputation: 9658
In a C# Windows application, I try to call "ffmpeg" to multiplex video and audio. It may be called several times. In the first call, everything is fine, but in the next call I have some problems. One problem is that the earlier "ffmpeg" process isn't closed. So, I tried to kill it if it exists. but now I got an error for a disposed object in the following code:
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();
}
}
}
}
I get ObjectDisposedException
for ErrorDataRecieved
event on errorWaitHandle.Set();
First, I want to resolve this error, but if you know any better solution to run the "ffmpeg" for several times please suggest me.
Upvotes: 0
Views: 409
Reputation: 9658
The problem is that for the second time, the "ffmpeg" must overwrite the previous generated video file. Then, it asks a question and waits for the user to allow overwriting. Since I used CreateNoWindow
, the user can't reply this message. To resolve this problem I used the option -y
to automatically overwrite any previous file.
process.StartInfo.Arguments
= string.Format(@"-i ""{0}"" -i ""{1}"" -y -acodec copy -vcodec copy ""{2}""",
avi_path, mp3_path, output_file);
Upvotes: 1