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

C#程序调用yt-dlp出现参数解析错误的问题求助

问题分析与解决

你的核心问题是在UseShellExecute = false的情况下,把shell的重定向符号>当成了yt-dlp的参数传递,而yt-dlp会将其识别为无效URL,从而触发错误。

当你直接在命令行运行时,系统shell(比如cmd或PowerShell)会处理>符号,将命令输出写入指定文件;但在C#中设置UseShellExecute = false后,进程会直接启动yt-dlp,所有参数(包括>和文件名)都会被yt-dlp当作需要处理的输入内容,自然会报错。

解决方案

1. 移除参数中的重定向部分,改用C#进程输出重定向处理文件写入

既然你已经设置了RedirectStandardOutput = true,直接通过C#读取进程输出并写入文件即可,不需要依赖shell的重定向功能。

2. 修正参数拼接逻辑,避免引号转义问题

使用字符串插值简化参数拼接,减少手动转义引号时可能出现的格式错误。

修改后的完整代码

ProcessStartInfo runYtDlp = new ProcessStartInfo();
runYtDlp.FileName = ytDlpPath;
// 移除重定向部分,只保留yt-dlp本身需要的参数
string ytDlpArgs = $"--skip-download --ignore-errors --flat-playlist --playlist-random --force-overwrites --print \"https://www.youtube.com/embed/%(id)s?controls=0&autoplay=1&cc_load_policy=1&cc_lang_pref=en    %(title)s   %(duration)s\" \"{videoPlaylists["alpha"]}\"";
runYtDlp.Arguments = ytDlpArgs;

runYtDlp.UseShellExecute = false;
runYtDlp.RedirectStandardOutput = true;

// 使用using管理进程和文件写入流,确保资源正确释放
using Process process = Process.Start(runYtDlp);
using StreamWriter writer = new StreamWriter(Path.Combine(operatingDir, "alpha.txt"));

// 逐行读取输出并写入文件,避免大输出占用过多内存
while (!process.StandardOutput.EndOfStream)
{
    string line = process.StandardOutput.ReadLine();
    writer.WriteLine(line);
}

process.WaitForExit();
Console.ReadKey();

额外说明

  • 用Path.Combine拼接文件路径,避免因操作系统路径分隔符差异导致的错误。
  • 如果不需要向yt-dlp输入内容,可以去掉RedirectStandardInput = true的设置,减少不必要的资源占用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:53:15