python-mpv icon indicating copy to clipboard operation
python-mpv copied to clipboard

please provide a merged example of pyQt usage AND observer/key_press callbacks

Open tempest766 opened this issue 3 months ago • 3 comments

The Qt example works fine accept there is no documented indication how to implement the observers or key bindings. The example on observer/key-binding requires the player object to be global, which doesn't fit in with a well designed OO plan.

I can find no syntax for

@player.property_observer('time-pos') or @player.on_key_press('q') that works as a class method where player is a class instance variable.

Alternately what observer/callbacks can be presented from the mpv module when run inside the pyQt event loop?

Are the two mutually incompatible? If so, it would be good to document that rather than rely purely on "documentation by example".

Thoughts?

tempest766 avatar Oct 03 '25 16:10 tempest766

You can use the following snippet:


class MyPlayer:

    def __init__(self):
        self.mpv = MPV(ytdl=True, vo='libmpv', terminal="yes", msg_level="all=v")
        self.mpv.observe_property("time-pos", self._on_player_time_pos_changed)

    def _on_player_time_pos_changed(self, property, value):
        if value:
            print("Player time-pos changed", property, value)

and the output will be:

Player time-pos changed time-pos 1.7919999999999998
Player time-pos changed time-pos 1.833
Player time-pos changed time-pos 1.875
Player time-pos changed time-pos 1.9169999999999998
Player time-pos changed time-pos 1.958
Player time-pos changed time-pos 2.0

trin94 avatar Oct 13 '25 09:10 trin94

Thanks, that answers the question about observers, but what about passing mpv keybindings back to the mpv class for stuff like orderly exit?

When I hit 'Q' in the player window it needs to notify the mpv class so it can cleanly terminate the viewer and exit the app. It is unclear from the pydoc info whether the key_bindings function are callbacks to the mpv class, or just passthroughs to the player.

something like self.player.keybind('Q',self.done) doesn't execute the self.done method, but does terminated the mpv player itself when Q is pressed in player window.

tempest766 avatar Oct 13 '25 14:10 tempest766

I'm a bit unclear what you mean exactly.

When you register a key binding, python-mpv tells libmpv to emit events for that key by setting up a scripted key binding in libmpv. It also puts the handler function you provide into an internal registry. Then, whenever libmpv sends a key event, python-mpv looks up your handler function from the registry and calls it.

If that doesn't work for some reason, the most likely causes are either:

  1. (keys don't work at all) Your video output (vo) doesn't support keyboard input
  2. (something else happens) You might have loaded mpv's default key bindings. You can register new key bindings using python-mpv, but doing that doesn't overwrite any key bindings that are already registered. So if you want to change what a key does that has a default binding, you not just have to register your own handler, you also have to unregister the default handler.

In general, to avoid problems like (2), it's best to provide your own key binding script instead of relying on mpv's defaults, especially because those could change with mpv versions.

jaseg avatar Nov 14 '25 09:11 jaseg