Home > IT > Lyrics Show Based on Bash Script

Lyrics Show Based on Bash Script

分享家:Addthis中国

在Ubuntu听歌一直用的是Audacious听歌。
但是歌词问题总让我很不爽。
Lyriczilla还不错,但是效果一般。
昨天无意间在一个论坛看到一个叫mlrc的歌词显示脚本,非常之棒~
目前支持的播放器有audacious,amarok,exaile,mocp,mpd,rhythmbox,Quod Libet(只能由id3标签判断)
而且支持屏幕桌面的前端歌词显示

#!/bin/bash
 
#下载的歌词默认保存在~/.lyrics 
LRCDIR=~/.lyrics 
 
[ -d $LRCDIR ] || mkdir -p ~/.lyrics
 
USAGE(){
  cat <<EOF
 
  Usage: lrc [options] [arguments]
  Options
  -d, --download - download lyrics
  osd            - auto check player and show lyrics on OSD mod
  cli            - auto check player and show lyrics on CLI mod
  -h, --help     - display this
 
EOF
}
 
noid=0
nolrc=0
 
#获取歌曲名字
GETNAME(){
  case "$player" in
  'download')
    echo "$title-$artist";;
  'mpd')
    if [ "$readname" = "filename" ] ; then
      sn="`mpc playlist | grep ">" | sed -e 's/).*//' -e 's/>//'`"
      : $((fn=$sn-1))
      if [ -e ~/.mpdconf ] ; then
        state="`cat ~/.mpdconf | grep "^state_file" | awk -F'"' '{print $2}'`"
        cat "$state" | grep "^$fn" | sed -e 's/.*://' -e 's/.mp3//' -e 's/[0-9]\+[/./-]//g'
      else
        state="`cat /etc/mpd.conf | grep "^state_file" | awk -F'"' '{print $2}'`"
        cat "$state" | grep "^$fn" | sed -e 's/.*://' -e 's/.mp3//' -e 's/[0-9]\+[/./-]//g'
      fi
    else  
      if [ -n "`mpc playlist | grep ">" | sed -e "s/.*)\ //" | grep ".mp3"`" ] ; then
        echo "`mpc playlist | grep ">" | sed -e "s/.*)\ //" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"`"
        noid=1
      else
        echo "`mpc playlist | grep ">" | sed -e "s/.*)\ //" -e "s/[0-9]\+[/./-]//g"`"
        noid=0
      fi
    fi;;
  'mocp')
    if [ "$readname" = "filename" ] ; then
      echo "`mocp -i | grep -i "^File" | sed -e "s/.*\///" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"`"
    else
      if [ "`mocp -i | grep "^SongTitle:" | sed -e "s/.*:\ //"`" = "" ] ; then
        echo "`mocp -i | grep -i "^File" | sed -e "s/.*\///" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"`"
        noid=1
      else
        echo "`mocp -i | grep "^Artist:" | sed -e "s/.*:\ //" -e "s/[0-9]\+[/./-]//g"` - `mocp -i | grep "^SongTitle:" | sed -e "s/.*:\ //" -e "s/[0-9]\+[/./-]//g"`"
        noid=0
      fi
    fi;;
  'aud')
    #p="`qdbus org.mpris.audacious /org/atheme/audacious org.atheme.audacious.Position`"
    #t="`qdbus org.mpris.audacious /org/atheme/audacious org.atheme.audacious.SongTitle $p | sed -e "s/[0-9]\+[/./-]//g"`"
    #n="`qdbus org.mpris.audacious /org/atheme/audacious org.atheme.audacious.SongFilename $p`"
    #if [ "$readname" = "filename" ] ; then
    #  echo "$n" | sed -e "s/.*\///" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g" | perl -p -e 's/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg'
    #else
    #  if [ -n "`echo "$t" | grep ".mp3"`" ] ; then
    #    echo "$t" | sed -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"
    #    noid=1
    #  else
    #    echo "$t" | awk -F'-' '{if (NF > 2) {printf $1"-"$3} else {print}}'
    #    noid=0
    #  fi
    #fi;;
    if [ "$readname" = "filename" ] ; then
      echo "`audtool current-song-filename`" | sed -e "s/.*\///" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g" | perl -p -e 's/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg'
    else
      if [ -n "`audtool current-song | grep ".mp3"`" ] ; then
        echo "`audtool current-song | grep ".mp3"`" | sed -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"
        noid=1
      else
        echo "`audtool current-song`" | awk -F'-' '{if (NF > 2) {printf $1"-"$3} else {print}}'
        noid=0
      fi
    fi;;
  'amok')
    if [ "$readname" = "filename" ] ; then
      echo "`dcop amarok player path | sed -e "s/.*\///" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"`"
    else
      if [ -n "`dcop amarok player nowPlaying | grep ".mp3"`" ] ; then
        echo "`dcop amarok player nowPlaying | sed -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g"`"
        noid=1
      else
        echo "`dcop amarok player nowPlaying | sed -e "s/[0-9]\+[/./-]//g"`"
        noid=0
      fi
    fi;;
  'exaile')
    t=`dbus-send --session --print-reply --dest=org.exaile.DBusInterface /DBusInterfaceObject org.exaile.DBusInterface.get_title | sed -n '$p' | awk -F[\"\"] '{print $2}' | sed -e 's/[0-9]\+[/./-]//g'`
    if [ -n "`echo "$t" | grep ".mp3"`" ] ; then
      echo "$t" | sed -e "s/.mp3//"
    else
      echo `dbus-send --session --print-reply --dest=org.exaile.DBusInterface /DBusInterfaceObject org.exaile.DBusInterface.get_artist | sed -n '$p' | awk -F[\"\"] '{print $2}' | sed -e 's/[0-9]\+[/./-]//g'` - "$t"
    fi;;
  'quodli')
    t=`dbus-send --session --print-reply --dest=net.sacredchao.QuodLibet /net/sacredchao/QuodLibet net.sacredchao.QuodLibet.CurrentSong | sed -n '/title/{n;p}' | sed q | awk -F[\"\"] '{print $2}' | sed -e 's/[0-9]\+[/./-]//g'`
    a=`dbus-send --session --print-reply --dest=net.sacredchao.QuodLibet /net/sacredchao/QuodLibet net.sacredchao.QuodLibet.CurrentSong | sed -n '/artist/{n;p}' | sed '1p' | awk -F[\"\"] '{print $2}' | sed -e 's/[0-9]\+[/./-]//g'`
    echo "$a" - "$t";;
  'rhythmbox')
    url=`dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.getPlayingUri | sed -n '$p' | awk -F[\"\"] '{print $2}'`
    if [ "$readname" = "filename" ] ; then
      echo "$url" | sed -e "s/.*\///" -e "s/.mp3//" -e "s/[0-9]\+[/./-]//g" | perl -p -e 's/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg'
    else
      t=`dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Shell org.gnome.Rhythmbox.Shell.getSongProperties string:"$url" | sed -n '/title/{n;p}' | sed -n '2p' | awk -F[\"\"] '{print $2}' | sed -e 's/[0-9]\+[/./-]//g'`
      a=`dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Shell org.gnome.Rhythmbox.Shell.getSongProperties string:"$url" | sed -n '/artist/{n;p}' | sed -n '$p' | awk -F[\"\"] '{print $2}' | sed -e 's/[0-9]\+[/./-]//g'`
      [ -z "$t" ] && break
      if [ -n "`echo "$t" | grep ".mp3"`" ] ; then
        echo $t | sed -e "s/.mp3//"
      else
        echo "$a" - "$t"
      fi
    fi;;
  esac
}
 
