Last active
September 12, 2020 09:21
-
-
Save elundmark/a7e491a1a3b26834972adeeb4962a955 to your computer and use it in GitHub Desktop.
Remote control for MPV (mpv_remote.sh [play|pause|...] [all])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# License: CC0 <https://creativecommons.org/choose/zero/> | |
# Author: elundmark@posteo.se | |
# Link: https://gist.github.com/elundmark/a7e491a1a3b26834972adeeb4962a955 | |
# Using dbus and the mpv-mpris script: | |
# Targets either the frontmost player, | |
# or every instance of mpv (all), | |
# or one or more specific mpv processes by PID, | |
# separated by commas. | |
# Example: mpv_remote.sh play all | |
######################################################### | |
## This needs the mpv plugin mpv-mpris to be installed ## | |
## https://github.com/hoyon/mpv-mpris ## | |
helpText() { | |
SCRIPT_NAME="$(basename "$0")" | |
echo "Usage: \$ $SCRIPT_NAME COMMAND [all | PID[,PID]]" | |
echo "Example: $SCRIPT_NAME play all" | |
echo " $SCRIPT_NAME next" | |
echo " $SCRIPT_NAME pause 845" | |
echo " $SCRIPT_NAME pause 845,846" | |
return 0 | |
} | |
# exit conditions | |
if [ "$#" -eq 0 ] || [ "$#" -gt 2 ] \ | |
|| ! echo "$1" | grep -q -P -i '^(play|pause|toggle|playpause|p|n|next|skip|fwd|forward|prev|previous|back|stop)$' \ | |
|| ! echo "$2" | grep -q -P '^(all|[0-9,]+|$)$' | |
then | |
helpText | |
echo "Invalid argument" 1>&2 | |
exit 2 | |
fi | |
# cache pids right away | |
MPV_PIDS="$(pgrep -x mpv)" || exit 1 | |
if test -z "$MPV_PIDS" ; then | |
echo "No mpv process found - exiting" 1>&2 | |
exit 1 | |
fi | |
TARGET_ALL=0 | |
TARGET_PIDS='' | |
if [ "$2"x = allx ] ; then | |
TARGET_ALL=1 | |
elif echo "$2" | grep -q -P '^[0-9,]+$'; then | |
TARGET_PIDS="$2" | |
fi | |
CMD_STR='org.mpris.MediaPlayer2.Player.' | |
__DBUS_CMD='dbus-send --session --type=method_call --print-reply --dest=org.mpris.MediaPlayer2.mpv' | |
ACTION="$(echo "$1" | tr "[:upper:]" "[:lower:]")" # Play -> play | |
case "$ACTION" in | |
play) CMD_STR="$CMD_STR"Play;; | |
pause) CMD_STR="$CMD_STR"Pause;; | |
toggle) CMD_STR="$CMD_STR"PlayPause;; | |
playpause) CMD_STR="$CMD_STR"PlayPause;; | |
p) CMD_STR="$CMD_STR"PlayPause;; | |
n) CMD_STR="$CMD_STR"Next;; | |
next) CMD_STR="$CMD_STR"Next;; | |
skip) CMD_STR="$CMD_STR"Next;; | |
fwd) CMD_STR="$CMD_STR"Next;; | |
forward) CMD_STR="$CMD_STR"Next;; | |
prev) CMD_STR="$CMD_STR"Previous;; | |
previous) CMD_STR="$CMD_STR"Previous;; | |
back) CMD_STR="$CMD_STR"Previous;; | |
stop) CMD_STR="$CMD_STR"Stop;; | |
*) exit 99;; | |
esac | |
if test -n "$TARGET_PIDS" ; then | |
echo "$TARGET_PIDS" | grep -o -P '[0-9]+' | while read -r PID; do | |
$__DBUS_CMD".instance""$PID" /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1 \ | |
|| $__DBUS_CMD /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1 | |
done | |
elif [ "$TARGET_ALL" -eq 1 ] ; then | |
$__DBUS_CMD /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1& | |
echo "$MPV_PIDS" | while read -r PID; do | |
$__DBUS_CMD".instance""$PID" /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1& | |
done | |
else | |
if [ "$(echo "$MPV_PIDS" | wc -l)" -eq 1 ] ; then | |
$__DBUS_CMD /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1& | |
elif [ "$(echo "$MPV_PIDS" | wc -l)" -ge 2 ] ; then | |
$__DBUS_CMD".instance""$(xdotool getwindowfocus getwindowpid)" /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1 \ | |
|| $__DBUS_CMD /org/mpris/MediaPlayer2 "$CMD_STR" > /dev/null 2>&1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment