Files
fuzic/src/sh/mpv.sh
2025-09-05 16:18:02 +02:00

100 lines
2.0 KiB
Bash

__mpv_command() {
printf "{ \"command\": [\"%s\"] }\n" "$1" | $SOCAT - "$MPV_SOCKET"
}
__mpv_command_with_arg() {
printf "{ \"command\": [\"%s\", \"%s\"] }\n" "$1" "$2" | $SOCAT - "$MPV_SOCKET"
}
__mpv_command_with_args2() {
printf "{ \"command\": [\"%s\", \"%s\", \"%s\"] }\n" "$1" "$2" "$3" | $SOCAT - "$MPV_SOCKET"
}
__mpv_get() {
__mpv_command_with_arg "expand-text" "$1" | $JQ -r '.data'
}
mpv_playlist_count() {
__mpv_get '${playlist-count}'
}
mpv_playlist_position() {
__mpv_get '${playlist-pos}'
}
mpv_playlist_move() {
__mpv_command_with_args2 "playlist-move" "$1" "$2" >>/tmp/foo
}
mpv_playlist_clear() {
__mpv_command "playlist-clear"
}
mpv_quit() {
__mpv_command "quit"
}
mpv_start() {
MPV_SOCKET="$(mktemp --suffix=.sock)"
trap 'mpv_quit >/dev/null; rm -f "$MPV_SOCKET"' EXIT INT
$MPV --no-config --no-terminal --input-ipc-server="$MPV_SOCKET" --idle --no-osc --no-input-default-bindings &
}
mpv_play_index() {
__mpv_command_with_arg "playlist-play-index" "$1"
}
mpv_rm_index() {
__mpv_command_with_arg "playlist-remove" "$1"
}
mpv_play_list() {
t=$(mktemp)
cat >"$t"
__mpv_command_with_arg "loadlist" "$t"
rm -f "$t"
}
mpv_queue_list() {
t=$(mktemp)
cat >"$t"
__mpv_command_with_args2 "loadlist" "$t" "append-play"
rm -f "$t"
}
mpv_queue_next_list() {
t=$(mktemp)
cat >"$t"
pos=$(mpv_playlist_position)
cnt1=$(mpv_playlist_count)
__mpv_command_with_args2 "loadlist" "$t" "append-play"
rm -f "$t"
cnt2=$(mpv_playlist_count)
diff=$((cnt2 - cnt1))
[ "$diff" -gt 0 ] || return
# Move added items right after current item (numbers are 0 based)
for i in $(seq "$diff"); do
mpv_playlist_move $((cnt1 + i - 1)) $((pos + i))
done
}
mpv_next() {
__mpv_command "playlist-next"
}
mpv_prev() {
__mpv_command "playlist-prev"
}
mpv_seek_forward() {
__mpv_command_with_arg "seek" "10"
}
mpv_seek_backward() {
__mpv_command_with_arg "seek" "-10"
}
mpv_toggle_pause() {
__mpv_command_with_arg "cycle" "pause"
}