#下载歌词
DOWNLRC(){
  NM="$(GETNAME)"
  nolrc=0
  [ -e /tmp/lrc_file ] && rm /tmp/lrc_file
  [ -e /tmp/lrctmp ] && rm /tmp/lrctmp
  [ `locale |grep "LANG=.*UTF-8"` ] && lang=1
  if [ "$blurred" = "1" ] ; then
    if [ $lang ];then
      gb="`echo "$NM" | iconv -c -f utf-8 -t gb2312 | od -t x1 -A n |tr "\n" " " |tr " " % | sed 's/%*$//g'`"
    else 
      gb="`echo "$NM" | od -t x1 -A n | tr " " %`"
    fi
    wget "http://mp3.sogou.com/gecisearch.so?query="$gb"" -O /tmp/lrc_file -q
    links="`cat /tmp/lrc_file | grep -i "downlrc" | sed -e '2,$d' | awk -F'"' '{print $2}'`"
    [ "$links" != "" ] && wget http://mp3.sogou.com/"$links" -O /tmp/lrctmp -q
    [ "$links" = "" ] && nolrc=1 && DISPLAY
  else
    if [ $lang ] ; then
      gb="`echo "$NM" | iconv -c -f utf-8 -t gb2312`"
    else 
      gb="$NM"
    fi
    if [ "$readname" = "filename" ] || [ $noid ] ; then
      if [ "$namemode1" = "artist" ] ; then
        ar=`echo "$gb" | awk -F'-' '{printf $1}'`
      else
        ti=`echo "$gb" | awk -F'-' '{printf $1}'`
      fi
      if [ "$namemode2" = "title" ] ; then
        ti=`echo "$gb" | awk -F'-' '{printf $2}'`
      else
        ar=`echo "$gb" | awk -F'-' '{printf $2}'`
      fi
    else
      ar=`echo "$gb" | awk -F'-' '{printf $1}'`
      ti=`echo "$gb" | awk -F'-' '{printf $2}'`
    fi
    wget "http://lrc.aspxp.net/?ar=$ar&al=&ti=$ti" -O /tmp/lrc_file -q
    links="`cat /tmp/lrc_file | grep -i "&t=lrc&ac=dl" | sed -n 1p | awk -F'"' '{print $2}'`"
    [ "$links" != "" ] && wget http://lrc.aspxp.net/"$links" -O /tmp/lrctmp -q
    [ "$links" = "" ] && nolrc=1 && DISPLAY
  fi
  if [ $lang ];then
    [ -e /tmp/lrctmp ] && iconv -f gbk -t utf-8 -c /tmp/lrctmp -o $LRCDIR/"$NM.lrc"
  else
    cp /tmp/lrctmp $LRCDIR/"$NM.lrc"
  fi
  [ -e $LRCDIR/"$NM.lrc" ] && sed -i 's/\r//' $LRCDIR/"$NM.lrc"
}
 
