2

TLDR:

How do I run handbrakecli, get progress and ETA, pipe into dialog without flickering.

Details:

I have a script that finds all video files in a directory and then runs handbrakecli on them one at a time. Everytime handbrakecli runs it spits out a bunch of information about the file etc. and then the line:

Encoding: task 1 of 1, 3.30 % (295.53 fps, avg 303.23 fps, ETA 00h02m54s)

In my script I pipe the handbrakecli command to some other commands to filter out everything except the progress and then I pipe that to dialog like so:

Snippet 1

HandBrakeCLI --preset "Normal" -i "$f" -o "$DEST" | \
    stdbuf -o0 tr -s '\r' '\n' | \
    stdbuf -o0 grep -oP '(?<=, )\d+(?=\.\d\d \%)' | \
    dialog --gauge "$DIALOG_MSG" 10 70;

This works fine. It shows the progress and it doesn't flicker. Second task was to come up with a way to show both the progress AND the ETA and this is what I came up with:

Snippet 2

HandBrakeCLI --preset "Normal" -i "$f" -o "$DEST_FULL_FILE" | \
    stdbuf -oL tr -s '\r' '\n' | \
    while read -r str; do
        local REMAINING=$(echo "$str" | grep -oP "(?<=ETA )\d\dh\d\dm\d\ds(?=\))");
        local PROGRESS=$(echo "$str" | grep -oP "(?<=, )\d+(?=.\d\d)");
        echo "$PROGRESS" | dialog --gauge "$DIALOG_MSG Time remaining: $REMAINING" 10 70;
    done

This works but the dialog window flickers. I don't know if it's performance related (doubt it) or if the whole while read loop just doesn't work well with dialog.

I also tried doing it without piping to dialog but that just showed dialog once and then didn't update it at all.

Snippet 3

HandBrakeCLI --preset "Normal" -i "$f" -o "$DEST_FULL_FILE" | \
    stdbuf -oL tr -s '\r' '\n' | \
    while read -r str; do
        local REMAINING=$(echo "$str" | grep -oP "(?<=ETA )\d\dh\d\dm\d\ds(?=\))");
        local PROGRESS=$(echo "$str" | grep -oP "(?<=, )\d+(?=.\d\d)");
        dialog --gauge "$DIALOG_MSG Time remaining: $REMAINING" 10 70 $PROGRESS;
    done

Finally I tried this:

Snippet 4

HandBrakeCLI --preset "Normal" -i "$f" -o "$DEST_FULL_FILE" | \
    stdbuf -o0 tr -s '\r' '\n' | (
        read -r str;                                                                                                                          
        local REMAINING=$(echo "$str" | grep -oP "(?<=ETA )\d\dh\d\dm\d\ds(?=\))");
        local PROGRESS=$(echo "$str" | grep -oP "(?<=, )\d+(?=.\d\d)");
        echo "$PROGRESS";
    ) | dialog --gauge "$DIALOG_MSG Time remaining: $REMAINING" 10 70;

And that didn't work out well at all.

I'm aware there are some fundamental things wrong here.

1

Snippet 2 is closest. dialog --gauge will optionally look for the magic marker XXX in the input and then read the new percentage from the following line, and the new prompt from the lines after that until a new marker.

So you should get what you want with:

HandBrakeCLI --preset "Normal" -i "$f" -o "$DEST_FULL_FILE" |
stdbuf -oL tr -s '\r' '\n' |
while read -r str
do
    REMAINING=$(echo "$str" | grep -oP "(?<=ETA )\d\dh\d\dm\d\ds(?=\))")
    PROGRESS=$(echo "$str" | grep -oP "(?<=, )\d+(?=.\d\d)")
    echo -e "XXX\n$PROGRESS\n$DIALOG_MSG Time remaining: $REMAINING\nXXX"
done | 
dialog --gauge "$DIALOG_MSG Time remaining: " 10 70;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.