lrcdis不能用了?---mpd歌词脚本【11/08/07 更新,支持gnome-OSD】
发表于 : 2011-07-21 22:56
很长时间没听歌,最近心血来潮打算听听,发现以前听歌最佳伴侣--lrcdis(感谢几位大牛贡献)不好用了,貌似是网站版式更新了下载不到歌词?具体不记得了。
gnome-osd显示歌词一直很喜欢,可是它在我电脑上用不了。
gnome-osd挂了,意味着以前用过的osd-lyrics也不能用了,没有跟MPD配套的歌词显示插件,搜也没搜到好用的脚本,自己拿shell整了一个,终端显示,凑活用着。
========================================================
2011/08/07 更新
发现gnome-osd不是不能用,是不能像以前那样直接gnome-osd-client "Text"就可以显示,现在的版本会报错,但是运行manpage里的例子,它还是能工作的,参考了lrcdis,顺手把自己用的这个脚本整了整,重新贴下吧。
[bash]
[liwei@Arch ~] $ cat bin/mpdlyric
#!/bin/bash
# Show lyric when playing music with mpd.
# 2011/07/17
# wiewi,[email protected]
# Download function!! # Finished on July 20,Wednesday
# Updated on 2011/08/07
# Three ways of displaying lyrics
# 1.Gnome-OSD (Default) # 需要安装gnome-osd
# 2.Echo text in terminal
# 3.Redirect lyric to text file ($HOME/lyric.out)
# Work together with conky,add '${execi -1 tail -n 1 $HOME/tmp/lyric.out}'in conkyrc)
# 已知问题:
# 1.Note that when the text line is very long, last several words will not be displayed in conky.
# 歌词如果太长,conky将只能显示前面部分,除非你的conky很宽。
# 有个老外的python的脚本可以根据宽度断行的,我这bash的懒得弄了~~
# 2.Gnome-OSD显示英文歌词时,有时候歌词显示位置跟指定位置有一行的偏差,感觉像显示了换行符一样,暂时还没有搞定。
# Thanks to all the authors of the famous bash script 《lrcdis》 where some lines of code here come from.
##########################
# Lyric dir
LYRIC=$HOME/.lyrics
# Temp files
SEARCH=$HOME/tmp/search
OUT=$HOME/tmp/lyric.out
# Display timeout(for Gnome-OSD & notify-OSD)
Timeout=5000
##########################
# Gnome-OSD显示方式设置
# Span size
Size=20000
# Foreground (字体颜色) while/black/blue/green/yellow/...
Foreground=blue
# 动作 on/off
Animations=off
# 垂直位置 top/center/bottom
Vposition=bottom
# 水平位置 left/center/right
Halignment=left
#########################
# Player Status
STATUS=$(mpc status| sed -n '2p' | awk '{print $1}' | sed 's/[^a-zA-Z]//g')
GetInfo(){
# Song info
SONG=$(mpc --format %title% | sed -n '1p' 2>/dev/null)
ARTIST=$(mpc --format %artist% | sed -n '1p' 2>/dev/null)
NAME=$(echo $SONG | sed "s/$ARTIST//" 2>/dev/null | sed 's/\-//g'|sed 's/(.*)//g')
# LRC file
FILE=$LYRIC/`echo $NAME | tr -d ' '`.lrc
}
Download(){
# 从gougou搜索歌词文件并下载
#gougou will lead to the site used below
GetInfo;
[ -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
# 两种方式都搜索不到
Display "歌词搜索失败!" # && exit 1
SONG_PREV=$(mpc --format %title% | sed -n '1p')
Wait;
else
# 有搜索结果,那么下载歌词文件
wget -c -T 10 $URL -O "$FILE" > /dev/null 2>&1| iconv -f GBK
fi
}
Wait(){
# 对于下载不到歌词的曲目,每隔1秒重新读取播放器信息,一旦切换到下首,则重新尝试Download歌词
# 有两种情况: 1 可以得到歌曲信息,但是搜索不到符合条件的lrc
# 2 歌曲信息为空,无法搜索
while :
do
sleep 1
# 获取当前播放信息
GetInfo
# 有歌曲切换,检查文件是否存在,或者尝试下载,然后显示歌词
if [ -n "$SONG" -a "$SONG_PREV" != "$SONG" ];then
SONG_PREV=$SONG
[ -f "$FILE" ] || Download;
ShowLyric $Option;
fi
done
}
# 将showlyric得到的$TEXT用指定的方式显示
Display(){
case $Option in
# notification方式,显示不太准确
#-n) notify-send -t $Timeout $1
# ;;
# 输出到文本文件
-o) echo "$1" >> $OUT
;;
# Gnome-osd 显示
-g) gnome-osd-client -f "<message id='mpdlyric' osd_fake_translucent_bg='off' osd_vposition='$Vposition' animations='$Animations' hide_timeout='$Timeout' osd_halignment='$Halignment'><span size='$Size' foreground='$Foreground'>`echo -n $1`</span></message>"
;;
# 最简单的方式,终端文本输出
-t) echo $1
;;
*) gnome-osd-client -f "<message id='mpdlyric' osd_fake_translucent_bg='off' osd_vposition='$Vposition' animations='$Animations' hide_timeout='$Timeout' osd_halignment='$Halignment'><span size='$Size' foreground='$Foreground'>`echo -n $1`</span></message>"
;;
esac
}
ShowLyric(){
# 进入显示歌词的循环前,显示当前播放曲名
Display "===== $NAME - $ARTIST =====";
# 显示歌词的循环
while :
do
# 若是歌曲在sleep 0.3之间切换,那么歌曲名称等信息没有更新,下面取得TEXT的步骤可能出错,所以需要重新生成歌曲信息
GetInfo
[ -z "$SONG" ] && Display "歌曲信息不全!" && Wait;
if [ ! "`file $FILE | grep -i utf-8`" ];then
iconv -f GBK -t UTF-8 $FILE -o $FILE
fi
TIME=`mpc status | grep '/' | awk -F '/' '{print $2}' | awk '{print $2}'`
TEXT=`[ -f "$FILE" ] && cat "$FILE" | grep $TIME | sed 's/\[[^]]*]//g'|sed 's/\\r//'`
CURRENT=`mpc current`
if [ -z "$PREV" -o "$CURRENT" = "$PREV" ];then
PREV=`mpc current`
# 换到下一句歌词
if [ -n "$TEXT" -a "$TEXT" != "$TEXT_PREV" ];then
sleep 0.8
Display "$TEXT";
TEXT_PREV="$TEXT"
fi
# 歌曲切换
else
PREV=''
GetInfo
Display "===== $NAME - $ARTIST =====";
[ -f "$FILE" ] || Download;
continue
fi
# 等待0.3秒后进入下个循环
sleep 0.3
done
}
# 记录选项
Option="$1"
# 如果选择输出到文件,那么先删除以前的文件,避免文件过大
[ "$Option" = '-o' -a -f "$OUT" ] && rm -f "$OUT"
while :
do
case $STATUS in
playing) GetInfo
# 没有歌词就下载
[ -f "$FILE" ] || Download;
ShowLyric $Option;;
*) Display "MPD not running!" && exit 1;;
esac
done
[/bash]
大概效果就是这样了,比较土,本人菜鸟,欢迎拍砖
gnome-osd显示歌词一直很喜欢,可是它在我电脑上用不了。
gnome-osd挂了,意味着以前用过的osd-lyrics也不能用了,没有跟MPD配套的歌词显示插件,搜也没搜到好用的脚本,自己拿shell整了一个,终端显示,凑活用着。
========================================================
2011/08/07 更新
发现gnome-osd不是不能用,是不能像以前那样直接gnome-osd-client "Text"就可以显示,现在的版本会报错,但是运行manpage里的例子,它还是能工作的,参考了lrcdis,顺手把自己用的这个脚本整了整,重新贴下吧。
[bash]
[liwei@Arch ~] $ cat bin/mpdlyric
#!/bin/bash
# Show lyric when playing music with mpd.
# 2011/07/17
# wiewi,[email protected]
# Download function!! # Finished on July 20,Wednesday
# Updated on 2011/08/07
# Three ways of displaying lyrics
# 1.Gnome-OSD (Default) # 需要安装gnome-osd
# 2.Echo text in terminal
# 3.Redirect lyric to text file ($HOME/lyric.out)
# Work together with conky,add '${execi -1 tail -n 1 $HOME/tmp/lyric.out}'in conkyrc)
# 已知问题:
# 1.Note that when the text line is very long, last several words will not be displayed in conky.
# 歌词如果太长,conky将只能显示前面部分,除非你的conky很宽。
# 有个老外的python的脚本可以根据宽度断行的,我这bash的懒得弄了~~
# 2.Gnome-OSD显示英文歌词时,有时候歌词显示位置跟指定位置有一行的偏差,感觉像显示了换行符一样,暂时还没有搞定。
# Thanks to all the authors of the famous bash script 《lrcdis》 where some lines of code here come from.
##########################
# Lyric dir
LYRIC=$HOME/.lyrics
# Temp files
SEARCH=$HOME/tmp/search
OUT=$HOME/tmp/lyric.out
# Display timeout(for Gnome-OSD & notify-OSD)
Timeout=5000
##########################
# Gnome-OSD显示方式设置
# Span size
Size=20000
# Foreground (字体颜色) while/black/blue/green/yellow/...
Foreground=blue
# 动作 on/off
Animations=off
# 垂直位置 top/center/bottom
Vposition=bottom
# 水平位置 left/center/right
Halignment=left
#########################
# Player Status
STATUS=$(mpc status| sed -n '2p' | awk '{print $1}' | sed 's/[^a-zA-Z]//g')
GetInfo(){
# Song info
SONG=$(mpc --format %title% | sed -n '1p' 2>/dev/null)
ARTIST=$(mpc --format %artist% | sed -n '1p' 2>/dev/null)
NAME=$(echo $SONG | sed "s/$ARTIST//" 2>/dev/null | sed 's/\-//g'|sed 's/(.*)//g')
# LRC file
FILE=$LYRIC/`echo $NAME | tr -d ' '`.lrc
}
Download(){
# 从gougou搜索歌词文件并下载
#gougou will lead to the site used below
GetInfo;
[ -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
# 两种方式都搜索不到
Display "歌词搜索失败!" # && exit 1
SONG_PREV=$(mpc --format %title% | sed -n '1p')
Wait;
else
# 有搜索结果,那么下载歌词文件
wget -c -T 10 $URL -O "$FILE" > /dev/null 2>&1| iconv -f GBK
fi
}
Wait(){
# 对于下载不到歌词的曲目,每隔1秒重新读取播放器信息,一旦切换到下首,则重新尝试Download歌词
# 有两种情况: 1 可以得到歌曲信息,但是搜索不到符合条件的lrc
# 2 歌曲信息为空,无法搜索
while :
do
sleep 1
# 获取当前播放信息
GetInfo
# 有歌曲切换,检查文件是否存在,或者尝试下载,然后显示歌词
if [ -n "$SONG" -a "$SONG_PREV" != "$SONG" ];then
SONG_PREV=$SONG
[ -f "$FILE" ] || Download;
ShowLyric $Option;
fi
done
}
# 将showlyric得到的$TEXT用指定的方式显示
Display(){
case $Option in
# notification方式,显示不太准确
#-n) notify-send -t $Timeout $1
# ;;
# 输出到文本文件
-o) echo "$1" >> $OUT
;;
# Gnome-osd 显示
-g) gnome-osd-client -f "<message id='mpdlyric' osd_fake_translucent_bg='off' osd_vposition='$Vposition' animations='$Animations' hide_timeout='$Timeout' osd_halignment='$Halignment'><span size='$Size' foreground='$Foreground'>`echo -n $1`</span></message>"
;;
# 最简单的方式,终端文本输出
-t) echo $1
;;
*) gnome-osd-client -f "<message id='mpdlyric' osd_fake_translucent_bg='off' osd_vposition='$Vposition' animations='$Animations' hide_timeout='$Timeout' osd_halignment='$Halignment'><span size='$Size' foreground='$Foreground'>`echo -n $1`</span></message>"
;;
esac
}
ShowLyric(){
# 进入显示歌词的循环前,显示当前播放曲名
Display "===== $NAME - $ARTIST =====";
# 显示歌词的循环
while :
do
# 若是歌曲在sleep 0.3之间切换,那么歌曲名称等信息没有更新,下面取得TEXT的步骤可能出错,所以需要重新生成歌曲信息
GetInfo
[ -z "$SONG" ] && Display "歌曲信息不全!" && Wait;
if [ ! "`file $FILE | grep -i utf-8`" ];then
iconv -f GBK -t UTF-8 $FILE -o $FILE
fi
TIME=`mpc status | grep '/' | awk -F '/' '{print $2}' | awk '{print $2}'`
TEXT=`[ -f "$FILE" ] && cat "$FILE" | grep $TIME | sed 's/\[[^]]*]//g'|sed 's/\\r//'`
CURRENT=`mpc current`
if [ -z "$PREV" -o "$CURRENT" = "$PREV" ];then
PREV=`mpc current`
# 换到下一句歌词
if [ -n "$TEXT" -a "$TEXT" != "$TEXT_PREV" ];then
sleep 0.8
Display "$TEXT";
TEXT_PREV="$TEXT"
fi
# 歌曲切换
else
PREV=''
GetInfo
Display "===== $NAME - $ARTIST =====";
[ -f "$FILE" ] || Download;
continue
fi
# 等待0.3秒后进入下个循环
sleep 0.3
done
}
# 记录选项
Option="$1"
# 如果选择输出到文件,那么先删除以前的文件,避免文件过大
[ "$Option" = '-o' -a -f "$OUT" ] && rm -f "$OUT"
while :
do
case $STATUS in
playing) GetInfo
# 没有歌词就下载
[ -f "$FILE" ] || Download;
ShowLyric $Option;;
*) Display "MPD not running!" && exit 1;;
esac
done
[/bash]
大概效果就是这样了,比较土,本人菜鸟,欢迎拍砖