bashcolorsoutputyt-dlp

How to colorize output from bash command?


I've seen many similar questions, but none really works for me.
I have a simple script that downloads latest 2 video episodes from one channel, so I can watch them later on mobile in offline mode. It uses yt-dlp command with -w switch not to overwrite if video exists. I would like to colorize "Destination" so I see more clearly file name, and "has already been downloaded" if file has already been downloaded.
So my script looks like this:

    cd /tmp
EPI=$(curl -s https://rumble.com/c/robbraxman | grep  -m 2 "href=/\v"| awk '{print $5}' |awk -F\> '{print $1}')
for i in ${EPI//href=/http://rumble.com}; do
  printf "\e[1;38;5;212m Downloading \e[1;38;5;196m $i\e[m \n"
  yt-dlp -f mp4-480p -w $i
done

output of the yt-dlp is like:

[Rumble] Extracting URL: http://rumble.com/v2820uk-let-me-show-you-brax.me-privacy-focused-app.html
[Rumble] v2820uk-let-me-show-you-brax.me-privacy-focused-app.html: Downloading webpage
[RumbleEmbed] Extracting URL: https://rumble.com/embed/v25g3g0
[RumbleEmbed] v25g3g0: Downloading JSON metadata
[info] v25g3g0: Downloading 1 format(s): mp4-480p
[download] Let Me Show You Brax.Me - Privacy Focused App [v25g3g0].mp4 has already been downloaded
[download] 100% of   20.00MiB



[Rumble] Extracting URL: http://rumble.com/v2820uk-let-me-show-you-brax.me-privacy-focused-app.html
[Rumble] v2820uk-let-me-show-you-brax.me-privacy-focused-app.html: Downloading webpage
[RumbleEmbed] Extracting URL: https://rumble.com/embed/v25g3g0
[RumbleEmbed] v25g3g0: Downloading JSON metadata
[info] v25g3g0: Downloading 1 format(s): mp4-480p
[download] Destination: Let Me Show You Brax.Me - Privacy Focused App [v25g3g0].mp4
[download]   3.8% of  525.67MiB at    5.79MiB/s ETA 01:27^C

So I would probably need to pipe output somehow, and then color it. yt-dlp actually colors progress when downloading. I've tried to put grc in front of yt-dlp but didn't color it.


Solution

  • You can colorize specific things you want using grep:

    yt-dlp 'URL' | grep --color -P "^|(Destination.*|.*has already been downloaded)" 
    

    Basically it will match all lines with ^, so those lines will still be printed, however since this match contains no visible characters those lines will not be colored.

    Then you just add in the parts you want colored after the regex |. The --color shouldn't be neccesary, just adding it to make sure that is not the issue.