mpv icon indicating copy to clipboard operation
mpv copied to clipboard

Add support to use only one instance of mpv

Open gperezmz opened this issue 13 years ago • 16 comments

Maybe I'm just lacking of reading comprehension, but I can't see a way to achieve this on the man page. Is it possible to make an option to use the same mpv instance to play files? Instead of having to close one first and then open another one.

I know you can achieve this with an input file and loadfile PATH but...

gperezmz avatar Mar 14 '13 14:03 gperezmz

On what operating system? On OSX, there's a way (you have to use the bundle functionality).

For other platforms: No, there's no such feature yet. I can't say I like the idea. Implementing this would probably require lots of weird extra code (checking a named pipe on startup? something like this) for a feature I usually hate. I would accept a patch though. (As long as it doesn't use dbus.)

ghost avatar Mar 14 '13 15:03 ghost

Well, I hope someone who actually knows programming can implement this. I was talking about Linux, sorry didn't say it.

gperezmz avatar Mar 14 '13 18:03 gperezmz

On Linux you can use "pidof" or a similar program (pgrep, probably others) to simply check if there is a mpv process. If pidof succeeds you know that mpv is already running.

You can launch mpv with a bash script like this to check if there is already a mpv process.

#! /bin/bash
if pidof mpv
then echo "mpv already running"
else mpv $1
fi

It will print a message ("mpv already running") if there is already a mpv process. If there in no mpv process it will start mpv and pass the first argument called to the script to mpv. If you want to pass more than one argument to mpv you can quote them all together like this when you call the script.

one_mpv "--ao=alsa file_name.mkv"

You can make it more complicated by replacing the message with something else like killing the already running mpv process (or closing it cleanly by sending keyboard commands with xdotool) and then starting another mpv process.

arclance avatar May 23 '13 00:05 arclance

I made a small Python script that implements something like this: https://github.com/mpv-player/mpv/blob/master/TOOLS/umpv

There are some caveats, so read the comments at the start of the script.

Tell me if you tried it and whether you like it.

ghost avatar Jul 28 '13 21:07 ghost

Well, there's that script, and since there's no (further) interest, I'll just close this.

Feel free to comment or reopen, though.

ghost avatar Aug 04 '13 21:08 ghost

Wow what? I am so surprised this is not implemented? How??

MavropaliasG avatar May 17 '20 13:05 MavropaliasG

Because the main developer of this software thinks the concept is excruciatingly stupid.

ghost avatar May 17 '20 13:05 ghost

It clearly isn't @wm4 , in fact this would be the expected behavior of a video player. Why not even allow it as a configuration for people who don't think it's excruciatingly stupid?

MavropaliasG avatar May 20 '20 04:05 MavropaliasG

It clearly isn't @wm4 , in fact this would be the expected behavior of a video player. Why not even allow it as a configuration for people who don't think it's excruciatingly stupid?

I'm assuming you didn't read the thread and find the script that @wm4 went to the trouble of authoring, testing and publishing for this exact use case? Does that not cover your use case?

(FWIW I also think this sort of "detect other running instances and send commands to them" is extremely unexpected and stupid, borderline nonsensical behavior for a command line program. At best I could be convinced to only enable it in pseudo-gui mode)

haasn avatar May 20 '20 08:05 haasn

Even for GUI programs, this kind of behavior is inconvenient and annoying. Why the hell would you not start a new program when I ask for a new program?

ghost avatar May 20 '20 09:05 ghost

Single Instance

mpv/scripts/single_instance.lua

  • Windows:
local mp = require 'mp'
local utils = require 'mp.utils'

function close_other_instances()
    local current_pid = utils.getpid()

    local process_list = utils.subprocess({
        args = { "tasklist", "/FI", "IMAGENAME eq mpv.exe" },
        cancellable = false
    })

    if process_list.status == 0 then
        for line in process_list.stdout:gmatch("[^\r\n]+") do
            local pid = line:match("mpv%.exe%s+%d+")
            if pid then
                pid = tonumber(pid:match("%d+"))
                if pid and pid ~= current_pid then
                    utils.subprocess({
                        args = { "taskkill", "/PID", tostring(pid), "/F" },
                        cancellable = false
                    })
                end
            end
        end
    end
end

mp.register_event("start-file", close_other_instances)
  • Linux:
local mp = require 'mp'
local utils = require 'mp.utils'

function close_other_instances()
    local current_pid = utils.getpid()

    local process_list = utils.subprocess({
        args = { "pgrep", "-x", "mpv" },
        cancellable = false
    })

    if process_list.status == 0 then
        for pid in process_list.stdout:gmatch("%d+") do
            pid = tonumber(pid)
            if pid and pid ~= current_pid then
                utils.subprocess({
                    args = { "kill", tostring(pid) },
                    cancellable = false
                })
            end
        end
    end
end

mp.register_event("start-file", close_other_instances)

fahri314 avatar Nov 24 '24 15:11 fahri314

Even for GUI programs, this kind of behavior is inconvenient and annoying. Why the hell would you not start a new program when I ask for a new program?

Because many programmers are stupified by the obvious.

Digita1Menace avatar Nov 13 '25 14:11 Digita1Menace

Because the main developer of this software thinks the concept is excruciatingly stupid.

WOW... is it that same ass clown who made VLC? That dude is PURE TRASH. People begged that fuktard to allow VLC to simply open up the next file in a directory FOR YEARS ans the maggot refused to impliment it.

Digita1Menace avatar Nov 13 '25 14:11 Digita1Menace

Single Instance

mpv/scripts/single_instance.lua I'm going to have to try this because Mpv.Net [which allows 1 instance] has a couple annoying broken features as well. Thanks.

Digita1Menace avatar Nov 13 '25 14:11 Digita1Menace

WOW... is it that same ass clown who made VLC? That dude is PURE TRASH. People begged that fuktard to allow VLC to simply open up the next file in a directory FOR YEARS ans the maggot refused to impliment it.

If you pay me a billion dollars I'll implement this feature in mpv and VLC.

llyyr avatar Nov 13 '25 14:11 llyyr

WOW... is it that same ass clown who made VLC? That dude is PURE TRASH. People begged that fuktard to allow VLC to simply open up the next file in a directory FOR YEARS ans the maggot refused to impliment it.

If you pay me a billion dollars I'll implement this feature in mpv and VLC.

LOL, yo couldn't pay me a billion to ever touch that POS player [shit LC] again..

Digita1Menace avatar Nov 13 '25 14:11 Digita1Menace