Pause programmatically video player mpv
Asked Answered
S

3

16

I would like to know if there is a way to send a message to a running process on linux ?

For example, is it possible to programmatically "pause" a video launched with mpv.

Stratum answered 26/1, 2016 at 11:42 Comment(5)
There is no general way. The process itself would have to implement something which allowed it to receive messages from outside. Just for pausing though, you can send the process a SIGSTOP.Tailback
kill -s STOP 13021 and kill -s CONT 13021. Thanks, that's a brilliant work around ! Is it maybe possible to send a key signal too ?Stratum
What is a "key signal"?Tailback
sorry keystroke. Like Escape, Spacebar...Stratum
I use : xdotool key --window "$(xdotool search --class mpv)" p The key P is used to pause the video.Stratum
G
28

To control mpv remotely (eg from another terminal session) you can also start it with the option

--input-ipc-server=/tmp/mpvsocket

and control it by issuing commands like this:

echo '{ "command": ["set_property", "pause", true] }' | socat - /tmp/mpvsocket

See man mpv for (many) more details.

edit: see also mpv --list-properties

edit2: The most simple way I've found to "toggle" pause/play is

{"command": ["cycle", "pause"]}

Grave answered 21/1, 2017 at 14:16 Comment(2)
with this option by default mpv creates a single socket, which is for the last mpv instance opened. to get multiple sockets per PID, use the mpvSockets pluginSnowber
Wish I had come across that plugin sooner o_o. Ended up writing my own solution to handle communicating with multiple IPC servers by using the epoch time to create multiple sockets.Papism
S
11

kill -s STOP $(pidof mpv) and kill -s CONT $(pidof mpv)

or better :

xdotool key --window "$(xdotool search --class mpv)" p

The key "P", is set by default to pause the video.

Stratum answered 27/1, 2016 at 4:56 Comment(0)
N
8

It's possible to control mpv through IPC. From the manual mpv(1):

--input-ipc-server=<filename>
       Enable the IPC support and create the listening socket at the given path.

       On  Linux and Unix, the given path is a regular filesystem path.
       On Windows, named pipes are used, so the path refers to the pipe namespace (\\.\pipe\<name>). If the \\.\pipe\ prefix is missing, mpv will add it automatically before creating the pipe, so --input-ipc-server=/tmp/mpv-socket and --input-ipc-server=\\.\pipe\tmp\mpv-socket are equivalent for IPC on Windows.

       See JSON IPC for details.

A couple of examples:

$ echo 'cycle pause'   | socat - /tmp/mpv-socket
$ echo 'playlist-prev' | socat - /tmp/mpv-socket
$ echo 'playlist-next' | socat - /tmp/mpv-socket

See mpv(1) to learn more.

See also:

Neurogenic answered 17/9, 2017 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.