【问题标题】:How do I execute an ffmpeg cmd to command line using C#?如何使用 C# 将 ffmpeg cmd 执行到命令行?
【发布时间】:2019-11-03 16:51:51
【问题描述】:

我在尝试在 Visual Studio 19 上获取表单应用程序以在命令行上执行 cmd 以将视频从 mp4 转换为 avi 时遇到问题。 我为此使用ffmpeg,但每次编译它都不会拾取任何东西。

我已经通过命令行运行了参数,它可以很好地转换视频。据我所知,路径是正确的,所以我不确定为什么编译器不会拾取任何文件。

private void Button1_Click(object sender, EventArgs e)
    {
        string cmdString =  "c:\ffmpeg\bin";
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "ffmpeg.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments =  cmdString + $"-i shatner.mp4 shatner.avi";


        using (Process exeProcess = Process.Start(startInfo))
        {
            exeProcess.WaitForExit();
        }
    }
}

}

我得到的错误: "系统找不到指定的文件"

我也会在 Process.Start 周围放置一个 try catch 块,但这并不重要,因为它仍在抛出异常。

【问题讨论】:

  • 检查当前目录。
  • 你的ArgumentscmdString 没有任何意义。

标签: c# cmd ffmpeg


【解决方案1】:

您的文件名和参数指定不正确。请看下文。

private void button1_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "c:\\ffmpeg\\bin\\ffmpeg.exe";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.Arguments = "-i shatner.mp4 shatner.avi";

            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;       


            using (Process exeProcess = Process.Start(startInfo))
            {
                string error = exeProcess.StandardError.ReadToEnd();
                string output = exeProcess.StandardError.ReadToEnd();
                exeProcess.WaitForExit();

                MessageBox.Show("ERROR:" + error);
                MessageBox.Show("OUTPUT:" + error);
            }
        }

【讨论】:

  • 感谢您的反馈。我也试过了。它给了我一个不同的结果,但不是我期望的结果。我只需单击表单按钮,没有任何反应。你会碰巧知道它为什么会这样做吗?
  • 它应该可以工作。可能是你遇到了一些例外。尝试编辑后的代码,看看发生了什么。这应该会给你你遇到的输出或错误。
  • 再次感谢您的帮助,我终于让它工作了。我正在运行一个表单应用程序,但为了让它保持命令提示符打开并运行应用程序,我必须将项目管理器中的输出类型更改为控制台应用程序。这似乎起到了作用,因为它现在工作正常。再次感谢!
猜你喜欢
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
相关资源
最近更新 更多