Single instance of mpv

Published: Sat, 06 May 2017

In Linux

tags: mpv

Translations: bs

mpv is a pretty cool media player. It's simple and gets the job done. I've been using it since I started using Linux distributions. I like the simple config and keyboard focus. I even went so far to remove all keybindings and only assign those I actually use. The only downside it has is the fact that it launches a new instance (window) for every file that I open.

There's a script in the official mpv repository which is supposed to solve that problem. It does indeed reuse the opened instance of mpv, but it appends newly opened files to the playlist instead of replacing the currently playing video with it. That's the behaviour I desire, so the script isn't really useful for me.

Hacky solution

I figured that I could just use a hacky workaround which consisted of killing any open mpv instances and then launching our new one. After some trial and error and the help of the internet, I settled on this:


pid=$(pidof "mpv")

if [[ "$pid" ]]; then
  kill "$pid"

mpv --player-operation-mode=pseudo-gui "$1"

Save that into a file, make it executable and put it in your $PATH.

Next we need a desktop file. I slightly tweaked the example provided in the standard specification and this is the end result. For Exec= we have to specify the name of our above mentioned script.

[Desktop Entry]
Name=mpv Media Player - Single Instance
Comment=Run only one instance of mpv
Exec=mpv_single %f

That's pretty much it. Now all I had to do was associate media files with our new mpv Media Player - Single Instance desktop file via my file manager and I was set.

Arch users can use my provided PKGBUILD. Everyone else can find the Bash script and desktop file here.