1

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?

1
  • mpv is a command-line program (for next and previous press < and > in the command-line while it's running). Are you wondered how to remote control it from another shell? Commented Jul 28, 2022 at 4:41

3 Answers 3

2

There are two ways to do such a thing:

  • Setup IPC for mpv and socket controlling
  • Using 3rd party scripts

Setup IPC for mpv

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:

Play / Pause

echo cycle pause | socat - "$XDG_CONFIG_HOME/mpv/socket"

Next Track

echo playlist-next | socat - "$XDG_CONFIG_HOME/mpv/socket"

Previous Track

echo playlist-prev | socat - "$XDG_CONFIG_HOME/mpv/socket"

Get playback time

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"}

Using an external program

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
...
1

There are multiple options to interact with mpv from a command line, as suggested in the manual e.g.,

  • keybindings (using 'input.conf')
  • OSD
  • JSON IPC
  • client API (libmpv) ...

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.confhas an example configuration file.

You can check the manual to know about how to use in different operational systems:

https://mpv.io/manual/master/#command-interface

1

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:

  • Basics such as pause/play, next/previous .. etc.
  • Relative seeking.
  • Adding file(s), optionally replacing the current playlist.
  • Moving file playing to the trash.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .