-
Notifications
You must be signed in to change notification settings - Fork 6
/
mpv-currently-playing
executable file
·77 lines (71 loc) · 1.98 KB
/
mpv-currently-playing
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
# mpv-get-property wrapper that gets full path of currently playing song
# If there are multiple instances of mpv playing at the same time,
# prints both
#
# Can provide the --socket flag to instead print the socket of the
# current mpv instance
#
# Pass the --all flag to print the path for all items, regardless
# of whether or not they're playing currently
declare PRINT_SOCKET=''
declare ALLOW_PAUSED=''
while [[ -n "$1" ]]; do
case "$1" in
--socket)
PRINT_SOCKET='1'
;;
--all)
ALLOW_PAUSED='1'
;;
*)
printf 'Unknown option passed: %s\n' "$1" >&2
exit 1
;;
esac
shift
done
readonly PRINT_SOCKET ALLOW_PAUSED
# change directory to root, so when we try to compute the absolute path,
# it doesn't fail since the playing file is the current directory
cd /
declare -a result=()
for socket in $(mpv-active-sockets); do
# if the user didn't specify the --all flag
if ! ((ALLOW_PAUSED)); then
# ignore items that aren't playing
IS_PAUSED="$(mpv-get-property "${socket}" 'pause')"
[[ "${IS_PAUSED}" == "true" ]] && continue
fi
if [[ -n "${PRINT_SOCKET}" ]]; then # if user asked for socket, just print the socket
result+=("${socket}")
else
# else, try to get the full song path
FULL_SONG_PATH='' # reset full song path var
if REL_SONG_PATH="$(mpv-get-property "${socket}" 'path' 2>/dev/null)"; then
# if the path doesn't correspond to its absolute path, or this doesn't exist, try prepending the working directory
[[ -n "${REL_SONG_PATH}" && -e "${REL_SONG_PATH}" ]] || {
FULL_SONG_PATH="$(mpv-get-property "${socket}" 'working-directory')/${REL_SONG_PATH}"
}
if [[ -n "${FULL_SONG_PATH}" && -e "${FULL_SONG_PATH}" ]]; then
# prints the absolute path, if it exists
result+=("${FULL_SONG_PATH}")
else
# relative song path, typically a URL?
result+=("${REL_SONG_PATH}")
fi
fi
fi
done
# switch on length of array (number of paths found)
case "${#result[@]}" in
0)
exit 1
;;
*)
(
IFS=$'\n'
echo "${result[*]}"
)
;;
esac