You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#调用ffprobe参数不识别,重定向符号报错求助

C#调用ffprobe重定向输出报错的解决方法

在CMD中执行以下命令可正常运行:
ffprobe -v quiet -loglevel repeat+level+verbose -print_format json -show_format -show_streams -show_optional_fields always "http://hello.com" > "C:\\lolwut.mp4.json" 2> "C:\\test.txt"

但在C#中编写如下代码调用时出现错误:

string path = "C:\\\\";
string filename = Path.Combine(path, "ffprobe.exe");
string cParams = @" -v quiet -loglevel repeat+level+verbose -print_format json -show_format -show_streams -show_optional_fields always -i "http://hello.com" > "lolwut.mp4.json" > "test.txt"";
var proc = System.Diagnostics.Process.Start(filename, cParams);

错误信息:Argument '>' provided as input filename, but ''http://example.com/'' was already specified
尝试用单引号包裹文件名无效,询问如何正确编写参数。

解决方法

  • 别直接在参数里写>:CMD里的>和2>是命令行Shell的语法,用来处理输出重定向,但Process.Start直接调用程序时,不会通过Shell解析这些符号,ffprobe会把>当成输入文件名,自然报错。
  • 用Process的输出重定向属性处理:在C#里,应该通过ProcessStartInfo的相关属性捕获输出,再手动写入文件,示例代码如下:
string path = "C:\\";
string ffprobePath = Path.Combine(path, "ffprobe.exe");
string inputUrl = "http://hello.com";
string outputJsonPath = Path.Combine(path, "lolwut.mp4.json");
string errorLogPath = Path.Combine(path, "test.txt");

var startInfo = new ProcessStartInfo(ffprobePath)
{
    Arguments = $"-v quiet -loglevel repeat+level+verbose -print_format json -show_format -show_streams -show_optional_fields always -i \"{inputUrl}\"",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false, // 必须设为false才能启用输出重定向
    CreateNoWindow = true
};

using (var proc = Process.Start(startInfo))
{
    // 读取标准输出和错误输出,写入对应文件
    File.WriteAllText(outputJsonPath, proc.StandardOutput.ReadToEnd());
    File.WriteAllText(errorLogPath, proc.StandardError.ReadToEnd());
    proc.WaitForExit();
}
  • 注意参数引号:像URL这类可能包含特殊字符的参数,要用双引号包裹,避免参数解析出错。如果用字符串插值,要确保引号转义正确。

内容的提问来源于stack exchange,提问作者user46539

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 21:22:46