1

I am running some commands in bash (basically some ffmpeg commands), that I'm using grep and awk to filter out the results. The command takes some time and continously outputs some results as it progresses through the video. The grep pipe is the same. But the awk pipe waits until the command is completed and prints all at once, which is not good. I want it to output as soon as it found a match.

How to change this command to output results in real-time as it progresses?

ffmpeg -i freeze.mp4 -vf "freezedetect=n=-60dB:d=2" -map 0:v:0 -f null - 2>&1 | grep freezedetect | awk '{print $4,$5}' | tr -d , | grep lavfi

2 Answers 2

3

With standard tools, try

ffmpeg -i freeze.mp4 -vf "freezedetect=n=-60dB:d=2" -map 0:v:0 -f null - 2>&1 |  stdbuf -o 0 grep freezedetect | stdbuf -o 0 awk '{print $4,$5}' | stdbuf -o 0 tr -d , | stdbuf -o 0 grep lavfi
3
  • This one still prints all at once!
    – Mary
    Commented Apr 25, 2019 at 19:05
  • 1
    You may want to add stdbuf to all the following commands as well
    – Gyan
    Commented Apr 25, 2019 at 19:14
  • 2
    Yes. Adding stdbuf -o 0 to all pipes does the trick.
    – Mary
    Commented Apr 25, 2019 at 19:20
1

expect provides a tool, unbuffer, which you can use to solve this. It will connect to your long-running ffmpeg process with a pseudoterminal, which makes the system act as though it's interactive rather than using the 4K buffer that pipelines usually incorporate:

$ unbuffer ffmpeg [...] | grep [...] | awk [...]

Further, you can combine the grep and awk into one operation:

Rather than:

[...] | grep freezedetect | awk '{print $4,$5}' | tr -d , | grep lavfi

Instead, you can:

[...] | awk '/freezedetect/ && ( $4 /lavfi/ || $5 /lavfi/ ) {print $4,$5}' | tr -d ,
2
  • 1
    how to get unbuffer? Standard tools would be better.
    – Mary
    Commented Apr 25, 2019 at 19:06
  • unbuffer comes with expect. I agree that standard tools, all things being equal, are better; I hadn't recalled that grep came with --line-buffered.
    – DopeGhoti
    Commented Apr 25, 2019 at 19:13

You must log in to answer this question.

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