首页 > 解决方案 > 如何使用 C# 将 ffmpeg cmd 执行到命令行?

问题描述

我在尝试在 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 块,但这并不重要,因为它仍在抛出异常。

标签: c#cmdffmpeg

解决方案


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

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

推荐阅读