-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpvctl
executable file
·119 lines (108 loc) · 2.84 KB
/
mpvctl
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /bin/sh
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ] || [ -z "$1" ]; then
cat << EOF
$ mpvctl [--socket SOCKET] CMD
Control mpv through the IPC socket at $SOCKET
Commands:
play|toggle|pause: toggle playback
get: get info by property name
keypress: send a keypress
seek_to_end: seek to the end of the buffer
Example:
$ mpvctl toggle
$ mpvctl --socket /tmp/mpv_named_music play
EOF
exit
fi
hash mpv socat manage-mpv-sockets || exit 127
SOCKET=
if echo "$@" | grep '\--socket' >/dev/null; then
SOCKET="$2"
shift
shift
fi
if [ -z "$SOCKET" ]; then
if SOCKET=$(manage-mpv-sockets get_playing); then
continue
else
SOCKET=$(manage-mpv-sockets list | tail -n1)
if [ -z "$SOCKET" ]; then
echo "mpv not playing on any socket"
exit 10
fi
fi
fi
send_cmd() {
# see man at: list of input commands
res=$(echo "{\"command\": $1}" | socat - $SOCKET)
if echo $res | grep -F '"error":"success"' >/dev/null; then
echo $res | jq .data
else
if [ -n "$res" ]; then
echo $res 1>&2
fi
exit 1
fi
}
send_keypress() {
echo "sending keypress: $1"
send_cmd "[\"keypress\", \"$1\"]" >/dev/null
}
send_get() {
# see: man "Properties" for descriptions
send_cmd "[\"get_property\", \"$1\"]"
# pass 'property-list' for all properties
# https://github.com/mpv-player/mpv/issues/7562
}
seek_to_end() {
send_cmd "[\"seek\", \"99\", \"absolute-percent\"]"
}
notify() {
notify-send -i $1 -u normal -h string:x-canonical-private-synchronous:media-control -t 5000 "$2" "$(send_get media-title | cut -d\" -f2)"
}
next() {
if [ "$(send_get chapters)" -gt 0 ]; then # has chapters
send_cmd "[\"add\", \"chapter\", \"1\"]"
elif [ "$SOCKET" = "/tmp/mpv_named_music" ]; then
seek_to_end
elif [ "$(send_get playlist-count)" -gt 0 ]; then # has playlist
send_cmd "[\"playlist-next\", \"force\" ]"
else # just regular file
send_cmd "[\"seek\", \"60\"]"
fi
}
prev() {
if [ "$(send_get chapters)" -gt 0 ]; then # has chapters
send_cmd "[\"add\", \"chapter\", \"-1\"]"
elif [ "$(send_get playlist-count)" -gt 0 ]; then # has playlist
send_cmd "[\"playlist-prev\", \"weak\" ]"
else # just regular file
send_cmd "[\"seek\", \"-60\"]"
fi
}
case "$1" in
toggle)
send_keypress space >/dev/null # or send_cmd cycle pause
if [ `send_get pause 2>/dev/null` = "false" ]; then
notify player_play "Play"
else
notify player_pause "Pause"
fi;;
play)
send_cmd "[\"set_property\", \"pause\", false]"
notify player_play "Play"
;;
pause)
send_cmd "[\"set_property\", \"pause\", true]"
notify player_pause "Pause"
;;
next)
next
notify player_end "Next";;
prev)
prev
notify player_start "Previous";;
get) send_get "$2";;
key|keypress) send_keypress "$2";;
seek_to_end) seek_to_end;;
esac