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

时间:2016-02-03 10:35:21

标签: c# macos unity3d process ffmpeg

我正在尝试在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);
    }

该命令执行并且不会通过任何异常。然而,它立即终止并打印退出代码1 ,这是“一般错误的捕获” - 这似乎不太有用!

我的代码出了什么问题,好吗?

你会注意到我的代码完全打印出了命令。如果我复制该命令并将其粘贴到终端,它运行绝对正常。

1 个答案:

答案 0 :(得分:0)

事实证明我错误地设置了论据。参考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);

    }

似乎答案与我正在做的不同,但确实有效!