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