0

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
1

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
  • This one still prints all at once! – Tina J Apr 25 at 19:05
  • 1
    You may want to add stdbuf to all the following commands as well – Gyan Apr 25 at 19:14
  • Yes. Adding stdbuf -o 0 to all pipes does the trick. – Tina J Apr 25 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 ,
  • how to get unbuffer? Standard tools would be better. – Tina J Apr 25 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 Apr 25 at 19:13

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.