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

Bit of Confusion #9

Open
TingTingin opened this issue Jun 3, 2021 · 4 comments
Open

Bit of Confusion #9

TingTingin opened this issue Jun 3, 2021 · 4 comments

Comments

@TingTingin
Copy link

Should probably mention first that I am new to this (both python and mpv)

I'm trying to use this library to write some mpv scripts but I'm confused about a few things

  1. How to return data from mpv

    • Looking at the data only figured out how to send commands using mpv.command(command, **args) but was unable to figure how to get mpv to send back data best i could do was wait for a property to change with mpv.wait_for_property but this doesnt seem to return any data
  2. Where I find the properties that you can wait on with mpv.wait_for_property using mpv.io

    • I was able to find command names for mpv.command but not property names I could find some things under https://mpv.io/manual/master/#options but they don't seem to work do I need to format them in a particular way to use them
  3. How to connect to running instance of mpv

    • I saw that you can connect to running instance of mpv that's connected to a particular socket but when I use MPV(start_mpv=False, ipc_socket="/tmp/mpv-socket") it doesn't connect is there something I need to do to make my running mpv connect to a socket as of now I've only been able to work with mpv that is launched from PATH
  4. How to connect to running instance of mpv

    • How to use commands like mpv.volume = 20 I saw this in the docs where you change the volume with just a dot I am unsure where to find the attributes you can adjust like this I can only change things thus far with mpv.command(command, **args) not sure if this is maybe connected to the properties question I just asked, for example is there a mpv.playtime or mpv.subfile ETC.
@TingTingin
Copy link
Author

After some digging was able to figure this out most on my own here are the answers

How to return data from mpv

Not sure if there's another way but what I found is that you can use mpv.command with get-property like so

current_play_time = mpv.command("get_property", "time-pos")

This will return the current playtime of the file

Where I find the properties that you can wait on with mpv.wait_for_property using mpv.io

https://mpv.io/manual/master/#property-list

you can also set properties with

mpv.command("set_property", value_you_want_to_set)

e.g mpv.command("set_property", "pause", "yes") this will pause mpv

How to connect to running instance of mpv

still dont know

How use special commands

special commands are found here https://mpv.io/manual/master/#options you can use them like so

mpv.volume = 10
mpv.sid = 1 this selects the first sub file in mpv

@iwalton3
Copy link
Owner

iwalton3 commented Jun 5, 2021

You can set or get properties and invoke commands directly on the MPV object too. Just replace the dashes with underscores.

@TingTingin
Copy link
Author

You can set or get properties and invoke commands directly on the MPV object too. Just replace the dashes with underscores.

Still don't no how to use a running MPV though when I use this

MPV(start_mpv=False, ipc_socket="/tmp/mpv-socket")

It fails and says it cant connect

Also can I use a running MPV if I already know the location if I use MPV() I launches a MPV even if the MPV in path is already open

@TingTingin
Copy link
Author

Ok after some more digging again I was also able to figure out how to connect to a running instance what you do is add

input-ipc-server=/tmp/mpvsocket

To your mpv.conf which should be in your %appdata%/mpv folder if its not there create a mpv.conf file in the folder and add the mpv.conf there and then add this to your script as the socket /tmp/mpvsocket

However for me I was trying to make a script for mpv for other people to use so this wasn't sufficient what I ended having to do was create a lua script for mpv with the following code

local function test()
	
	file = io.open(mp.get_script_directory() .. "/settings.txt", "w")
	ipc_handle_path = mp.get_property('input-ipc-server')
	
	if ipc_handle_path == '' or ipc_handle_path == nil then
        local new_ipc_handle_path = '/tmp/mpv-ipc-handle-' .. os.time()
        mp.set_property('input-ipc-server', new_ipc_handle_path)
        ipc_handle_path = mp.get_property('input-ipc-server')
    end
	
	file:write(ipc_handle_path)
	file:close()

	mp.commandv("run", "python3", mp.get_script_directory() .. '/mpv_pauser.py')
	
end

test()

What this does is

  • When mpv is run it checks the server mpv is connected to

ipc_handle_path = mp.get_property('input-ipc-server')

  • If no connections are there then it creates one

if ipc_handle_path == '' or ipc_handle_path == nil then

  • Then it creates a text file in the directory its in that contains the mpv server that it connected to writen it the first line

file = io.open(mp.get_script_directory() .. "/settings.txt", "w")
file:write(ipc_handle_path)

  • Then it runs my python script

mp.commandv("run", "python3", mp.get_script_directory() .. '/mpv_pauser.py')

My script on boot reads the file that was created in the directory and launches connected to the socket that is listed in the file

with open(f"{cwd}\\settings.txt", "r") as f:
    socket = f.readline()

mpv = MPV(start_mpv=False, ipc_socket=socket)

A few notes

  • You should probably put the python and lua scripts into a subdirectory in the mpv/scripts in which case you need to name the lua file main.lua for mpv to run it on boot
  • Using mp.commandv is important if you use mp.command when running the python script it may not work
  • Remember to use the file:close() in lua otherwise you might have issues working with the file as it will stay open
  • If your names has spaces in lua you need to wrap your string in [[]] like this "mpv_pauser" works [[mpv pauser]] now cuz of the space

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

2 participants