My ultimate goal is to get a zenity progress dialog to work while encoding with fdkaac.

I first started with some code that is working when I encode "aa.wav" to "aa.mp3" with lame. This results in a progress bar that smoothly updates from 0 to 100%:

lame -m auto -V 4 aa.wav aa.mp3 | awk -vRS='\r' '(NR>3){gsub(/[()%|]/," ");print $2; fflush();}' | zenity --progress --title="Title" --text="encoding" --auto-close

Now I run this code with fdkaac:

fdkaac --profile 2 --bitrate-mode 5 aa.wav -o aa.aac

This results in this screen output at the end of the encode:
[100%] 05:31.227/05:31.227 (43x), ETA 00:00.000
14607096/14607096 samples processed in 00:07.689

During the encode, the first line is printed and the [100%] smoothly updates from 0 to 100 during the encode. At the very end of the encode the second line is printed.

Based on this, I modify the gsub search and replace to this: gsub(/[[%]/," ") to pick out the data.

I now run this code:

fdkaac --profile 2 --bitrate-mode 5 aa.wav -o aa.aac 2>&1 | awk -vRS='\r' '(NR>3){gsub(/[\[%]/," ");print $1; fflush();}' | zenity --progress --title="Title" --text="Encoding" --auto-close

The result is not what I expected. The progress dialog appears with 0% ... and then after some time jumps to 50% ... and then disappears when encoding is complete.

So I take a look at the data going to zenity with this code:

fdkaac --profile 2 --bitrate-mode 5 aa.wav -o aa.aac 2>&1 | awk -vRS='\r' '(NR>3){gsub(/[\[%]/," ");print $1; fflush();}' 

The screen output is not what I expected. 1 thru 50 are all printed at the same time but on consecutive lines and then 50 thru 100 are each printed on consecutive lines when encoding is finished:




and continues on to 50. and then later the output continues on separate lines from 50 to 100 (again printed all at the same time):




So, the problem is apparent ... The output data is printed in two batches (just as seen in the dialog box). And the data is apppearing on consecutive lines. (The lame output after the awk filter is all printed on the same line and updates smoothly).

I suspect the problem has to do with the extra carriage returns, but I don't know how to get rid of them. I tried removing the -vRS='\r command .. but that resulted in no output at all.

I do not understand the subsitution: RS='\r'. Where is the variable RS appearing?

It is also strange to me that the data is printed out at exactly 50% and 100%. Why not 38% or 67%? .. so the data is telling me something, but I'm not sure what it is.

2 Answers 2


The problem may be that fdkaac is buffering its output when it is to a pipe. Try prefixing to the command:

stdbuf -o 0 -e 0 fdkaac ... 2>&1 | ...

where -o is for stdout and -e for stderr.

If you want to try an alternative to awk, you can use a shell script. Make sure your shell is bash then

stdbuf -o 0 -e 0 fdkaac ... 2>&1 | 
while read -d$'\r' junk1 percent junk2
do  echo "$percent"
done) |
zenity --progress --title="Title" --text="encoding" --auto-close

By setting IFS (bash field separator) to include 3 more chars, "%[]", they effectively become like spaces in the input, so the read with delimiter carriage-return (-d) should put the first word of the line in var junk1, the 2nd in percent, and the rest in junk2. We can then just echo the variable which should be holding just the number.

