从不断更新的输出中查找输出
我正在尝试围绕 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 的输出放入文本文件时,结果如下所示:
看起来我可以将输出推送到临时文件并从那里读取完成的百分比,但是那感觉很尴尬,好像应该有一种更优雅的方式来做到这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑你可能无法做到这一点。 百分比输出可能会通过 curses 发送到终端(以允许输入 -进行动态更新),因此通过标准输出的输出将受到限制。
将输出重定向到文件并查看其中写入的内容可能是值得的。 IE
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 可能不会以相同的方式输出此信息。 尝试在末尾运行“> 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.
我最终使用 NSScanner 来解析输出。 NSTask 中的每一行都被发送到此方法:
I ended up using
NSScanner
to parse the output. Each line from theNSTask
was sent to this method: