How can I control mpv
in command line?
Like is there a mpv next/prev
command that I can use to play next or previous media file?
How can I control mpv
in command line?
Like is there a mpv next/prev
command that I can use to play next or previous media file?
There are two ways to do such a thing:
You can do such a thing with IPC, as man mpv
said:
Using --input-ipc-server is also suitable for purposes like remote control.
First you'll need to enable IPC socketing in your mpv.conf
or ~/.config/mpv/mpv.conf
:
# Enable the IPC support to control mpv from the command-line.
input-ipc-server=~~/socket
If you want to keep it open temporarily and only for one session, you can do:
$ mpv --input-ipc-server=$XDG_CONFIG_HOME/mpv/socket
Now you can control your media:
echo cycle pause | socat - "$XDG_CONFIG_HOME/mpv/socket"
echo playlist-next | socat - "$XDG_CONFIG_HOME/mpv/socket"
echo playlist-prev | socat - "$XDG_CONFIG_HOME/mpv/socket"
To get return values, we send the command as JSON:
echo '{ "command": ["get_property", "playback-time"] }' | socat - "$XDG_CONFIG_HOME/mpv/socket"
{"data":7.026783,"request_id":0,"error":"success"}
Another way to control mpv from command line, is to use mpvc
or such programs, it's like mpc
but for `mpv.
Really powerful, examples:
$ mpvc next # Play next media
$ mpvc prev # Play previous media
$ mpvc mute # Mute media's sound track
$ mpvc unmute # Unmute media's sound track
$ mpvc volume + # Increase volume
$ mpvc volume - # Decrease volume
...
There are multiple options to interact with mpv from a command line, as suggested in the manual e.g.,
In GNU systems you can check /usr/share/doc/mpv/
to have some inspiration on how to configure mpv: input.conf
has default keybindings and mpv.conf
has an example configuration file.
You can check the manual to know about how to use in different operational systems:
While there are multiple ways and programs to handle controlling MPV from the command line, I wasn't satisfied with the existing solutions, so I wrote a script that supports controlling mpv from the command line in Python.
https://github.com/ideasman42/mpvctl/blob/master/mpvctl
$ mpvctl next # Play next media
$ mpvctl prev # Play previous media
$ mpvctl add *.mp3 # Play MP3's in the current directory.
$ mpvctl trash # Move the file to the trash and play the next.
This supports:
The alternatives I found had some limitations (not expanding the $PWD
when adding files - for example), and I found the BASH to handle more involved commands was difficult to follow. Hence the rewrite in Python.