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:

#!/bin/bash

pid=$(pidof "mpv")

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

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]
Version=1.0
Type=Application
Name=mpv Media Player - Single Instance
Comment=Run only one instance of mpv
Exec=mpv_single %f
Icon=mpv
MimeType=application/ogg;application/x-ogg;application/sdp;application/smil;application/x-smil;application/streamingmedia;application/x-streamingmedia;application/vnd.rn-realmedia;application/vnd.rn-realmedia-vbr;audio/aac;audio/x-aac;audio/m4a;audio/x-m4a;audio/mp1;audio/x-mp1;audio/mp2;audio/x-mp2;audio/mp3;audio/x-mp3;audio/mpeg;audio/x-mpeg;audio/mpegurl;audio/x-mpegurl;audio/mpg;audio/x-mpg;audio/rn-mpeg;audio/ogg;audio/scpls;audio/x-scpls;audio/vnd.rn-realaudio;audio/wav;audio/x-pn-windows-pcm;audio/x-realaudio;audio/x-pn-realaudio;audio/x-ms-wma;audio/x-pls;audio/x-wav;video/mpeg;video/x-mpeg;video/x-mpeg2;video/mp4;video/msvideo;video/x-msvideo;video/ogg;video/quicktime;video/vnd.rn-realvideo;video/x-ms-afs;video/x-ms-asf;video/x-ms-wmv;video/x-ms-wmx;video/x-ms-wvxvideo;video/x-avi;video/x-fli;video/x-flv;video/x-theora;video/x-matroska;video/webm;audio/x-flac;audio/x-vorbis+ogg;video/x-ogm+ogg;audio/x-shorten;audio/x-ape;audio/x-wavpack;audio/x-tta;audio/AMR;audio/ac3;video/mp2t;audio/flac;audio/mp4;

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.