-
Notifications
You must be signed in to change notification settings - Fork 6
/
mpv-song-description
executable file
·58 lines (45 loc) · 1.59 KB
/
mpv-song-description
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
# print a description of the song, like
# title - artist (album)
#
# this uses the 'title', 'artist' and 'album' metadata keys
# as set by MPV, it handles parsing data from ID3 / metadata
#
# if the MPV_DESC_RAW environment variable is set, prints the raw JSON metadata
# if MPV_DESC_SKIP_ALBUM is set, doesn't include the album
#
# Exits unsuccessfully if nothing is playing or it couldn't find
# any of the expected keys on the parsed metadata
set -o pipefail
IFS=$'\n'
declare -a playing
if ! playing=($(mpv-currently-playing --socket)); then
echo 'No mpv instances which are currently playing media...' >&2
exit 1
fi
# reset IFS -- acts as normal
# https://unix.stackexchange.com/questions/26784/understanding-ifs
unset IFS
declare metadata
metadata="$(mpv-get-property "${playing[-1]}" 'metadata')"
[[ -n "$MPV_DESC_RAW" ]] && {
printf '%s\n' "${metadata}"
exit 0
}
declare TITLE ARTIST ALBUM DESCRIPTION
fetch_attr_or_empty() {
# jq the key out default to empty space, remove newlines and spaces from end of field
jq --arg FIELD "$1" '.[$FIELD]//""' -r <<<"${metadata}" | sed -e 's/\n/ /g; s/\s*$//g'
}
TITLE="$(fetch_attr_or_empty title)"
ARTIST="$(fetch_attr_or_empty artist)"
[[ -z "$MPV_DESC_SKIP_ALBUM" ]] && ALBUM="$(fetch_attr_or_empty album)"
DESCRIPTION=''
[[ -n "$TITLE" ]] && DESCRIPTION="${TITLE}"
[[ -n "$ARTIST" ]] && DESCRIPTION="${DESCRIPTION} - ${ARTIST}"
[[ -n "$ALBUM" && -z "${MPV_DESC_SKIP_ALBUM}" ]] && DESCRIPTION="${DESCRIPTION} (${ALBUM})"
if [[ -z "${DESCRIPTION}" ]]; then
echo 'No data extracted from metadata' 1>&2
exit 1
fi
printf '%s\n' "${DESCRIPTION}"