~/.local/bin/mpsytctl
#!/bin/bash
###################################################################################################
# Summary : mps-youtube control for wm keybinding, notification, open browser, download track
# (experimental : mpris app control & notification)
#
# Dependencies : mpv, mpsyt, dunst, playerctl, youtube-dl, paplay,(mopidy,spotify, gpmdp, ...)
#
# by nietz
###################################################################################################
MPRIS_APP="mps-youtube" # "mps-youtube", "mopidy", "spotify", "google-play-music-desktop-player", ...
MPRIS_NAME="$(playerctl -l | grep -i $MPRIS_APP)"
if [ "$MPRIS_NAME" = "" ]
then
echo "$MPRIS_APP is not running..."
exit 0
fi
MPRIS_STATUS="$(playerctl -p $MPRIS_NAME status)"
if [ "$MPRIS_STATUS" = "Stopped" ]
then
echo "$MKPRIS_APP is Stoped"
exit 0
fi
MPRIS_ARTURL="$(playerctl -p $MPRIS_NAME metadata mpris:artUrl)"
MPRIS_ARTURL_PREFIX=""
if [ "$MPRIS_APP" = "mopidy" ]
then
if ( echo "$MPRIS_ARTURL" | grep -qi "/images/" )
then
echo "local track" # possible download coverart
MPRIS_ARTURL_PREFIX="http://192.168.0.6:6680"
MPRIS_ARTURL=$MPRIS_ARTURL_PREFIX$MPRIS_ARTURL
echo $MPRIS_ARTURL
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?"
else
echo "Streaming track: pass MPRIS http url"
fi
fi
MPRIS_TITLE="$(playerctl -p $MPRIS_NAME metadata xesam:title)"
MPRIS_TRACK_ID="$(playerctl -p $MPRIS_NAME metadata mpris:trackid)"
if [ ! "$MPRIS_APP" = "mps-youtube" ]
then
MPRIS_ALBUM="$(playerctl -p $MPRIS_NAME metadata xesam:album)"
MPRIS_ALBUM_ARITIS="$(playerctl -p $MPRIS_NAME metadata xesam:albumArtist)"
MPRIS_ARTIST="$(playerctl -p $MPRIS_NAME metadata xesam:artist)"
MPRIS_URL="$(playerctl -p $MPRIS_NAME metadata xesam:url)"
echo $MPRIS_URL
else
#YOUTUBE_TRACK_ID="$(playerctl -p $MPRIS_NAME metadata mpris:trackid | sed 's/\/CurrentPlaylist\/ytid\///' | sed "s/'//g" )"
YOUTUBE_TRACK_ID="$(echo $MPRIS_TRACK_ID | \
sed 's/\/CurrentPlaylist\/ytid\///' | sed "s/'//g")"
YOUTUBE_URL_PREFIX="https://www.youtube.com/watch?v="
YOUTUBE_URL=$YOUTUBE_URL_PREFIX$YOUTUBE_TRACK_ID
fi
MPRIS_VOLUME="$(playerctl -p $MPRIS_NAME volume)"
MPRIS_VOLUME_LEVEL=$(echo "$MPRIS_VOLUME" | awk '{print ($A)*100}')%
MPRIS_POS="$(playerctl -p $MPRIS_NAME position)"
MPRIS_LENGTH="$(playerctl -p $MPRIS_NAME metadata mpris:length)"
TOTAL_LENGTH=$(echo "$MPRIS_LENGTH" | awk '{printf "%.3f", ($MPRIS_LENGTH)/1000000}')
OLD_COVERART_URL=$(cat /home/nietz/.xmonad/.mpsyt_coverart)
NEW_COVERART_URL=$MPRIS_ARTURL
DUNST_APPNAME="Mpsyt" # ~/.config/dunst/dunstrc profile name
if [ "$MPRIS_NAME" == "mopidy" ]
then
DUNST_APPNAME="Mopidy"
echo $DUNST_APPNAME
elif [ "$MPRIS_NAME" = "spotify" ]
then
DUNST_APPNAME="Spotify"
elif [ "$MPRIS_NAME" = "google-play-music-desktop-player" ]
then
DUNST_APPNAME="google-play-music-desktop-player"
else
DUNST_APPNAME="Mpsyt"
fi
ICON="/tmp/mpsyt_coverart.jpg"
DUNST_POS_STRING="$MPRIS_POS / $TOTAL_LENGTH (sec)"
CMD=""
YOUTUBE_DL_FORMAT="bestvideo[height<=?2160][fps<=?60][protocol!=http_dash_segments][ext=mp4]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio/bestaudio[ext=webm])/bestvideo+bestaudio/best[ext=mp4]/best"
DOWNLOAD_DIR="/home/nietz/Videos/" # 반드시 절대 경로로 사용
YOUTUBE_DL_OUTPUT_TEMPLATE="${DOWNLOAD_DIR}%(title)s.%(ext)s"
YOUTUBE_DL_OPTION=" -f $YOUTUBE_DL_FORMAT -o $YOUTUBE_DL_OUTPUT_TEMPLATE --all-subs "
YOUTUBE_DL_CMD="youtube-dl $YOUTUBE_DL_OPTION $YOUTUBE_URL"
#echo $YOUTUBE_DL_CMD
#$YOUTUBE_DL_CMD
#echo
get_coverart () {
if [ ! "$NEW_COVERART_URL" = "$OLD_COVERART_URL" ]
then
wget "$MPRIS_ARTURL" -O /tmp/mpsyt_coverart.jpg
fi
}
put_coverart () {
echo $NEW_COVERART_URL > /home/nietz/.xmonad/.mpsyt_coverart
}
get_dunstPID () {
ID=$(cat /home/nietz/.xmonad/.dunst_mpsyt)
}
update_trackinfo () {
MPRIS_ARTURL="$(playerctl -p $MPRIS_NAME metadata mpris:artUrl)"
if [ "$MPRIS_APP" = "mopidy" ]
then
if ( echo "$MPRIS_ARTURL" | grep -qi "/images/" )
then
echo "local track" # possible download coverart
MPRIS_ARTURL_PREFIX="http://192.168.0.6:6680"
MPRIS_ARTURL=$MPRIS_ARTURL_PREFIX$MPRIS_ARTURL
echo $MPRIS_ARTURL
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?"
else
echo "Streamming track: pass MPRIS http url"
fi
fi
MPRIS_TITLE="$(playerctl -p $MPRIS_NAME metadata xesam:title)"
MPRIS_TRACK_ID="$(playerctl -p $MPRIS_NAME metadata mpris:trackid)"
if [ "$MPRIS_APP" = "mps-youtube" ]
then
YOUTUBE_TRACK_ID="$(echo $MPRIS_TRACK_ID | \
sed 's/\/CurrentPlaylist\/ytid\///' | sed "s/'//g")"
YOUTUBE_URL_PREFIX="https://www.youtube.com/watch?v="
YOUTUBE_URL=$YOUTUBE_URL_PREFIX$YOUTUBE_TRACK_ID
else
MPRIS_ALBUM="$(playerctl -p $MPRIS_NAME metadata xesam:album)"
MPRIS_ALBUM_ARITIS="$(playerctl -p $MPRIS_NAME metadata xesam:albumArtist)"
MPRIS_ARTIST="$(playerctl -p $MPRIS_NAME metadata xesam:artist)"
MPRIS_URL="$(playerctl -p $MPRIS_NAME metadata xesam:url)"
fi
MPRIS_VOLUME="$(playerctl -p $MPRIS_NAME volume)"
MPRIS_VOLUME_LEVEL=$(echo "$MPRIS_VOLUME" | awk '{print ($A)*100}')%
MPRIS_POS="$(playerctl -p $MPRIS_NAME position)"
MPRIS_LENGTH="$(playerctl -p $MPRIS_NAME metadata mpris:length)"
TOTAL_LENGTH=$(echo "$MPRIS_LENGTH" | awk '{printf "%.3f", ($MPRIS_LENGTH)/1000000}')
OLD_COVERART_URL=$(cat /home/nietz/.xmonad/.mpsyt_coverart)
NEW_COVERART_URL=$MPRIS_ARTURL
DUNST_POS_STRING="$MPRIS_POS / $TOTAL_LENGTH (sec)"
}
get_trackinfo () {
#update_trackinfo
get_coverart
get_dunstPID
if [ "$MPRIS_APP" = "mps-youtube" ]
then
echo $YOUTUBE_URL | xclip -selection primary # "primary", or "clipbard"
echo "$YOUTUBE_URL String is stored to clipboard"
echo
if [ $ID -gt 0 ]
then
dunstify -p -r $ID --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "Youtube ID : $YOUTUBE_TRACK_ID
Youtube URL : $YOUTUBE_URL" >/home/nietz/.xmonad/.dunst_mpsyt
else
dunstify -p --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL" "Youtube ID : $YOUTUBE_TRACK_ID
Youtube URL : $YOUTUBE_URL" >/home/nietz/.xmonad/.dunst_mpsyt
fi
else
echo $MPRIS_URL | xclip -selection primary
echo "$MPRIS_URL String is stored to clipboard"
if [ $ID -gt "0" ]
then
dunstify -p -r $ID --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "$MPRIS_ARTIST - $MPRIS_ALBUM" >/home/nietz/.xmonad/.dunst_mpsyt
else
dunstify -p --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "$MPRIS_ARTIST - $MPRIS_ALBUM" >/home/nietz/.xmonad/.dunst_mpsyt
fi
fi
put_coverart
}
download_track () {
get_dunstPID
if [ $ID -gt "0" ]
then
dunstify -p -r $ID --appname=$DUNST_APPNAME --urgency=low --timeout=2000 -i $ICON "$MPRIS_TITLE
" "Downloading started
Youtube URL : $YOUTUBE_URL" >/home/nietz/.xmonad/.dunst_mpsyt
else
dunstify -p --appname=$DUNST_APPNAME --urgency=low --timeout=2000 -i $ICON "$MPRIS_TITLE
" "Downloading started
Youtube URL : $YOUTUBE_URL" >/home/nietz/.xmonad/.dunst_mpsyt
fi
$YOUTUBE_DL_CMD
paplay /usr/share/sounds/freedesktop/stereo/complete.oga
if [ $ID -gt "0" ]
then
dunstify -p -r $ID --appname=$DUNST_APPNAME --urgency=low --timeout=2000 -i $ICON "$MPRIS_TITLE
" "finished downloading
Youtube URL : $YOUTUBE_URL" >/home/nietz/.xmonad/.dunst_mpsyt
else
dunstify -p --appname=$DUNST_APPNAME --urgency=low --timeout=2000 -i $ICON "$MPRIS_TITLE
" "finished downloading
Youtube URL : $YOUTUBE_URL" >/home/nietz/.xmonad/.dunst_mpsyt
fi
sleep 1
MPRIS_TITLE=$(echo $MPRIS_TITLE | sed -e 's/\//_/g')
playerctl -p $MPRIS_NAME pause
mpv "${DOWNLOAD_DIR}${MPRIS_TITLE}.mkv" &
}
get_cmdinfo() {
$1
echo $1
echo
sleep 1
update_trackinfo
get_coverart
get_dunstPID
local SUMMARY_PREFIX
# start of Summary iconFont prefix
if [ "$2" == "next" ]
then
SUMMARY_PREFIX=""
elif [ "$2" == "previous" ]
then
SUMMARY_PREFIX=""
elif [ "$2" == "play-pause" ]
then
if [ "$MPRIS_STATUS" == "Playing" ]
then
SUMMARY_PREFIX=""
MPRIS_STATUS="Paused"
else
SUMMARY_PREFIX=""
MPRIS_STATUS="Playing"
fi
elif [ "$2" == "volumeUp" ]
then
SUMMARY_PREFIX=" "
elif [ "$2" == "volumeDown" ]
then
SUMMARY_PREFIX=" "
elif [ "$2" == "volume10%" ]
then
SUMMARY_PREFIX=" 10% :"
elif [ "$2" == "volume20%" ]
then
SUMMARY_PREFIX=" 20% :"
elif [ "$2" == "volume20%" ]
then
SUMMARY_PREFIX=" 20% :"
elif [ "$2" == "volume30%" ]
then
SUMMARY_PREFIX=" 30% :"
elif [ "$2" == "volume40%" ]
then
SUMMARY_PREFIX=" 40% :"
elif [ "$2" == "volume50%" ]
then
SUMMARY_PREFIX=" 50% :"
elif [ "$2" == "volume60%" ]
then
SUMMARY_PREFIX=" 60% :"
elif [ "$2" == "volume70%" ]
then
SUMMARY_PREFIX=" 70% :"
elif [ "$2" == "volume80%" ]
then
SUMMARY_PREFIX=" 80% :"
elif [ "$2" == "volume90%" ]
then
SUMMARY_PREFIX=" 90% :"
elif [ "$2" == "volume100%" ]
then
SUMMARY_PREFIX=" 100% :"
elif [ "$2" == "mute" ]
then
SUMMARY_PREFIX=" mute :"
elif [ "$2" == "forward" ]
then
SUMMARY_PREFIX=" 1 sec :"
elif [ "$2" == "backward" ]
then
SUMMARY_PREFIX=" 1 sec :"
elif [ "$2" == "forward10" ]
then
SUMMARY_PREFIX=" 10 sec :"
elif [ "$2" == "backward10" ]
then
SUMMARY_PREFIX=" 10 sec :"
else
SUMMARY_PREFIX=""
fi
# end of Summary iconFont prefix
if [ "$MPRIS_APP" = "mps-youtube" ]
then
echo $YOUTUBE_URL | xclip -selection primary # "primary", or "clipbard"
echo "$YOUTUBE_URL String is stored to clipboard"
echo
if [ $ID -gt "0" ]
then
dunstify -p -r $ID --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "Command : $SUMMARY_PREFIX $1" >/home/nietz/.xmonad/.dunst_mpsyt
else
dunstify -p --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "Command : $SUMMARY_PREFIX $1" >/home/nietz/.xmonad/.dunst_mpsyt
fi
else
echo $MPRIS_URL | xclip -selection primary
echo "$MPRIS_URL String is stored to clipboard"
if [ $ID -gt "0" ]
then
dunstify -p -r $ID --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "$MPRIS_ARTIST - $MPRIS_ALBUM
Command : $SUMMARY_PREFIX $1" >/home/nietz/.xmonad/.dunst_mpsyt
else
dunstify -p --appname=$DUNST_APPNAME -i $ICON "$MPRIS_TITLE
Status : $MPRIS_STATUS
Volume : $MPRIS_VOLUME_LEVEL , Position : $DUNST_POS_STRING" "$MPRIS_ARTIST - $MPRIS_ALBUM
Command : $SUMMARY_PREFIX $1" >/home/nietz/.xmonad/.dunst_mpsyt
fi
fi
put_coverart
}
if [ "$1" == "trackinfo" ]
then
get_trackinfo
elif [ "$1" == "openURL" ]
then
qutebrowser --backend webengine $YOUTUBE_URL &
playerctl -p $MPRIS_NAME pause
sleep 2
get_trackinfo
elif [ "$1" == "download" ]
then
download_track
elif [ "$1" == "next" ]
then
CMD="playerctl -p $MPRIS_NAME next"
get_cmdinfo "$CMD" $1
elif [ "$1" == "previous" ]
then
CMD="playerctl -p $MPRIS_NAME previous"
get_cmdinfo "$CMD" $1
elif [ "$1" == "play-pause" ]
then
CMD="playerctl -p $MPRIS_NAME play-pause"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volumeUp" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.1+"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volumeDown" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.1-"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume10%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.1"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume20%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.2"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume30%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.3"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume40%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.4"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume50%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.5"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume60%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.6"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume70%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.7"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume80%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.8"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume90%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.9"
get_cmdinfo "$CMD" $1
elif [ "$1" == "volume100%" ]
then
CMD="playerctl -p $MPRIS_NAME volume 1.0"
get_cmdinfo "$CMD" $1
elif [ "$1" == "mute" ]
then
CMD="playerctl -p $MPRIS_NAME volume 0.0"
get_cmdinfo "$CMD" $1
elif [ "$1" == "forward" ]
then
CMD="playerctl -p $MPRIS_NAME position 1+"
get_cmdinfo "$CMD" $1
elif [ "$1" == "backward" ]
then
CMD="playerctl -p $MPRIS_NAME position 1-"
get_cmdinfo "$CMD" $1
elif [ "$1" == "forward10" ]
then
CMD="playerctl -p $MPRIS_NAME position 10+"
get_cmdinfo "$CMD" $1
elif [ "$1" == "backward10" ]
then
CMD="playerctl -p $MPRIS_NAME position 10-"
get_cmdinfo "$CMD" $1
elif [ "$1" == "backward10" ]
then
CMD="playerctl -p $MPRIS_NAME position 10-"
get_cmdinfo "$CMD" $1
else
echo "- room 503 out -"
get_trackinfo
fi
echo "------------------------------------"
echo "Player : "$MPRIS_NAME
echo "Title : "$MPRIS_TITLE
echo "Status : "$MPRIS_STATUS
echo "CoverArt URL : "$MPRIS_ARTURL
echo "Current Position : "$MPRIS_POS
echo "Total length : "$MPRIS_LENGTH
echo "Track ID : "$MPRIS_TRACK_ID
echo "Youtube ID : "$YOUTUBE_TRACK_ID
echo "Youtube URL : "$YOUTUBE_URL
echo "------------------------------------"
# temp
#echo
#A=0.1
#B=0.2
#C=`expr $A + $B`
#echo $A $B
#echo "$A $B" | awk '{print $A+$2}'
#echo "$A $B" | awk '{print ($A+$2)*100}'
#CURRENT_VOLUME_LEVEL=$(echo "$A $B" | awk '{print ($A+$2)*100}')%
#echo $CURRENT_VOLUME_LEVEL
#TOTAL_LENGTH=$(echo "$MPRIS_LENGTH" | awk '{printf "%.3f", ($MPRIS_LENGTH)/1000000}')
#echo $TOTAL_LENGTH
#echo $DUNST_POS_STRING