淡泊以明志,寜靜以致遠

show lyric for deadbeef 显示歌词

修改自 http://forum.ubuntu.org.cn/viewtopic.php?f=21&t=339043 1楼 感谢wiewi
为简洁起见,我把主体和函数分开了。
我只是修改很少的一部分。函数我还没看完呢。:-(
比较难的可能是如何获取播放状态吧。
下面是 Deadbeef music player 的一个问题。

I haven’t looked at DeaDBeeF’s command line options, but is there one that will output its status? Then you’d just need a script with an if conditional:

!#/bin/bash
if `deadbeef –state` = “PLAY” #replace this with whatever code is necessary to find out if DeaDBeeF is playing
deadbeef –pause
else
deadbeef –play
endif

…and that’s your play/pause toggle.

我自己想办法解决了。

Chow Daniel
我真是笨啊。檢查播放狀態不一定要直接從程序給出的命令行中得出​。如果1秒前的播放位置和現在不同,那麽它就是“正在播放”
about an hour ago ·

主体部分
文件名 lyric4deadbeef.sh

#!/bin/bash
# modified from mpdlyric by wiewi,lwldcr@gmail.com
# Show lyric when playing music with deadbeef
# 2011/07/17
# contributor wiewi,lwldcr@gmail.com
# 2011/08/06
# modified by Danny Chow, xizhengbing \A\Tgmail.com
# Download function!! 
# Finished on July 20,Wednesday
# Last modifed August 6,Saturday
. functions.main
# Lyric dir
 [ -d $HOME/.lyrics ] || mkdir $HOME/.lyrics -v

LYRIC=$HOME/.lyrics

# Temp files
[ -d $HOME/tmp ] || mkdir $HOME/tmp -v
SEARCH=$HOME/tmp/search

# Notify timeout
TIMEOUT=1000
case $1 in
    -n) CMD='notify-send -t $TIMEOUT --  $TEXT'
        ;;
    *)  CMD='echo $TEXT'
        ;;
esac

Getstatus
while :
do
case $STATUS in
    playing) GetInfo
             [ -f "$FILE" ] || Download;
             ShowLyric;;
    *) echo "Deadbeef is not running!" && exit 1;;
esac
done

函数部分
文件名 functions.main

#!/bin/bash
# functions for lyric4deadbeef

######	include functions										###### 
######	Getstatus() GetInfo()  ShowLyric() Download() Wait()	###### 

# modified from mpdlyric by wiewi,lwldcr@gmail.com
# Show lyric when playing music with deadbeef
# 2011/07/17
# contributor wiewi,lwldcr@gmail.com
# 2011/08/06
# modified by Danny Chow, xizhengbing \A\T gmail.com
# Download function!! 
# Finished on July 20,Wednesday
# Last modifed August 6,Saturday
Getstatus(){
before=$(deadbeef --nowplaying  %e 2>/dev/null)
after=$(sleep 1;deadbeef --nowplaying  %e 2>/dev/null)
[ $before != $after ] && STATUS="playing"
echo $STATUS
}
GetInfo(){

# Song info
SONG=$(deadbeef --nowplaying %t 2>/dev/null)
ARTIST=$(deadbeef --nowplaying %a 2>/dev/null)
NAME=$SONG

# LRC file
FILE=$LYRIC/`echo $NAME | tr -d ' '`.lrc
}

Download(){
    # 从gougou搜索歌词文件并下载
    #gougou will lead to the site used below

    GetInfo;

    PERIOD=''
    [ -f "$SEARCH" ] && rm -f $SEARCH

    # “歌曲名 歌手” 搜索  管道写法参考lrcdis脚本
    wget -c -T 10 "http://www.lrc123.com/?keyword=$NAME $ARTIST&" -O $SEARCH > /dev/null 2>&1 |  iconv -f GBK

    URL=`cat $SEARCH | grep -B1 '下载' | grep color | sed -n '1p'| sed 's/<[^>]*>//g' | tr -d ' '|sed 's/\\r//'`

    # 第一种方式搜索不到,使用“歌曲名” 搜索
    if [ -z "$URL" ];then
        wget -c -T 10 "http://www.lrc123.com/?keyword=$NAME&" -O $SEARCH > /dev/null 2>&1 | iconv -f GBK
    fi
    URL=`cat $SEARCH | grep -B1 '下载' | grep color | sed -n '1p'| sed 's/<[^>]*>//g' | tr -d ' '|sed 's/\\r//'`

    if [ -z "$URL" ];then
    # 两种方式都搜索不到
        echo "LRC not found!" # &&  exit 1
        SONG_PREV=$(deadbeef --nowplaying %t 2>/dev/null)
        Wait;

    else
    # 有搜索结果,那么下载歌词文件
        wget -c -T 10 $URL -O "$FILE" > /dev/null 2>&1| iconv -f GBK
    fi

}

Wait(){
    # 对于下载不到歌词的曲目,每隔1秒重新读取播放器信息,一旦切换到下首,则重新尝试Download歌词
while :
do
   sleep 1
   GetInfo
   if [ "$SONG_PREV" != "$SONG" ];then
       SONG_PREV=$SONG
       [ -f "$FILE" ] || Download;
       ShowLyric;
   fi
done
}
ShowLyric(){
#       [ -f "$FILE" ] || Download;
       #eval "$CMD" "\n$NAME - $ARTIST\n"
       echo -e "\n$NAME - $ARTIST\n"
       notify-send "$NAME - $ARTIST"
       while :
       do
       if [ ! "`file $FILE | grep -i utf-8`" ];then
            iconv -f GBK -t UTF-8 $FILE -o $FILE
       fi
       TIME=$(deadbeef --nowplaying %e 2>/dev/null)
       TEXT=$(cat "$FILE" | grep $TIME | sed 's/\[[^]]*]//g')
       CURRENT=$(deadbeef --nowplaying %t 2>/dev/null)

       if [ -z "$PREV" -o "$CURRENT" = "$PREV" ];then
         PREV=$CURRENT

         # 换到下一句歌词
         if [ -n "$TEXT" -a "$TEXT" != "$TEXT_PREV" ];then
# Notify 方式有问题,歌词更新太慢,即使指定了timeout问题依旧
#       notify-send -t $TIMEOUT -- "$TEXT"
#              echo $TEXT
              sleep 0.8
              eval "$CMD"
              TEXT_PREV="$TEXT"
         fi
       # 歌曲切换
       else
              PREV=''
              GetInfo
             # eval "$CMD" "\n$NAME - $ARTIST\n"
              echo -e "\n$NAME - $ARTIST\n"
              notify-send "$NAME - $ARTIST"

              [ -f "$FILE" ] || Download;
              continue
       fi
         sleep 0.3
       done
}


时间紧,暂时到此。水平有限,可能修改得不够好。

使用办法
把前面的代码保存为 lyric4deadbeef.sh 或者你喜欢的名字
后面的代码保存为 functions.main (和 lyric4deadbeef.sh同一个目录下)
然后

chmod +x  lyric4deadbeef.sh  functions.main

当然,不想分开也是可以的。把函数部分并在lyric4deadbeef.sh中的 #!/bin/bash 后面,去掉行 . functions.main

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Connecting to %s

加关注

Get every new post delivered to your Inbox.