如何在Process.Arguments中传递复杂的命令行参数?

时间:2015-06-04 22:40:40

标签: c# powershell ffmpeg

在C#中,我有一组复杂的参数传递给Process.Arguments。我通过使用已编译的Powershell的单行脚本来规避问题。争论的确切“训练”是:

ffmpeg -framerate $fr -f image2 -i $temp\JPEG%01d -c:v libx264 -threads $threads -crf $encqual $video_out

有问题的参数是-i(带有JPEG%01模式)&amp; -c参数。我尝试过双重逃避,但我尝试的任何方式都无法正常工作。我不喜欢/<arg>: <val>符号,因为它与-c:v冲突。如上所述,我通过将其传递给PowerShell来实现它,但这是一个黑客攻击。该目录也会被解析,因为它有空格。

ERROR:

“无法为'C:\ Program'找到合适的输出格式 “C:\ Program”参数无效。

CODE:

Process proc = new Process();
// proc.StartInfo = _fw.CreateProcessStartInfo4Ffmpeg(
      pathTempDir,    output);
proc.StartInfo = _fw.CreateProcessStartInfo4AviGen(pathTempDir, output);
proc.Start();

// doesn't work yet
public ProcessStartInfo CreateProcessStartInfo4Ffmpeg(string tempFile,
       string output)
{
   string args = FormCmdArgsForFfmpeg(this, tempFile, output);
   ProcessStartInfo psi = CreateProcessStartInfo4Ffmpeg(this, args);
return psi;
}

public static ProcessStartInfo CreateProcessStartInfo4Ffmpeg(
        FfmpegWrapper fw, string args)
 {
   string sExePath = clsRegistry.regInfo.FfmpegExePath;
   ProcessStartInfo pi = new ProcessStartInfo(sExePath);

   pi.UseShellExecute = false;
   pi.CreateNoWindow = false;
   pi.WindowStyle = ProcessWindowStyle.Normal;

   pi.Arguments = String.Copy(args);
   string sCmdFmtdMsg = string.Format("Command: ", 
           sExePath, pi.Arguments);
return pi;
} // end CreateProcessStartInfo4AviGen()

// N.B. doesn't work yet; args prob
public static string FormCmdArgsForFfmpeg(FfmpegWrapper fw, 
     string    tempDir, string output)
{
 /* -framerate $framerate -f image2 -i $temp_dir_jpgs\JPEG%01d 
 -c:v libx264 -threads $threads -crf $encode_quality $video_out
 */
    string sFmtdArg2 = "-{0} \"{1}\" ";
    string sFmtdArg1 = "-{0} ";
    string sArgs = String.Empty;
    string sout = null;
    string pathInput = Path.Combine(tempDir, "JPEG%01d");

    StringBuilder sb = new StringBuilder("");
    sb.Append(string.Format(sFmtdArg2, "framerate", fw.framerate));
    sb.Append(string.Format(sFmtdArg2, "f", "image2"));
    sb.Append(string.Format(sFmtdArg2, "i", pathInput));
    sb.Append(string.Format(sFmtdArg2, "c:v", "libx264"));
    sb.Append(string.Format(sFmtdArg2, "threads", fw.threads) );
    sb.Append(string.Format(sFmtdArg2, "crf", fw.encode_quality));
    sb.Append(output);
    sout = sb.ToString();
return sout;
}

0 个答案:

没有答案