#整理歌词
READLRC(){
  NM="$(GETNAME)"
  #[ -e $LRCDIR/"$NM.lrc" ] && awk '! /(\] *.?)$|^( *)$|^[^\[]/' $LRCDIR/"$NM.lrc" | awk -F"]" '{if (NF > 2) {for (i = 1; i < NF; i++) {print $i"]"$NF;}} else{print $0;}}' | sort -o $LRCDIR/"$NM.lrc"
}
 
#显示歌词
DISPLAY(){ 
  NM="$(GETNAME)"
  clear
  echo  -e "\033[;32m****** $NM ****** \033[0m "
  [ "$mode" = "osd" ] && gnome-osd-client "<span foreground='$foreground'>`echo -e "$NM"`</span>"
  if [ -e $LRCDIR/"$NM.lrc" ]; then
    echo ""
  else
    echo "lyrics not found..."
    [ "$mode" = "osd" ] && sleep 3 && gnome-osd-client "<span foreground='$foreground'>`echo -e "lyrics not found..."`</span>"
  fi
  if [ -e $LRCDIR/"$NM.lrc" ]; then
    LRC="$(< $LRCDIR/"$NM.lrc")"
  fi
  [ "$nolrc" = "1" ] && LRC=""
  while [ true ] ; do
    if [ "$NM" != "$(GETNAME)" ] ; then
      NM="$(GETNAME)"
      while [ ! -e $LRCDIR/"$NM.lrc" ] ; do
        DOWNLRC 
      done
      LRC="$(< $LRCDIR/"$NM.lrc")" 
      clear
      echo  -e "\033[;32m****** $NM ****** \033[0m " 
      echo ""
      [ "$mode" = "osd" ] && gnome-osd-client "<span foreground='$foreground'>`echo -e "$NM"`</span>"
      N=1
    fi
    if [ "$player" = "mocp" ] ; then
      TM="`mocp -i | grep "CurrentTime" | sed -e 's/CurrentTime:\ //'`"
    elif [ "$player" = "aud" ] ; then
      #t="`qdbus org.mpris.audacious /org/atheme/audacious org.atheme.audacious.Time`"
      #t="`dbus-send --session --print-reply --dest=org.atheme.audacious /org/atheme/audacious org.atheme.audacious.Time | grep "uint32" | awk -F' ' '{print $2}'`"
      t="`audtool current-song-output-length-frames`"
      #t="`python -c "import dbus;print dbus.SessionBus().get_object('org.atheme.audacious', '/Player').PositionGet()"`"
      : $((t=$t/1000))
      : $((m=$t/60))
      : $((s=$t%60))
      s=`printf '%.2d' $s`
      TM="$m:$s"
    elif [ "$player" = "amok" ] ; then
      t="`dcop amarok player trackCurrentTime`"
      : $((m=$t/60))
      : $((s=$t%60))
      s=`printf '%.2d' $s`
      TM="$m:$s"
    elif [ "$player" = "mpd" ] ; then
      TM="`mpc | grep "/" | awk -F' ' '{print $3}' | awk -F'/' '{print $1}'`"
    elif [ "$player" = "rhythmbox" ] ; then
      #t=`dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.getElapsed | sed -n '$p' | awk '{print $2}'`
      t=`qdbus org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.getElapsed`
      if [ "$t" != "" ] ; then
      [ -z "`echo $t | sed 's/[0-9]//g'`" ] && let m=$t/60
      [ -z "`echo $t | sed 's/[0-9]//g'`" ] && let s=$t%60
      fi
      s=`printf '%.2d' $s`
      TM="$m:$s"
    elif [ "$player" = "exaile" ] ; then
      TM="`dbus-send --session --print-reply --dest=org.exaile.DBusInterface /DBusInterfaceObject org.exaile.DBusInterface.query | sed -n '$p' | awk -F['\[\]'] '{print $2}'`"
    elif [ "$player" = "quodli" ] ; then
      t="`dbus-send --session --print-reply --dest=net.sacredchao.QuodLibet /net/sacredchao/QuodLibet net.sacredchao.QuodLibet.GetPosition | sed -n '$p' | awk '{print $2}'`"
      : $((t=$t/1000))
      : $((m=$t/60))
      : $((s=$t%60))
      s=`printf '%.2d' $s`
      TM="$m:$s"
    fi
    [ "$NM" = "No song playing."  ] && exit
    [ "$TM" = ""  ] && break
    NOW="`echo "$LRC" | sed -n "/$TM/p" | sed 's/\[.*\]//' | tr '<' '[' | tr '>' ']'`"
    if [ "$NOW" != ""  -a  "$NOW" != "$LAST" ] ; then 
      N=`expr $N + 1`
      if [ $N -ge "$row" ] ; then
        N=1
        clear
        echo  -e "\033[;32m****** $NM ****** \033[0m"
        echo "" 
        echo $LAST 
      fi
      LAST=$NOW 
      echo $NOW 
      [ "$mode" = "osd" ] && gnome-osd-client -f "<message id='mymusic' osd_fake_translucent_bg='off' osd_vposition='$vposition' osd_halignment='$halignment' animations='$animations' hide_timeout='$timeout'><span size='$size' foreground='$foreground'>`echo "$NOW"`</span></message>"
    fi 
    sleep 1
  done 
}
 
#默认流程
DEFAULT(){
  clear
  NM="$(GETNAME)"
  if [ ! -e $LRCDIR/"$NM"".lrc" ] ; then 
    DOWNLRC
    READLRC
    DISPLAY
  else
    READLRC
    DISPLAY
  fi
}
 
#main
case "$1" in
  '-d'|'--download')
    player='download'
    title=$2
    artist=$3
    blurred=1
    DOWNLRC;;
  ''|'osd'|'cli')
    [ -d ~/.config ] || mkdir -p ~/.config
    while [ ! -e ~/.config/mlrc.conf ] ; do
      echo -e "[config]\nmode=osd\nplayer=mpd\nblurred=0\nreadname=id3\nnamemode=artist-title\n[cli]\nrow=10\n[osd]\nvposition=bottom\nhalignment=left\ntimeout=10000\nsize=18000\nforeground=white\nanimations=on" > ~/.config/mlrc.conf
    done
    #判断模式
    if [ "$1" = "osd" ] ; then
      mode="osd"
      sed -i 's/^mode=.*/mode=osd/' ~/.config/mlrc.conf
    elif [ "$1" = "cli" ] ; then
      mode="cli"
      sed -i 's/^mode=.*/mode=cli/' ~/.config/mlrc.conf
    else
      mode="`cat ~/.config/mlrc.conf | sed -n /^mode/p | awk -F'=' '{print $2}'`"
    fi
    [ ! `which gnome-osd-client` ] && mode="cli"
    #判断播放器
    if [ -n "`ps -d | grep audacious`" ] ; then
      player="aud"
      sed -i 's/^player=.*/player=aud/' ~/.config/mlrc.conf
    elif [ -n "`ps -d | grep amarokapp`" ] ; then
      player="amok"
      sed -i 's/^player=.*/player=amok/' ~/.config/mlrc.conf
    elif [ -n "`ps -d | grep rhythmbox`" ] ; then
      player="rhythmbox"
      sed -i 's/^player=.*/player=rhythmbox/' ~/.config/mlrc.conf
    elif [ -n "`ps -d | grep exaile`" ] ;then
      player="exaile"
      sed -i 's/^player=.*/player=exaile/' ~/.config/mlrc.conf
    elif [ -n "`ps -d | grep quodli`" ] ;then
      player="quodli"
      sed -i 's/^player=.*/player=quodli/' ~/.config/mlrc.conf
    elif [ -n "`ps -A | grep mocp`" ] ; then
      player="mocp"
      sed -i 's/^player=.*/player=mocp/' ~/.config/mlrc.conf
    elif [ -n "`ps -d | grep mpd`" ] ;then
      player="mpd"
      sed -i 's/^player=.*/player=mpd/' ~/.config/mlrc.conf
    else
      player="`cat ~/.config/mlrc.conf | sed -n /^player/p | awk -F'=' '{print $2}'`"
    fi
    #读取配置文件数据
    blurred="`cat ~/.config/mlrc.conf | sed -n /^blurred/p | awk -F'=' '{print $2}'`"
    readname="`cat ~/.config/mlrc.conf | sed -n /^readname/p | awk -F'=' '{print $2}'`"
    namemode1="`cat ~/.config/mlrc.conf | sed -n /^namemode/p | awk -F'=' '{print $2}' | awk -F'-' '{print $1}'`"
    namemode2="`cat ~/.config/mlrc.conf | sed -n /^namemode/p | awk -F'=' '{print $2}' | awk -F'-' '{print $2}'`"
    row="`cat ~/.config/mlrc.conf | sed -n /^row/p | awk -F'=' '{print $2}'`"
    vposition="`cat ~/.config/mlrc.conf | sed -n /^vposition/p | awk -F'=' '{print $2}'`"
    halignment="`cat ~/.config/mlrc.conf | sed -n /^halignment/p | awk -F'=' '{print $2}'`"
    timeout="`cat ~/.config/mlrc.conf | sed -n /^timeout/p | awk -F'=' '{print $2}'`"
    size="`cat ~/.config/mlrc.conf | sed -n /^size/p | awk -F'=' '{print $2}'`"
    foreground="`cat ~/.config/mlrc.conf | sed -n /^foreground/p | awk -F'=' '{print $2}'`"
    animations="`cat ~/.config/mlrc.conf | sed -n /^animations/p | awk -F'=' '{print $2}'`"
    DEFAULT;;
  '-h'|'--help')
    USAGE;;
  *)
    echo "Option '$1' is not recognized, see $0 -h or --help.";;
esac

原帖
原帖的附近有点小问题,基本不影响使用,尽量使用上面的代码,可以直接用上面的代码覆盖到原帖的脚本代码

You don't feel like leaving comments? Please leave your palm-print.
懒得留言?那留下您的掌印吧。
Categories: IT Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

:D 8) :down: 8O :!: :halo: :evil: :heart: :kiss: :lol: :money: :mrgreen: :x :| :pirate: :?: :oops: :roll: :P :( :) :o :up: :vangry: :twisted: ;) :? :cry:

也许您会觉得ReCAPTCHA验证码很繁琐,会认为他和其他所有的验证码一样让你厌烦。那请点击这里,您会了解到ReCAPTCHA为人类文化的传承做出了多大的贡献。请您也为人类知识的保存和传递也出一份力吧,谢谢!
 
    Posts Nav.