jaseg / python-mpv

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

playlist_next() and playlist_prev() beyond end of playlist causes SystemError #275

Closed monkeylugs closed 2 months ago

monkeylugs commented 2 months ago

Not sure if this is just my incompetence but whilst trying to understand the difference between idle=True and keep_open=True I came across a SystemError

The error - (mpv doesn't crash despite the error)

Traceback (most recent call last): File "/home/mark/python_stuff/mpv/./mpv_test_basic.py", line 33, in player.playlist_next() File "/home/mark/python_stuff/mpv_1_0_6.py", line 1315, in playlist_next self.command('playlist_next', mode) File "/home/mark/python_stuff/mpv_1_0_6.py", line 1229, in command _mpv_command_node(self.handle, ppointer, out) File "/home/mark/python_stuff/mpv_1_0_6.py", line 142, in raise_for_ec raise ex SystemError: ('Error running mpv command', -12, (<MpvHandle object at 0x7ed75d77ca70>, <mpv_1_0_6.LP_MpvNode object at 0x7ed75d16c170>, <mpv_1_0_6.LP_MpvNode object at 0x7ed75d16c0e0>))

An example to replicate the error -

#!/usr/bin/env python3

import mpv
import time

path = "/home/mark/python_stuff/video_clips/"
mpv_playlist_list = ["media_0.ts", "media_1.ts", "media_2.ts", "media_3.ts", "media_4.ts", "media_5.ts", "media_6.ts"]

print("create player")
player = mpv.MPV(
                    ytdl=False,
                    input_default_bindings=True,
                    input_vo_keyboard=True,
                    osc=True,
                    prefetch_playlist=True,
                    autofit="30%",
                    # idle=True,
                    keep_open=True
                    )
for clip in mpv_playlist_list:
    print(f"playlist append = {path}{clip}")
    player.playlist_append(f"{path}{clip}")

print("playlist_pos = 0")
player.playlist_pos = 0

time.sleep(2)

for i in range(7):
    player.playlist_next()

x = input("press enter to quit")

This came up when experimenting with keybindings with the default key bindings disabled, i'd bound keys to playlist_next() and playlist_prev() as this gave an obvious way to show the key bindings were working. Interestingly using the default key bindings of < & > to move through a playlist the error isn't triggered.

My rudimentary understanding of the source code suggests that playlist_next() default mode = weak, so advancing beyond the ends of the playlist should be pretty much ignored.

Apologies if I'm just doing something stupid

jaseg commented 2 months ago

This error comes from libmpv, and is just bubbled up by python-mpv. Looking at mpv's source code, mpv's documentation on the playlist-next and playlist-prev commands is a bit unclear. The meaning of weak versus force is that force will terminate playback, i.e. stop the file that is currently playing, if it is at the end of the playlist, while weak will leave it playing. However, in either case, libmpv will report a "command" error. See the source here for reference.

You can safely ignore this error with a try: ... except SystemError: pass in your python code.