Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: single instance mode #4954

Closed
UserNaem opened this issue Oct 4, 2017 · 29 comments
Closed

Feature request: single instance mode #4954

UserNaem opened this issue Oct 4, 2017 · 29 comments

Comments

@UserNaem
Copy link

UserNaem commented Oct 4, 2017

As in, pass the file(s) and\or URL(s) to an existing mpv instance instead of running another one. It'll be a lot faster than waiting for another instance of mpv to launch.

@Argon-
Copy link
Member

Argon- commented Oct 4, 2017

@UserNaem
Copy link
Author

UserNaem commented Oct 4, 2017

Sadly, I'm not on Linux. I tried making an AutoHotkey macro, too, but mpv silently ignored any PostMessage "file drop" events (whereas notepad didn't).

@Hrxn
Copy link
Contributor

Hrxn commented Oct 4, 2017

Linux only, it seems?

@garoto
Copy link
Contributor

garoto commented Oct 4, 2017

Currently, sending loadfile to mpv's named pipe is the only way around it.

@BurhanDanger
Copy link

Check - https://gist.github.com/dwgill/a66769e0edef69c04d3b . This has example of how to use loadfile.

@stibinator
Copy link

stibinator commented Oct 26, 2017

I'd love this option. I use MPV as my default player on Windows, and need to be able to set the option so that it works when opening the file from explorer as well as from the command line.

My use case is, I'll be looking through a folder of footage or renders on my edit machine. Opening each one opens up a new window, meaning I end up with dozens of videos playing back simultaneously., often hidden behind other windows.

Other players like VLC or MPC* do it, how is it so hard for MPV? Not rhetorical, I'm actually interested.

@kevmitch
Copy link
Member

kevmitch commented Feb 1, 2018

Does MPV have any command internal system that can be used?

JSON IPC

@ghost
Copy link

ghost commented Feb 1, 2018

This is so hard to implement?

Probably not, but I don't see anything in this feature (most programs I use which do it I try to disable it). Let me correct myself: I really hate this feature whenever something uses it. If I start a program, I want a new program, not something retarded like affecting existing instances.

Also even if it were implemented, it'd probably cause a bunch of problems we'd have to care about.

@haasn
Copy link
Member

haasn commented Feb 1, 2018

The way I could see this feature being implemented, FWIW, is basically as an extension of input-ipc-server. Basically, if the filename specified by input-ipc-server already exists, mpv would first connect to it and send a quit message to the existing instance, before replacing it.

Perhaps something like input-ipc-conflict-action=<ignore|replace|error> which translates to something like “do nothing, leave both instances running”, “send a quit message to the existing instance” and “error out and refuse to run if the socket already exists” respectively.

