Question

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.

Was it helpful?

Solution 3

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];
  }
}

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top