简体   繁体   English

process.start()参数

[英]process.start() arguments

when i do the following command into dos it will work fine 当我对dos执行以下命令时,它将正常工作

ffmpeg -f image2 -i frame%d.jpg -vcodec mpeg4 -b 800k video.avi

When I try to use the process class in c#, without the arguments, it loads ffmpeg in a console window then dissapears like usual. 当我尝试在c#中使用流程类而没有参数时,它会在控制台窗口中加载ffmpeg,然后像往常一样消失。 However, when I try to use the argument as I do above, formatted exactly the same...it doesn't work! 但是,当我尝试像上面那样使用参数时,格式完全一样......它不起作用! ffmpeg still loads, however since the console window closes so fast I cannot determine what the error is :/ ffmpeg仍然加载,但是由于控制台窗口关闭如此之快,我无法确定错误是什么:/

Process ffmpeg = new Process();
ffmpeg.StartInfo.FileName = path + "//" + "ffmpeg.exe";
ffmpeg.StartInfo.Arguments = " -f image2 -i frame%d.jpg -vcodec mpeg4 -b 800k video.avi";
ffmpeg.Start();

Any one know why this is? 谁知道为什么会这样? Why would the command work from dos and then fail to work using c# even when the arguments are exactly the same? 为什么命令可以从dos工作,然后使用c#无法工作,即使参数完全相同? I've used this method before for many things and never encountered this. 我之前使用过这种方法很多东西,从来没有遇到过这种情况。

Not really a direct answer, but I'd highly recommend using LINQPad for this kind of "exploratory" C# programming. 这不是一个直接的答案,但我强烈建议使用LINQPad进行这种“探索式”C#编程。

I have the following as a saved "query" in LINQPad: 我在LINQPad中将以下内容保存为“查询”:

var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c echo Foo && echo Bar";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardOutput.ReadToEnd().Dump();

Feel free to adapt as needed. 随意根据需要进行调整。

Try fully qualifying the filenames in the arguments - I notice you're specifying the path in the FileName part, so it's possible that the process is being started elsewhere, then not finding the arguments and causing an error. 尝试完全限定参数中的文件名 - 我注意到你在FileName部分中指定了路径,因此有可能在其他地方启动进程,然后找不到参数并导致错误。

If that works, then setting the WorkingDirectory property on the StartInfo may be of use. 如果可以,那么在StartInfo上设置WorkingDirectory属性可能是有用的。

Actually, according to the link 实际上,根据链接

The WorkingDirectory property must be set if UserName and Password are provided. 如果提供了UserName和Password,则必须设置WorkingDirectory属性。 If the property is not set, the default working directory is %SYSTEMROOT%\\system32. 如果未设置该属性,则默认工作目录为%SYSTEMROOT%\\ system32。

Make sure to use full paths, eg not only "video.avi" but the full path to that file. 确保使用完整路径,例如不仅是“video.avi”,而是使用该文件的完整路径。

A simple trick for debugging would be to start a command window using cmd /k <command> instead: 调试的一个简单技巧是使用cmd /k <command>来启动命令窗口:

string ffmpegPath = Path.Combine(path, "ffmpeg.exe");
string ffmpegParams = @"-f image2 -i frame%d.jpg -vcodec"
    + @" mpeg4 -b 800k C:\myFolder\video.avi"

Process ffmpeg = new Process();
ffmpeg.StartInfo.FileName = "cmd.exe";
ffmpeg.StartInfo.Arguments = "/k " + ffmpegPath + " " + ffmpegParams
ffmpeg.Start();

This will leave the command window open so that you can easily check the output. 这将使命令窗口保持打开状态,以便您可以轻松检查输出。

To diagnose better, you can capture the standard output and standard error streams of the external program, in order to see what output was generated and why it might not be running as expected. 为了更好地进行诊断,您可以捕获外部程序的标准输出和标准错误流,以查看生成的输出以及可能无法按预期运行的原因。

Look up: 抬头:

If you set each of those to true, then you can later call process.StandardOutput.ReadToEnd() and process.StandardError.ReadToEnd() to get the output into string variables, which you can easily inspect under the debugger, or output to trace or your log file. 如果将每个设置为true,则可以稍后调用process.StandardOutput.ReadToEnd()process.StandardError.ReadToEnd()以将输出转换为字符串变量,您可以在调试器下轻松检查,或输出到跟踪或您的日志文件。

Very edge case, but I had to use a program that worked correctly only when I specified 非常边缘的情况,但我必须使用只有在我指定时才能正常工作的程序

StartInfo = {..., RedirectStandardOutput = true}

Not specifying it would result in an error. 不指定它会导致错误。 There was not even the need to read the output afterward. 之后甚至没有必要阅读输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM