XMonad

~/.xmonad/scripts/ncmpcpp_coverart.sh

Rina Kawakita 2018. 10. 14. 23:46

~/.xmonad/scripts/ncmpcpp_coverart.sh

#!/bin/bash

MUSIC_DIR="$HOME/Music/"

COVER=/tmp/cover.jpg

function reset_background
{
    printf "\e]20;;100x100+1000+1000\a"
}

if [[ -n "$(pgrep mopidy)" ]] ; then
    MPRIS_URL="$(playerctl -p mopidy metadata xesam:url)"
    MPRIS_ARTURL="$(playerctl -p mopidy metadata mpris:artUrl)"
    MPRIS_ARTURL_PREFIX=""
    rm -f /tmp/mopidy_coverart_temp.jpg


    if ( echo "$MPRIS_ARTURL" | grep -qi "/images/" )
    then
        #echo "local track"      # possible download coverart
        MPRIS_ARTURL_PREFIX="http://127.0.0.1:6680"
        MPRIS_ARTURL=$MPRIS_ARTURL_PREFIX$MPRIS_ARTURL
        #echo $MPRIS_ARTURL
        wget "$MPRIS_ARTURL" -O /tmp/mopidy_coverart_temp.jpg >/dev/null 2>&1;
    elif ( echo "$MPRIS_URL" | grep -qi "file://" )
    then
        # impossible download coverart . file path ??
        #echo "file:// is local file url string. $MPRIS_URL :  How to convert local file path string?"
        reset_background
        exit 0;
    else
        #echo "Streaming track: pass MPRIS http url"
        wget "$MPRIS_ARTURL" -O /tmp/mopidy_coverart_temp.jpg >/dev/null 2>&1;
    fi

    convert "/tmp/mopidy_coverart_temp.jpg" -resize 280x -background "#312e39" \
        -extent 1100x280 "$COVER" >/dev/null 2>&1;
    printf "\e]20;${COVER};100x100+103+25:op=keep-aspect\a"

    #echo $MPRIS_ARTURL;
    exit 0;
fi

{
    album="$(mpc --format %album% current)"
    file="$(mpc --format %file% current)"
    album_dir="${file%/*}"
    [[ -z "$album_dir" ]] && exit 1
    album_dir="$MUSIC_DIR/$album_dir"

    covers="$(find "$album_dir" -type d -exec find {} -maxdepth 1 -type f -iregex ".*/.*\(${album}\|cover\|folder\|artwork\|front\).*[.]\(jpe?g\|png\|gif\|bmp\)" \; )"
    src="$(echo -n "$covers" | head -n1)"

    rm -f "$COVER" 
    rm -f "/tmp/tmp1234567.jpg"
    src="$(echo $COVER)"
    #ffmpeg -i "$MUSIC_DIR/`mpc current -f '%file%'`" -vf scale=400:-1 \
    ffmpeg -i "$MUSIC_DIR/`mpc current -f '%file%'`" \
        -y "/tmp/tmp1234567.jpg" >/dev/null 2>&1;


    if [[ -n "$src" ]] ; then
        #resize the image's height to 300px & extent it to cover the urxvt length
        #convert "$src" -resize 300x -background "#2f343f" -extent 1100x400 "$COVER"
        convert "/tmp/tmp1234567.jpg" -resize 280x -background "#312e39" \
            -extent 1100x280 "$COVER" >/dev/null 2>&1;
        if [[ -f "$COVER" ]] ; then
           #-- original script --
           #scale down the cover to 30% of the original
           #place it 1% away from left and 50% away from top.
           #printf "\e]20;${COVER};70x70+0+4:op=keep-aspect\a"
           #---------------------
           #no need to scale down, i have extent it
           #printf "\e]20;${COVER};125x125+122+25:op=keep-aspect\a"
           #printf "\e]20;${COVER};125x125+0+25:op=keep-aspect\a"
           printf "\e]20;${COVER};100x100+0+0:op=keep-aspect\a"
        else
            reset_background
        fi
    else
        reset_background
    fi
}