【问题标题】:ffmpeg from a C# app using Process class - user prompt not shown in standardError使用 Process 类的 C# 应用程序中的 ffmpeg - 用户提示未显示在标准错误中
【发布时间】:2016-08-14 00:33:13
【问题描述】:

我正在编写一个使用 c# 运行 ffmpeg 的应用程序。我的程序将 standardError 输出重定向到一个流,以便解析它以获取进度信息。

在测试过程中我发现了一个问题:

如果输出显示在命令窗口中而不是被重定向,ffmpeg 将显示它的正常标题,后跟 “文件 c:\temp\testfile.mpg 已存在。覆盖 [y]”。如果我单击命令窗口并按 y,程序会继续对文件进行编码。

如果 StandardError 被重定向到我的处理程序,然后打印到控制台,我会看到在命令窗口中显示的相同标题信息现在打印到控制台。 文件除外...已经存在提示。如果我在命令窗口中单击并按 y,程序会继续处理该文件。

当提示操作员输入信息时,是否使用了除standardOutput 或standardError 之外的流,或者我是否遗漏了其他内容?

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);
        }
    }

【问题讨论】:

    标签: c# process ffmpeg redirectstandardoutput


    【解决方案1】:

    不要遍历所有这些东西,而是在启动进程时使用“-y”命令行选项,这将强制 ffmpeg 覆盖现有文件。

    【讨论】:

    • 感谢您的解决方法。我会用它。然而,我仍然很好奇为什么信息显示在命令窗口而不是标准错误或标准输出流上。
    • 这与 DOS 中的一些旧挂钩有关。程序直接操作屏幕内存,不使用标准输出和错误流。
    • Windows 仍然支持这种替代方法
    • 谢谢,接受了答案。我可能会使用 -n 选项,它确实通过 standardError 'File... 已存在发送消息。退出'
    • 啊当然。感谢这个解决方法,这真的很有帮助。那么有没有办法检索这个输出呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2022-11-16
    相关资源
    最近更新 更多