jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
550 stars 68 forks source link

AttributeError: 'MPV' object has no attribute #221

Closed S0yKaf closed 2 years ago

S0yKaf commented 2 years ago

After I upgraded my system, I started getting these errors.

They happen as far as I call tell, on every function call that use the mpv.MPV object. suspecting something is broken with the libmpv bindings.

python version: 3.10.4 libmpv version: libmpv.so.1.109.0 mpv version: 1:0.34.1-4 kernel: 5.17.9-arch1-1

Minimal Reproduction

import mpv

player = mpv.MPV(input_default_bindings=True, input_vo_keyboard=True, osc=True)

overlay = player.create_image_overlay()
img = Image.new('RGBA', (400, 150),  (255, 255, 255, 0))
d = ImageDraw.Draw(img)

Trackback:

Exception in callback MPV.__init__.<locals>.mpv_event_extractor() at /home/kiniamaro/.local/lib/python3.10/site-packages/mpv.py:276
handle: <Handle MPV.__init__.<locals>.mpv_event_extractor() at /home/kiniamaro/.local/lib/python3.10/site-packages/mpv.py:276>
Traceback (most recent call last):
  File "/home/kiniamaro/.local/lib/python3.10/site-packages/mpv.py", line 254, in _event_generator
    raise StopIteration()
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/kiniamaro/.local/lib/python3.10/site-packages/mpv.py", line 278, in mpv_event_extractor
    for event in _event_generator(self.handle):
RuntimeError: generator raised StopIteration
Traceback (most recent call last):
  File "/home/kiniamaro/Git/Python/handy_mpv/test.py", line 5, in <module>
    overlay = player.create_image_overlay()
AttributeError: 'MPV' object has no attribute 'create_image_overlay'
ay'

Let me know if you need any more info from me. On my side I'll do some more digging tomorrow.

S0yKaf commented 2 years ago

So I did some more digging. cloned the repo and ran the tests. all green.

inside my code I found a couple things.

using the on_key_press(event) decorator will throw the AttributeError but using register_key_binding() will work.

# Will throw Attribute Error
@player.on_key_press('down')
def my_down_binding(key_state, key_name, key_char):
    value = player._get_property('playback-time')
    time_ms = int(value * 1000)
    print(time_ms)
    sync_play(time_ms, 'true')

# Will Work

def my_down_binding(key_state, key_name, key_char):
    value = player._get_property('playback-time')
    time_ms = int(value * 1000)
    print(time_ms)
    sync_play(time_ms, 'true')

 player.register_key_binding("down", my_down_binding)

The same is true for event callbacks:

# AttributeError: type object 'MpvEventID' has no attribute 'PAUSE'
@player.event_callback('pause')
def video_pause(event):
    sync_play(0, 'false')

# Works

def on_event(event):
    e = event.as_dict(decoder=mpv.lazy_decoder)["event"]
    match e:
        case "pause":
            video_pause(event)

player.register_event_callback(on_event)
S0yKaf commented 2 years ago

I ended up fixing my code, but there was a lot of changes needed. on last thing I found was that

mpv.quit() stopped working but calling mpv.command("quit") works as intended.

so I'm definitely thinking things changed in the libmpv or something is very wrong with my setup.

dfaker commented 2 years ago

Line 278 or thereabouts of recent releases of mpv.py seems to be in the middle of a class definition yet your error seems to point to it being in the middle of the event logic:

File "/home/kiniamaro/.local/lib/python3.10/site-packages/mpv.py", line 278, in mpv_event_extractor for event in _event_generator(self.handle):

Nor is there any current method called 'mpv_event_extractor'.

Do you perhaps have some older version of the mpv.py installed by your package manager rather than pip?

S0yKaf commented 2 years ago

Line 278 or thereabouts of recent releases of mpv.py seems to be in the middle of a class definition yet your error seems to point to it being in the middle of the event logic:

File "/home/kiniamaro/.local/lib/python3.10/site-packages/mpv.py", line 278, in mpv_event_extractor for event in _event_generator(self.handle):

Nor is there any current method called 'mpv_event_extractor'.

Do you perhaps have some older version of the mpv.py installed by your package manager rather than pip?

Seems like you're right, I just re-tested my example and now it works. no idea how this happened. I think what was happening was that I have a bash script to run my app and activate the venv. but the part that activate the venv wasn't actually working so it was grabbing my none pip version of the lib which was indeed not 1.0.1.

I'll be closing the issue. Thanks for looking into this.