That way the logic for deciding how and when to behave in “single instance mode” is pretty much left to the user, and there are also clear semantics for how this is implemented on each platform, and you could even have multiple coexisting “single instance groups”. (For example, your file manager might be configured to start mpv with the socket pointing at /run/user/$UID/mpv.filemanager.socket, so that clicking on multiple files from your file manager replaces the same instance every time - but doesn't affect mpv as run from the command line)

@ghost
Copy link

ghost commented Feb 1, 2018

I don't think quitting the other instance would cut it - it would destroy and possibly recreate the window, and reset state like the window position or audio volume. The other modes seem rather useless compared to just using a dedicated script like umpv.

@SilverEzhik
Copy link

SilverEzhik commented Mar 22, 2018

Hi, I gave implementing single instance mode for Windows a shot: https://github.com/SilverEzhik/umpvw

It handles things macOS-style - opening a file will replace what you have running`. It also handles opening multiple files as a playlist.

@thekobbyist
Copy link

I don't think quitting the other instance would cut it - it would destroy and possibly recreate the window, and reset state like the window position or audio volume. The other modes seem rather useless compared to just using a dedicated script like umpv.

Most of the time I only need one mpv instance to run and i usually want the newly opened file to replace what's already running. umpv seems to only append the file to playlist and that doesnt cut it for me. I dont really use application volume, mostly set to max and the window position and size is explicitly defined in my mpv config( i either watch a video as a pop up or in fullscreen). I use a custom script to kill the currently running mpv instance and relaunch with the new video. For me the annoying thing is the second or two it takes to kill and relaunch mpv.

Smplayer seems to have this feature and its based on mpv. I would love to see this implemented in mpv

@garoto
Copy link
Contributor

garoto commented Nov 12, 2018

@aiamkobby commented on null:

[...] umpv seems to only append the file to playlist and that doesnt cut it for me.

Change append to replace on line 81.

@AjvarXX
Copy link

AjvarXX commented Apr 26, 2019

You need to make triple-flips for just a basic functionality. This holds for practically anything in mpv.
It already feels I have some darn constructor for thumbnail-preview, volume-control on mouse scroll, On-top only when playing, Hide/Show borders and now single instance?

Let alone for not so basic features

@Tsubajashi
Copy link

@AjvarXX If you think thats a "triple-flip", i suppose you are Not the Type of User the Player is Designed for.
Just Use Other frontends where everything is configured for you already :)

@AjvarXX
Copy link

AjvarXX commented Apr 26, 2019

you are Not the Type of User the Player is Designed for...

Yeah-yeah, I heard same lines from devs and I won't argue saying that you don't hear those if product is indeed ideal, open-source or not. I will let it for their own judging.

  1. Google the question.
  2. Same as for Linux, there are only few results and lucky you if at least one Resolved.
  3. Follow long instructions on downloading/editing configs for something which intuitively deserves 1-click if not Out-of-the-box solution.

@UserNaem
Copy link
Author

UserNaem commented May 5, 2019

Another issue with using umpvw is that it doesn't switch to the mpv window, so if it's covered with other windows you have to manually switch to it.

@victoriastuart
Copy link

This (simple) solution appears to work well. Note: Linux.

MPV SINGLE INSTANCE MODE

  • script: /mnt/Vancouver/programming/scripts/mpv.sh
  • desktop entry: /home/victoria/.local/share/applications/mpv-single-instance.desktop
  • associate (Krusader file manager) FLV, MKV, MP4, MPG, MPEG, AVI, WEBM, ... files with that single-instance program
  • also added to XFCE Whiskers Menu

  • mpv.sh (/mnt/Vancouver/programming/scripts/mpv.sh):
#!/bin/bash
# vim: set filetype=sh :
# vim: syntax=sh
export LANG=C.UTF-8

pid=$(pidof "mpv")

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

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

mpv-single-instance.desktop:

[Desktop Entry]
Version=1.0
Type=Application
Name=MPV (single instance)
Comment=Run only one instance of mpv
# Exec=mpv_single %f
Exec=/mnt/Vancouver/programming/scripts/mpv.sh %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;
111

@Akemi
Copy link
Member

Akemi commented Jul 27, 2019

duplicate of #3811

@hirak99
Copy link

hirak99 commented Mar 25, 2022

This would be good to have implemented natively in mpv.

I get it that usually you would want a new window to launch every time you doubleclick. But that's hardly what I've ever wanted to do for a media player. I don't want multiple songs or movies playing overlapping each other, and hate it when it does that.

Scripts can easily get around it, but it's a workaround. I've been seriously considering to choose VLC or Celluloid only because they do the sane thing by default, and allow single window mode.

@Ferk
Copy link

Ferk commented Apr 24, 2022

[...] umpv seems to only append the file to playlist and that doesnt cut it for me.

Change append to replace on line 81.

This works. But I wonder if there's a command we can send so after appending to the playlist, it could switch to the playlist item we just added, so we can both build a playlist and play the video.

@poxu
Copy link

poxu commented May 3, 2022

I have made a piece of software, which works for Linux and Windows and does what umpv does. It effectively implements single instance mode for mpv. I have named it runmpv.

To use it, make runmpv default program to play video files and specify mpv executable path in runmpv.properties (config file). You don't have to configure that under linux, if mpv executable is in your PATH.

It is written in Java, but compiles to native code. You still need Windows Redistributables under windows, but as for linux, there's no additional dependencies.

runmpv can work like umpv and open all videos in the same window. Also it can open different mpv windows for files from different directories. I kinda needed that ))

You can take binaries from runmpv github page.

I have tested runmpv on Ubuntu Xfce and on Windows 10. I would appreciate if someone tries it on their machine )))

@blueray453
Copy link

blueray453 commented Jul 28, 2022

I have modified @victoriastuart 's code a bit and it now looks like:

#!/bin/bash

pid=$(pidof "mpv")

if [[ "$pid" ]]; then
    xdotool search --class "mpv" search --classname "gl" getwindowgeometry
    COORDINATE_VALUES=$(xdotool search --class "mpv" search --classname "gl" getwindowgeometry | grep Position | awk -v N=2 '{print $N}' | tr ',' ':')
    kill "$pid"
    mpv --player-operation-mode=pseudo-gui --geometry="$COORDINATE_VALUES" "$1"
else
    mpv --player-operation-mode=pseudo-gui "$1"
fi

This is so that it maintain the coordinates of the previous window.

Please let me know if the script can be made better.

@blueray453
Copy link

blueray453 commented Jul 28, 2022

A much better solution given in https://askubuntu.com/a/1322227/598062

#!/bin/bash

pid=$(pidof "mpv")

if [[ "$pid" ]]; then
    wmctrl -x -R gl.mpv
    echo "{ \"command\": [\"loadfile\", \"$1\"] }" | socat - "/tmp/mpv-socket"
else
    mpv --input-ipc-server=/tmp/mpv-socket --player-operation-mode=pseudo-gui "$1"
fi

Or, you can do like:

#!/bin/bash

pid=$(pidof "mpv")

if [[ "$pid" ]]; then
    wmctrl -x -R gl.mpv
    echo "{ \"command\": [\"loadfile\", \"$1\", \"append\"] }" | socat - "/tmp/mpv-socket"
    echo playlist-next | socat - "/tmp/mpv-socket"
    # echo '{ "command": ["playlist-next"] }' | socat - "/tmp/mpv-socket"
else
    mpv --input-ipc-server=/tmp/mpv-socket --player-operation-mode=pseudo-gui "$1"
fi

This way instead of just loading the new file, it will add it to the playlist and then run it. That way you can go to the previous file(s).

IMPORTANT: socat must be installed in your machine.

@deivi98
Copy link

deivi98 commented Jan 16, 2023

Last solution was great but I had a lot of issues when mixing regular files and other kind of sources/inputs. +solution could not accept mpv options as arguments. This script parses options and adds them into command remotely sent to single mpv instance running.

However, there is an issue when parsing force-media-title argument where it cannot take any spaces or commas. I think this has to do with Mpv itself and the way it receives options when using loadfile command. Tried many different things, but only solution I could find was to get rid of spaces and commas. Let me know if you find something better. Enjoy :)

Why I did this? Because I wanted other apps such as gtk-pipe-viewer to use same mpv instance as well. However, it was not possible since they were trying to pass multiple parameters, and even audio, video separately.

#!/bin/bash

MPV_SOCKET="/tmp/mpv-socket"

# Save args and options
POSITIONAL_ARGS=()
OPTIONS=""

# Load options and its values
while [[ $# -gt 0 ]]; do
  case $1 in
    --force-media-title=*)
      # This option must be parsed specifically as loadfile seems to fail loading strings with spaces or commas
      option="${1:20}"
      option_no_commas_or_spaces=$(echo $option | sed 's/,//g' | sed 's/ /_/g')
      OPTIONS="$OPTIONS,force-media-title=$option_no_commas_or_spaces"
      shift
      ;;
    --*=*)
      # Parse regular options (--example=value format)
      OPTIONS="$OPTIONS,${1:2}"
      shift
      ;;
    --*)
      # Parse boolean options (--no-ytdl format)
      OPTIONS="$OPTIONS,${1:2}=1"
      shift
      ;;
    *)
      # Parse usual positional args (file input in this case)
      POSITIONAL_ARGS+=("$1")
      shift
      ;;
  esac
done

# Remove initial options comma (if options specified)
if [[ ! -z "$OPTIONS" ]]; then
  OPTIONS="${OPTIONS:1}"
fi

# Restore positional arguments
set -- "${POSITIONAL_ARGS[@]}"

# Sends commands to Mpv instance
commandMpv () {
  echo $1 | socat - $MPV_SOCKET
}

# Play file mpv command
play () {
  commandMpv "{ \"command\": [\"loadfile\", \"$2\", \"$1\", \"$3\"] }"
} 

# Replaces what it is currently playing
playFirst () {
  play "replace" "$@"
}

# Appends file to queue and instantly plays it next
playNext () {
  wmctrl -x -R gl.mpv
  play "append" "$@"
  commandMpv playlist-next
}

# Starts mpv allowing remote (and insecure) control
start () {
  mpv --input-ipc-server=/tmp/mpv-socket --player-operation-mode=pseudo-gui&
  # Sleep needed to open mpv before immediately sending command with next file
  sleep 0.05
}

pid=$(pidof "mpv")

if [[ "$pid" ]]; then
  # If instance is running
  playNext "$1" "$OPTIONS"
else
  # Start instance and play first file
  start
  playFirst "$1" "$OPTIONS"
fi

@Lxtharia
Copy link

Hello, I've made a similar script that allows to run new files in an already running instance, but with the added option to specify the socket path, so you can have two single-instances for example.

You can check it out here: https://github.com/Lxtharia/si-mpv

@kqvanity
Copy link

I find it weird that there's a different a script for such a trivial opt-in feature, and it doesn't even perform the intended behavior!

@FeyrisTan
Copy link

FeyrisTan commented Oct 19, 2024

I find it weird that there's a different a script for such a trivial opt-in feature, and it doesn't even perform the intended behavior!

While I can understand that it might be frustrating that this is still not an included feature - there are more than enough WORKING methods that have been posted here. The one directly above your comment does work. So does umpv and most of the other ones.

@kqvanity
Copy link

there are more than enough WORKING methods

It's just the idea of using a wrapper script (which still has to be modified) to enable this feature. It's fine now. I've now started using a different approach e.g., keep-open-pause & manually paste URLs which still needed a script to enable copy-pasting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests