从不断更新的输出中查找输出

发布于 2024-07-24 09:14:42 字数 1140 浏览 7 评论 0原文

我正在尝试围绕 Lame 编写一个简单的脚本来根据我的特定用途自定义程序。 我想做的是从 Lame 输出中解析出完整性百分比。

这就是该行现在的样子:

./lame --nohist ~/Desktop/Driver.wav ~/Desktop/Driver.mp3 2>&1| egrep -o "\([0-9\%]+\)"

但这不会返回任何内容。 Lame 的输出如下所示:

    LAME 3.99 (alpha 1, Jun  4 2009 19:42:31) 32bits (http://www.mp3dev.org/)
    warning: alpha versions should be used for testing only
    Using polyphase lowpass filter, transition band: 16538 Hz - 17071 Hz
    Encoding /Users/jkubicek/Desktop/Driver.wav
        to /Users/jkubicek/Desktop/Driver.mp3
    Encoding as 44.1 kHz j-stereo MPEG-1 Layer III (11x) 128 kbps qval=3
    Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
    1500/8765   (17%)|    0:02/    0:15|    0:03/    0:17|   14.654x|    0:14

最后一行代码随着文件转换而动态更新。 当我将这个确切的文本复制/粘贴/回显/通过管道传输到我的 grep 中时,它发现 17% 就很好,但是当我真正运行它时,它发现了零。

编辑: 当我将 lame 的输出放入文本文件时,结果如下所示:

lameout.txt

看起来我可以将输出推送到临时文件并从那里读取完成的百分比,但是那感觉很尴尬,好像应该有一种更优雅的方式来做到这一点。

I'm trying to write a simple script around Lame to customize the program for my specific uses. What I'd like to do is parse out just the percent completeness from the Lame output.

Here's what the line looks like now:

./lame --nohist ~/Desktop/Driver.wav ~/Desktop/Driver.mp3 2>&1| egrep -o "\([0-9\%]+\)"

But that returns nothing. Here's what the output from Lame looks like:

    LAME 3.99 (alpha 1, Jun  4 2009 19:42:31) 32bits (http://www.mp3dev.org/)
    warning: alpha versions should be used for testing only
    Using polyphase lowpass filter, transition band: 16538 Hz - 17071 Hz
    Encoding /Users/jkubicek/Desktop/Driver.wav
        to /Users/jkubicek/Desktop/Driver.mp3
    Encoding as 44.1 kHz j-stereo MPEG-1 Layer III (11x) 128 kbps qval=3
    Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
    1500/8765   (17%)|    0:02/    0:15|    0:03/    0:17|   14.654x|    0:14

The last line of code dynamically updates as the file is converted. When I copy/paste/echo/pipe this exact text into my grep it finds the 17% just fine, but when I run it for real, it finds zilch.

Edit:
When I throw the output from lame into a text file, here is what the results look like:

lameout.txt

It looks like I could push the output to a temp file and read the percentage complete out of there, but that feels awkward, like there should be a more elegant way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友欢 2024-07-31 09:14:42

我怀疑你可能无法做到这一点。 百分比输出可能会通过 curses 发送到终端(以允许输入 -进行动态更新),因此通过标准输出的输出将受到限制。

将输出重定向到文件并查看其中写入的内容可能是值得的。 IE

lame > /tmp/lame.log

I suspect you may not be able to do this. The percentage output will probably go to the terminal via curses (to permit in-place dynamic updating), and so there'll be a limited output via stdout.

It may be worth redirecting the output to a file and see what gets written there. i.e.

lame > /tmp/lame.log
独夜无伴 2024-07-31 09:14:42

当未连接到终端时,lame 可能不会以相同的方式输出此信息。 尝试在末尾运行“> output.txt”的蹩脚命令,并查看附加到另一个进程时它打印的内容。

另一种很可能的可能性是“17%”从未真正打印出来。 打印的可能是:

%,向左移动,1,向左移动,2,向左移动3,...向左移动,向左移动,1, 7,向左移动8,等等。

lame is probably not outputting this information in the same way when not connected to a terminal. Try running your lame command with at the end "> output.txt" and look at what it's printing when attached to another process.

The other very likely possibility is that "17%" is never actually printing out. What probably is printing is:

%, move left, 1, move left, 2, move left 3, ... move left, move left, 1, 7, move left 8, etc.

独留℉清风醉 2024-07-31 09:14:42

我最终使用 NSScanner 来解析输出。 NSTask 中的每一行都被发送到此方法:

- (NSNumber *)parseOutputString:(NSString *)output {
  NSScanner *scanner = [NSScanner scannerWithString:output];
  NSString *endString = @"% complete";
  NSInteger percentComplete;
  BOOL didFindNumber = NO;

  while (![scanner scanString:endString intoString:nil]) {
        [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
        didFindNumber = [scanner scanInteger:&percentComplete];

        if ([scanner isAtEnd]) {
              didFindNumber = NO;
              break;
        }
  }

  if (didFindNumber) {
        return [NSNumber numberWithInteger:percentComplete];
  } else {
        return [NSNumber numberWithInt:0];
  }
}

I ended up using NSScanner to parse the output. Each line from the NSTask was sent to this method:

- (NSNumber *)parseOutputString:(NSString *)output {
  NSScanner *scanner = [NSScanner scannerWithString:output];
  NSString *endString = @"% complete";
  NSInteger percentComplete;
  BOOL didFindNumber = NO;

  while (![scanner scanString:endString intoString:nil]) {
        [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
        didFindNumber = [scanner scanInteger:&percentComplete];

        if ([scanner isAtEnd]) {
              didFindNumber = NO;
              break;
        }
  }

  if (didFindNumber) {
        return [NSNumber numberWithInteger:percentComplete];
  } else {
        return [NSNumber numberWithInt:0];
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文