gpt4 book ai didi

c# - 为什么处理输出阅读器返回空数据?当其他进程中的相同代码正常工作时

转载 作者:行者123 更新时间:2023-12-04 23:07:51 25 4
gpt4 key购买 nike

我尝试从 ffmpeg 进程获取输出,但无法获取输出。
在另一个进程和命令中,它可以正常工作,但在启动时立即返回输出!

        using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo()
{
FileName = LinkHelper.IPFS_PATH,
Arguments = cmd,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};

process.ErrorDataReceived += FfmpegErrorRecieved;
process.Start();

using (StreamReader reader = process.StandardOutput)
{
string output = await reader.ReadToEndAsync();
Console.WriteLine(output);
}
process.WaitForExit();
}

Before output handle!

更新:
正如 szatmary 所说,ffmpeg 使用输出错误而不是标准输出,所以当你初始化 process.StandartInfo 时不要忘记将属性“RedirectStandardError”初始化为 TRUE!

这是正确的代码:
private async Task DetectFFmpegCamerasAsync()
{
var cmd = "-list_devices true -f dshow -i dummy";
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo()
{
FileName = LinkHelper.FFMPEG_PATH,
Arguments = cmd,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true
};

process.Start();

using (StreamReader reader = process.StandardError)
{
string output = await reader.ReadToEndAsync();
Console.WriteLine($"Camera detection output: \n {output}");
}
process.WaitForExit();
}
}

最佳答案

ffmpeg将其输出写入 stderr而不是 stdout .
因此,您必须阅读 standard error而不是标准输出。

因此,请改用以下几行:

using (StreamReader reader = process.StandardError)
{
string output = await reader.ReadToEndAsync();
Console.WriteLine(output);
}

关于c# - 为什么处理输出阅读器返回空数据?当其他进程中的相同代码正常工作时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57609413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com