Note: you need to try this without the zenity bit to see if you are getting the number field. I dont know why I had to use a first var junk1, as the percentage should be the first field in the line, but if you dont get the wanted number, try removing the junk1 variable from the read.

  • I figured out how to get the raw data to text file. It does not display properly here here. But every line in the file ends with a carriage return and displays as a new line. I don't see anything abnormal. [1%] 00:02.020/05:31.227 (21x), ETA 00:15.481 [1%] 00:04.040/05:31.227 (24x), ETA 00:13.362 [2%] 00:06.060/05:31.227 (28x), ETA 00:11.804 [2%] 00:08.081/05:31.227 (30x), ETA 00:10.837 [3%] 00:10.101/05:31.227 (31x), ETA 00:10.205 [4%] 00:12.121/05:31.227 (33x), ETA 00:09.794 [4%] 00:14.141/05:31.227 (34x), ETA 00:09.463 [5%] 00:16.161/05:31.227 (34x), ETA 00:09.143
    – daniel
    Oct 13, 2015 at 9:39
  • I tried the stdbuf -o 0 fdkaac ... command ...nothing changed. The raw output also looked identical.
    – daniel
    Oct 13, 2015 at 9:42
  • ok, perhaps fdkaac overrides the stdbuf setting. it doesnt work for all commands. If you just pipe the fdkaac output into cat do the lines appear one by one correctly?
    – meuh
    Oct 13, 2015 at 9:50
  • i'm not sure I understand. I tried fdkaac ... 2>&1>cat Is this what you mean? The screen output didn't look any different (but it goes by very fast as the data is over-writing the same line). They do not appear one by one. . I also took the raw fdkaac data file I created and sent it thru the awk filter. I also created a lame raw data file and sent it thru its awk filter. The final outputs looked very similar. They both contained integers from 1 to 100 line by line (with no extra outputs).
    – daniel
    Oct 13, 2015 at 12:32
  • I don't think the fdkaac is over-riding the buffer settings. If I run the fdkaac command and send the output to screen, I see the % grow from 0 to 100 very smoothly. The datestamp on the raw data file also indicates a smooth output. After going thru the awk filter the situation change. Watching the awk output on screen, the data gets updated only twice (1 thru 50 gets printed when 50 is completed. 51 thru 100 gets printed when encoding is finshed). The lame data goes thru the awk filter & outputs smoothly. The lame raw data file is larger and much more complex than the fdkaac raw data.
    – daniel
    Oct 13, 2015 at 12:42

Let's break down your awk command: awk -vRS='\r' '(NR>3){gsub(/[()%|]/," ");print $1; fflush();}'

The record separator is \r, the field separator is [ \t]+. The characters ()%| will be replaced with space. You are taking the first field.

Based on this format: [100%] 05:31.227/05:31.227 (43x), ETA 00:00.000 The following will be passed to zenity: [100]

Perhaps your awk should be something more like: awk -vRS='\r' '(NR>3){gsub(/[()%|[]]/," ");print $1; fflush();}'

Or even better remove the positional argument: gawk -vRS='\r' 'match($0, /([0-9]+)%/, ary) {print ary[1]}'

  • I tried the your suggestion: gsub(/[()%|[]]/," ") It does not strip off the "[" and the progress bar fails My code of: gsub(/[[%/," ") is working & do not understand the suggestion to change I do not have gawk installed & can not try that option. I'm thinking the issue is related to how the output data is sent to the screen in consecutive lines: 1 2 3 The lame output does not do this. The 2 overwrites the 1. and then the 3 overwrites the 2 ... But the code is the same (except for the gosub). The progess bar failure with my code is that it jumps from 0 to 50 and then to 100.
    – daniel
    Oct 13, 2015 at 4:13
  • Could you provide a sample of the raw output of fdkaac? I suspect what is happening is that sometimes the format of fdkaac matches the format you expect and sometimes it doesn't
    – teambob
    Oct 13, 2015 at 6:15
  • I'm not sure how to provide a sample differently than show above:
    – daniel
    Oct 13, 2015 at 6:46
  • I'm not sure how to provide a sample differently than show above: [100%] 05:31.227/05:31.227 (43x), ETA 00:00.000 14607096/14607096 samples processed in 00:07.689 the second line is printed when encoding is finished. The first line is updated as encoding happens. It starts at 0% and smoothly runs up to 100%. The output is occuring on the same line and data is just overwritten. This is similar to the raw output of lame, except lame outputs several lines (different records) of data. Is there a better way to show you the raw output?
    – daniel
    Oct 13, 2015 at 6:53
  • I'm trying to format the output my comments but it seems impossible and then i run over the 5 minute limit ... and things look strange
    – daniel
    Oct 13, 2015 at 6:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .