8
$ ffmpeg -v debug ... 

Coloured output.

$ ffmpeg -v debug ... |& less -R 

Dull output.

How do I make the output coloured while piping it to something?

1
  • 7
    The problem is not with less, but with ffmpeg, which seems to disable coloring when stdout is not associated with a terminal. Some utilities like grep have a --color=always option. You should look at what ffmpeg provides. Commented Aug 22, 2014 at 18:26

2 Answers 2

13

For commands that do not have an option similar to --color=always, you can do, e.g. with your example:

script -c "ffmpeg -v debug ..." /dev/null < /dev/null |& less -R 

What script does is that it runs the command in a terminal session.

EDIT: Instead of a command string, if you want to be able to provide an array, then the following zsh wrapper script seems to work:

#!/usr/bin/env zsh script -c "${${@:q}}" /dev/null < /dev/null |& less -R 
8
  • Very useful, didn't know about script! Commented Aug 22, 2014 at 19:24
  • How do I run script with argument array instead of a string? Commented Aug 22, 2014 at 23:34
  • @Vi. I suspect that an argument array isn't used for some reason (same things for the zsh zsh/zpty module). If this is really important, you can write a wrapper that will accept an array and generate the right script invocation (not easy, but perhaps it has already been done somewhere…). Commented Aug 23, 2014 at 0:04
  • @Vi. Is my zsh wrapper script OK for an array in your case? Commented Aug 23, 2014 at 0:19
  • @vinc17, I already have such script: stackoverflow.com/questions/3069163/… . It does not try to quote anything (which I consider insecure), but uses base64 and explicit deserializer. Commented Aug 23, 2014 at 10:28
0

As alternative to script, which expectes command line as a string instead of normal array, there is a special mode of reptyr:

reptyr -L ffmpeg -v debug ... < /dev/null |& less -R 

Note that not all versions of reptyr have working -L option and accept < /dev/null gracefully. See my pull request for this.

3
  • Under Debian/unstable, reptyr -L ... doesn't work ("Unable to read terminal attributes: Inappropriate ioctl for device"), and with the -l option, the color is lost just like with a normal pipe. :( Commented Aug 22, 2014 at 23:55
  • @vinc17, Try feeding some other valid terminal instead of /dev/null to make it happy. I've submitted a pull request to reptyr to make it continue if input is not a terminal. Commented Aug 23, 2014 at 10:26
  • Anyone know how to keep frame= 206 fps= 29 q=-0.0 Lsize= 607kB time=00:00:06.80 bitrate= 731.7kbits/s speed=0.97x printed. Those line disappear when piped. Commented Mar 10, 2021 at 4:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.