Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
$ ffmpeg -v debug ... 

Coloured output.

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

Dull output.

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

share|improve this question
6  
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. –  vinc17 Aug 22 at 18:26

2 Answers 2

up vote 8 down vote accepted

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
share|improve this answer
    
Very useful, didn't know about script! –  Amphiteót Aug 22 at 19:24
    
How do I run script with argument array instead of a string? –  Vi. Aug 22 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…). –  vinc17 Aug 23 at 0:04
    
@Vi. Is my zsh wrapper script OK for an array in your case? –  vinc17 Aug 23 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. –  Vi. Aug 23 at 10:28

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.

share|improve this answer
    
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. :( –  vinc17 Aug 22 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. –  Vi. Aug 23 at 10:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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