-
Notifications
You must be signed in to change notification settings - Fork 1
/
lrc.sh
executable file
·39 lines (29 loc) · 1.23 KB
/
lrc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
lrc_path=$(printf %s\\n '{ "command": ["get_property", "path"] }' | socat - UNIX-CONNECT:/tmp/mpv-socket) || exit 1
lrc_path=$(printf %s "$lrc_path" | jq -r .data)
[ "$lrc_path" = null ] && exit 1
lrc_path=${lrc_path%.*}.lrc
case $lrc_path in
/*) ;;
*) lrc_path=$(printf %s\\n '{ "command": ["get_property", "working-directory"] }' | socat - /tmp/mpv-socket | jq -r .data)/$lrc_path
esac
[ -e "$lrc_path" ] && exec $EDITOR "$lrc_path"
[ -e "${lrc_path%.*}.ja.lrc" ] && exec $EDITOR "${lrc_path%.*}.ja.lrc"
metadata=$(printf %s\\n '{ "command": ["get_property", "metadata"] }' \
| socat - /tmp/mpv-socket | jq .data)
# The keys are lower case in ID3 tags and upper case in Vorbis comments.
artist=$(printf %s "$metadata" | jq -r 'if has("artist") then .artist else .ARTIST end')
title=$(printf %s "$metadata" | jq -r 'if has("title") then .title else .TITLE end')
query="$artist $title"
# grep -P can be unavailable
if printf %s "$query" | perl -nC -e 'exit(not /[\p{Hiragana}\p{Katakana}\p{Han}]/)'; then
query="$query 歌詞"
else
query="$query lyrics"
fi
case ${BROWSER:=chromium} in
*chrom*) query="? $query" ;;
firefox) BROWSER="$BROWSER --search"
esac
$BROWSER "$query" 2>/dev/null &
exec $EDITOR + "$lrc_path"