$ ffmpeg -v debug ... Coloured output.
$ ffmpeg -v debug ... |& less -R Dull output.
How do I make the output coloured while piping it to something?
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 script! script with argument array instead of a string? script invocation (not easy, but perhaps it has already been done somewhere…). 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.
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. :( /dev/null to make it happy. I've submitted a pull request to reptyr to make it continue if input is not a terminal. 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.
less, but withffmpeg, which seems to disable coloring when stdout is not associated with a terminal. Some utilities likegrephave a--color=alwaysoption. You should look at whatffmpegprovides.