如何使用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
相关产品推荐
相关产品推荐

