Mac上的C#启动过程-FFMPEG-退出代码1 [英] C# Start Process on Mac - FFMPEG - Exit Code 1
问题描述
我正在尝试在Mac和Windows(使用Unity)上启动一个进程,以运行FFMPEG将视频转换为.ogv视频.我的代码如下:
I am trying to start a process on Mac and Windows (using Unity) to run FFMPEG to convert a video to a .ogv video. My code is as follows:
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 ,该代码为常见错误的补充"-这似乎不太有用!
The command executes and does not through any exception. However, it terminates instantly and prints Exit Code 1 which is "Catchall for general errors" -this seems not too helpful!
请问我的代码有什么问题?
What am I doing wrong with my code, please?
您会注意到我的代码完整地打印了命令.如果我复制该命令并将其粘贴到终端中,则它运行得很好.
You'll notice that my code prints out the command in full. If I copy that command and paste it into the terminal, it runs absolutely fine.
推荐答案
事实证明我错误地设置了参数.引用此堆栈溢出问题,我可以使用以下代码产生预期的结果:
It turns out I was setting up the arguments wrongly. Referring to this Stack Overflow question, I was able to produce the expected result with the following code:
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);
}
似乎答案与我所做的没什么不同,但确实有效!
It does seem as though the answer was not that different from what I was doing, but it does work!
这篇关于Mac上的C#启动过程-FFMPEG-退出代码1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!