Last active
October 2, 2024 22:34
-
-
Save dwgill/a66769e0edef69c04d3b to your computer and use it in GitHub Desktop.
A small script for controlling mpv via the JSON IPC. Intended to resemble playerctl.
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
#!/usr/bin/env bash | |
# This script requires: | |
# - that the directory $HOME/.mpv exist | |
# - that the program socat be installed | |
# - that you start mpv with the unix socket feature pointing at that directory | |
# I recommend an alias in your .bashrc or equivalent file: | |
# alias mpv="mpv --input-unix-socket=$HOME/.mpv/socket" | |
socket="$HOME/.mpv/socket" | |
command() { | |
# JSON preamble. | |
local tosend='{ "command": [' | |
# adding in the parameters. | |
for arg in "$@"; do | |
tosend="$tosend \"$arg\"," | |
done | |
# closing it up. | |
tosend=${tosend%?}' ] }' | |
# send it along and ignore output. | |
# to print output just remove the redirection to /dev/null | |
echo $tosend | socat - $socket &> /dev/null | |
} | |
# exit mpv | |
[ "$1" = "stop" ] && command 'quit' | |
# toggle play-pause | |
[ "$1" = "play-pause" ] && command 'cycle' 'pause' | |
# start playing | |
[ "$1" = "pause" ] && command 'set' 'pause' 'yes' | |
# stop playing | |
[ "$1" = "play" ] && command 'set' 'pause' 'no' | |
# play next item in playlist | |
[ "$1" = "next" ] && command 'playlist_next' | |
# play previous item in playlist | |
[ "$1" = "previous" ] && command 'playlist_prev' | |
# add item(s) to playlist | |
[ "$1" = "add" ] && shift && | |
for video in "$@"; do | |
command 'loadfile' "$video" 'append-play'; | |
done; |
Thank you very much, exactly what I was looking for!
Thank you very much, exactly what I was looking for!
Exactly that. Although using --input-unix-socket gave me a warning for possible deprecation in the future. Just needed to change it to --input-ipc-server.
This is probably a small subset of mpvc
.
https://github.com/lwilletts/mpvc/blob/master/mpvc
@dwgill nice ❤️
Starting using this script, while nice I wanted more advanced functionality so ported it to Python:
https://github.com/ideasman42/mpvctl/
... resolves the issue that paths from add
needed to be absolute, mpvctl add *.mp3
would fail for e.g.
input-unix-socket
was completely deprecated and doesn't work anymore. --input-ipc-server
works
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you very much for this