local/bin/music.sh

   1 #!/bin/bash
   2 
   3 play_icon=♪
   4 pause_icon=Ⅱ
   5 
   6 # Moc {{{
   7 
   8 if [[ $(pgrep -x mocp) ]]; then
   9     case $1 in
  10         play) mocp --play; sleep 0.2 ;;
  11         stop) mocp --stop ;;
  12         pause) mocp --toggle-pause ;;
  13         prev) mocp --previous; sleep 0.1 ;;
  14         next) mocp --next; sleep 0.1 ;;
  15     esac
  16 
  17     IFS='|' read state tl file title <<< \
  18     $(mocp -Q '%state|%tl|%file|%artist - %song' 2> /dev/null)
  19 
  20     if [[ $state == PLAY || $state == PAUSE ]]; then
  21         icon=$play_icon
  22         [[ $state == PAUSE ]] &&
  23         icon=$pause_icon
  24 
  25         [[ $title == ' -' ]] &&
  26         title=$(basename "$file")
  27 
  28         echo "$icon $title (-$tl)"
  29     fi
  30 fi
  31 
  32 # }}}
  33 # MediaPlayer2 {{{
  34 
  35 query(){ # {{{
  36     qdbus org.mpris.MediaPlayer2.$player \
  37     /org/mpris/MediaPlayer2 \
  38     org.mpris.MediaPlayer2.Player.$1 2> /dev/null
  39 } # }}}
  40 get(){ # {{{
  41     grep "$1: " <<< "$meta" | cut -d ' ' -f 2-
  42 } # }}}
  43 
  44 for player in audacious rhythmbox; do
  45     [[ ! $(pgrep -x $player) ]] &&
  46     continue
  47 
  48     case $1 in
  49         play) query Play; sleep 0.2 ;;
  50         stop) query Stop ;;
  51         pause) query PlayPause ;;
  52         prev) query Previous ;;
  53         next) query Next ;;
  54     esac > /dev/null
  55 
  56     state=$(query PlaybackStatus)
  57     if [[ $state == Playing || $state == Paused ]]; then
  58         icon=$play_icon
  59         [[ $state == Paused ]] &&
  60         icon=$pause_icon
  61 
  62         meta=$(query Metadata)
  63         artist=$(get artist)
  64         title=$(get title)
  65 
  66         len=$(get length)
  67         pos=$(query Position)
  68         tl=$(($len - $pos))
  69         tl=$(date -ud @$(($tl / 1000000)) +'%H:%M:%S')
  70         tl=${tl/00:/}
  71 
  72         echo "$icon $artist - $title (-$tl)"
  73     fi
  74 done
  75 
  76 # }}}
  77