ffmpeg from a C# app using Process class - user pr

2019-06-07 04:04发布

问题:

I am writing an app to run ffmpeg using c#. My program redirects the standardError output to a stream so it can be parsed for progress information.

During testing I have found a problem:

If the output is shown in a command window rather than being redirected ffmpeg will display it's normal headers followed by "file c:\temp\testfile.mpg already exists. overwrite [y]". If I click on the command window and press y the program continues to encode the file.

If the StandardError is redirected to my handler and then printed to the console, I see the same header information that was displayed in the command window now printed to the console. except for the file...already exists prompt. If I click in the command window and press y the program continues to process the file.

Is there a stream other than standardOutput or standardError that is used when the operator is prompted for information, or am I missing something else?

public void EncodeVideoWithProgress(string filename, string arguments, BackgroundWorker worker, DoWorkEventArgs e)
    {

        Process proc = new Process();

        proc.StartInfo.FileName = "ffmpeg";
        proc.StartInfo.Arguments = "-i " + " \"" + filename + "\" " + arguments;

        proc.StartInfo.UseShellExecute = false;
        proc.EnableRaisingEvents = true;

        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardOutput = false;
        proc.StartInfo.CreateNoWindow = false; //set to true for testing

        proc.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);

        proc.Start();
        proc.BeginErrorReadLine();


        StreamReader reader = proc.StandardOutput;
         string line;
         while ((line = reader.ReadLine()) != null)
        { Console.WriteLine(line); }
     proc.WaitForExit();
}

private static void NetErrorDataHandler(object sendingProcess,
               DataReceivedEventArgs errLine)
    {
        if (!String.IsNullOrEmpty(errLine.Data))
        {
            Console.WriteLine(errLine.Data);
        }
    }

回答1:

Rather than going through all this stuff, use the "-y" command line option when you start the process, which will force ffmpeg to overwrite existing files.