C# 在 Mac 上启动进程 - FFMPEG - 退出代码 1

C# Start Process on Mac - FFMPEG - Exit Code 1

我正在尝试在 Mac 和 Windows(使用 Unity)上启动到 运行 FFMPEG 的进程,以将视频转换为 .ogv 视频。我的代码如下:

    string command = "ffmpeg -i '" + filepath + "'  -codec:v libtheora -qscale:v 10 -codec:a libvorbis -qscale:a 10 -y '"+workingDir+"/ogv_Video/"+System.IO.Path.GetFileNameWithoutExtension(filepath)+".ogv'";
    UnityEngine.Debug.Log("Command: "+command);

    try{

        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo (workingDir+"/..", command);
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.FileName =workingDir+"/ffmpeg";

        //Process.Start (startInfo);
        Process p = Process.Start(startInfo); 
        p.EnableRaisingEvents = true;
        string strOutput = p.StandardOutput.ReadToEnd(); 
        UnityEngine.Debug.Log ("Running..."+strOutput);
        p.WaitForExit(); 

        UnityEngine.Debug.Log ("Got here. "+strOutput);
        int exitCode = p.ExitCode;
        UnityEngine.Debug.Log ("Process exit code = "+exitCode);
    }
    catch(Exception e) {
        UnityEngine.Debug.Log ("An error occurred");
        UnityEngine.Debug.Log ("Error: "+e);
    }

命令执行并没有通过任何异常。但是,它会立即终止并打印 Exit Code 1 即 "Catchall for general errors" - 这似乎不太有用!

请问我的代码有什么问题吗?

您会注意到我的代码打印出了完整的命令。如果我复制该命令并将其粘贴到终端中,它 运行 绝对没问题。

原来我设置的参数有误。参考 this Stack Overflow question,我能够使用以下代码产生预期的结果:

try{

        Process process = new Process();
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +@"ffmpeg";

        process.StartInfo.Arguments = command;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.Start();


        JSONDataObject rtnMsg = new JSONDataObject("StartConvertOK", "-1", new List<string>());
        return JsonUtility.ToJson(rtnMsg);

    }

看起来答案似乎与我所做的没有太大不同,但它确实有效!