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

如何使用Medallion.Shell实现控制台输出过滤?

使用Medallion.Shell实现FFmpeg输出过滤

要实现和你之前用ProcessInfo相同的输出过滤效果,无需直接将标准错误流重定向到控制台,而是通过Medallion.Shell的流监听方法逐行处理并过滤内容:

using Medallion.Shell;

var command = Command.Run(ffMpegBinaryPath, args);
// 保留标准输入输出的重定向(如果业务需要)
command.RedirectTo(Console.OpenStandardOutput());
command.RedirectFrom(Console.OpenStandardInput());

// 异步监听标准错误流,按规则过滤输出
await command.StandardError.ListenAsync(line =>
{
    if (!string.IsNullOrEmpty(line) && line.StartsWith("frame=", StringComparison.InvariantCulture))
    {
        Console.WriteLine(line);
        Console.SetCursorPosition(0, Console.CursorTop - 1);
    }
});

// 等待命令执行完成,避免程序提前退出
await command.Task;

关键说明:

  • ListenAsync是Medallion.Shell为流提供的扩展方法,会异步读取标准错误流的每一行并触发处理委托,和你之前使用ErrorDataReceived事件的逻辑完全匹配。
  • 过滤逻辑和原代码一致:仅保留以frame=开头的行,输出后调整光标位置实现行覆盖效果。
  • 必须调用await command.Task等待命令执行完成,否则程序可能在FFmpeg结束前就退出,导致输出不完整。
  • 若需同步处理,可替换为command.StandardError.Listen(line => { ... }),但异步方式更适合避免阻塞主线程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 15:42:46