From f4086d4bb44603dcbaac51a162b280f760cbde2c Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 15 Jul 2024 14:52:59 +0200 Subject: [PATCH 01/10] Add API to set dict-valued properties --- mpv.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mpv.py b/mpv.py index 8941aed..94eeb85 100644 --- a/mpv.py +++ b/mpv.py @@ -2064,7 +2064,10 @@ class MPV(object): def _set_property(self, name, value): self.check_core_alive() ename = name.encode('utf-8') - if isinstance(value, (list, set, dict)): + if isinstance(value, dict): + _1, _2, _3, pointer = _make_node_str_map(value) + _mpv_set_property(self.handle, ename, MpvFormat.NODE, pointer) + elif isinstance(value, (list, set)): _1, _2, _3, pointer = _make_node_str_list(value) _mpv_set_property(self.handle, ename, MpvFormat.NODE, pointer) else: From 775fc4a868fa8a84b6e1830a42cba4847a72e961 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 16 Jul 2024 11:10:36 +0200 Subject: [PATCH 02/10] Add test for dict-valued properties --- tests/test_mpv.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_mpv.py b/tests/test_mpv.py index 6a962f0..a59ade1 100755 --- a/tests/test_mpv.py +++ b/tests/test_mpv.py @@ -210,6 +210,11 @@ class TestProperties(MpvTestCase): # See comment in test_property_decoding_invalid_utf8 self.m.osd.alang + def test_dict_valued_property(self): + nasty_stuff = '\xe2\x80\x8e Mozilla/5.0 Foobar \xe2\x80\x8e \xe2\x80\x81' + self.m.ytdl_raw_options = {'user-agent': nasty_stuff} + self.assertEqual(self.m.ytdl_raw_options, {'user-agent': nasty_stuff}) + def test_option_read(self): self.m.loop = 'inf' self.m.play(TESTVID) From ef3f47c3ec8e276d4e25fd9ec2a7f06afd7df1ea Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 14 Aug 2024 10:48:37 +0200 Subject: [PATCH 03/10] Fix quit and quit_watch_later commands --- mpv.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mpv.py b/mpv.py index 94eeb85..fa6666f 100644 --- a/mpv.py +++ b/mpv.py @@ -1375,11 +1375,17 @@ class MPV(object): def quit(self, code=None): """Mapped mpv quit command, see man mpv(1).""" - self.command('quit', code) + if code is not None: + self.command('quit', code) + else: + self.command('quit') def quit_watch_later(self, code=None): """Mapped mpv quit_watch_later command, see man mpv(1).""" - self.command('quit_watch_later', code) + if code is not None: + self.command('quit_watch_later', code) + else: + self.command('quit_watch_later') def stop(self, keep_playlist=False): """Mapped mpv stop command, see man mpv(1).""" From 3d09f5199e73dd010b22e81709452cc0117d73e7 Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 14 Aug 2024 10:55:51 +0200 Subject: [PATCH 04/10] Windows: Improve DLL loading error messages --- mpv.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mpv.py b/mpv.py index fa6666f..d784db5 100644 --- a/mpv.py +++ b/mpv.py @@ -38,7 +38,15 @@ if os.name == 'nt': dll = ctypes.util.find_library('mpv-2.dll') or ctypes.util.find_library('libmpv-2.dll') or ctypes.util.find_library('mpv-1.dll') if dll is None: raise OSError('Cannot find mpv-1.dll, mpv-2.dll or libmpv-2.dll in your system %PATH%. One way to deal with this is to ship the dll with your script and put the directory your script is in into %PATH% before "import mpv": os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].') - backend = CDLL(dll) + # flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR + # cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa + try: + backend = CDLL(dll, 0x00001000 | 0x00000100) + except Exception as e: + if not os.path.isabs(dll): + raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it. It looks like find_library found mpv.dll under a relative path entry in %PATH%. Please make sure all paths in %PATH% are absolute. Instead of trying to load mpv.dll from the current working directory, put it somewhere next to your script and add that path to %PATH% using os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"]') from e + else: + raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it.') from e fs_enc = 'utf-8' else: import locale From 16cd0b338e50419713b2d7bd8fea956cb593c303 Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 14 Aug 2024 11:05:01 +0200 Subject: [PATCH 05/10] Windows: Look for mpv.dll next to mpv.py --- README.rst | 5 +++-- mpv.py | 26 +++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 1341efc..db57f22 100644 --- a/README.rst +++ b/README.rst @@ -29,8 +29,9 @@ submit a `pull request`_ on github. On Windows you can place libmpv anywhere in your ``%PATH%`` (e.g. next to ``python.exe``) or next to this module's ``mpv.py``. Before falling back to looking in the mpv module's directory, python-mpv uses the DLL search order built -into ctypes, which is different to the one Windows uses internally. Consult `this stackoverflow post -`__ for details. +into ctypes, which is different to the one Windows uses internally. You can modify `%PATH%` before importing python-mpv +to modify where python-mpv looks for the DLL. Consult `this stackoverflow post `__ +for details. Python >= 3.9 ............. diff --git a/mpv.py b/mpv.py index d784db5..cd843a2 100644 --- a/mpv.py +++ b/mpv.py @@ -2,7 +2,7 @@ # vim: ts=4 sw=4 et # # Python MPV library module -# Copyright (C) 2017-2022 Sebastian Götte +# Copyright (C) 2017-2024 Sebastian Götte # # python-mpv inherits the underlying libmpv's license, which can be either GPLv2 or later (default) or LGPLv2.1 or # later. For details, see the mpv copyright page here: https://github.com/mpv-player/mpv/blob/master/Copyright @@ -24,6 +24,7 @@ import ctypes.util import threading import queue import os +import os.path import sys from warnings import warn from functools import partial, wraps @@ -35,19 +36,30 @@ import traceback if os.name == 'nt': # Note: mpv-2.dll with API version 2 corresponds to mpv v0.35.0. Most things should work with the fallback, too. - dll = ctypes.util.find_library('mpv-2.dll') or ctypes.util.find_library('libmpv-2.dll') or ctypes.util.find_library('mpv-1.dll') - if dll is None: - raise OSError('Cannot find mpv-1.dll, mpv-2.dll or libmpv-2.dll in your system %PATH%. One way to deal with this is to ship the dll with your script and put the directory your script is in into %PATH% before "import mpv": os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].') - # flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR - # cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa + names = ['mpv-2.dll', 'libmpv-2.dll', 'mpv-1.dll'] + for name in names: + dll = ctypes.util.find_library(name) + if dll: + break + else: + for name in names: + dll = os.path.join(os.path.dirname(__file__), name) + if os.path.isfile(dll): + break + else: + raise OSError('Cannot find mpv-1.dll, mpv-2.dll or libmpv-2.dll in your system %PATH%. One way to deal with this is to ship the dll with your script and put the directory your script is in into %PATH% before "import mpv": os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].') + try: + # flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR + # cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa backend = CDLL(dll, 0x00001000 | 0x00000100) except Exception as e: - if not os.path.isabs(dll): + if not os.path.isabs(dll): # can only be find_library, not the "look next to mpv.py" thing raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it. It looks like find_library found mpv.dll under a relative path entry in %PATH%. Please make sure all paths in %PATH% are absolute. Instead of trying to load mpv.dll from the current working directory, put it somewhere next to your script and add that path to %PATH% using os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"]') from e else: raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it.') from e fs_enc = 'utf-8' + else: import locale lc, enc = locale.getlocale(locale.LC_NUMERIC) From e6e9313af38dc4e6f650ac893b712eaa058b0b5a Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 25 Aug 2024 13:45:13 +0200 Subject: [PATCH 06/10] Tests: Fix race condition in test_wait_for_property_concurrency --- tests/test_mpv.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_mpv.py b/tests/test_mpv.py index a59ade1..9c42332 100755 --- a/tests/test_mpv.py +++ b/tests/test_mpv.py @@ -49,7 +49,8 @@ def timed_print(): start_time = time.time() def do_print(level, prefix, text): td = time.time() - start_time - print('{:.3f} [{}] {}: {}'.format(td, level, prefix, text), flush=True) + print('{:.3f} [{}] {}: {}'.format(td, level, prefix, text.strip()), flush=True) + return do_print class MpvTestCase(unittest.TestCase): @@ -922,11 +923,10 @@ class RegressionTests(MpvTestCase): def test_wait_for_property_concurrency(self): players = [mpv.MPV(vo=testvo, loglevel='debug', log_handler=timed_print()) for i in range(2)] - try: for _ in range(150): for player in players: - player.play('tests/test.webm') + player.loadfile('tests/test.webm', loop='inf') for player in players: player.wait_for_property('seekable') for player in players: From 1bc7e2525e166e963bf9449c07a1c13135cf85f6 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 21 Dec 2024 14:31:14 +0100 Subject: [PATCH 07/10] Use callback id() instead of frame hash() to identify anonymous python streams Frame hashes are not unique since the frame isn't kept around for the life time of the stream. Fixes #292. --- mpv.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/mpv.py b/mpv.py index cd843a2..03b7828 100644 --- a/mpv.py +++ b/mpv.py @@ -1956,6 +1956,10 @@ class MPV(object): Any given name can only be registered once. The catch-all can also only be registered once. To unregister a stream, call the .unregister function set on the callback. + If name is None (the default), a name and corresponding python:// URI are automatically generated. You can + access the name through the .stream_name property set on the callback, and the stream URI for passing into + mpv.play(...) through the .stream_uri property. + The generator signals EOF by returning, manually raising StopIteration or by yielding b'', an empty bytes object. @@ -1972,16 +1976,25 @@ class MPV(object): reader.unregister() """ def register(cb): + nonlocal name + if name is None: + name = f'__python_mpv_anonymous_python_stream_{id(cb)}__' + if name in self._python_streams: raise KeyError('Python stream name "{}" is already registered'.format(name)) + self._python_streams[name] = (cb, size) def unregister(): if name not in self._python_streams or\ self._python_streams[name][0] is not cb: # This is just a basic sanity check raise RuntimeError('Python stream has already been unregistered') del self._python_streams[name] + cb.unregister = unregister + cb.stream_name = name + cb.stream_uri = f'python://{name}' return cb + return register @contextmanager @@ -2003,10 +2016,8 @@ class MPV(object): """ q = queue.Queue() - frame = sys._getframe() - stream_name = f'__python_mpv_play_generator_{hash(frame)}' - EOF = frame # Get some unique object as EOF marker - @self.python_stream(stream_name) + EOF = object() # Get some unique object as EOF marker + @self.python_stream() def reader(): while (chunk := q.get()) is not EOF: if chunk: @@ -2017,21 +2028,19 @@ class MPV(object): q.put(chunk) # Start playback before yielding, the first call to reader() will block until write is called at least once. - self.play(f'python://{stream_name}') + self.play(reader.stream_uri) yield write q.put(EOF) def play_bytes(self, data): """ Play the given bytes object as a single file. """ - frame = sys._getframe() - stream_name = f'__python_mpv_play_generator_{hash(frame)}' - @self.python_stream(stream_name) + @self.python_stream() def reader(): yield data reader.unregister() # unregister itself - self.play(f'python://{stream_name}') + self.play(reader.stream_uri) def python_stream_catchall(self, cb): """ Register a catch-all python stream to be called when no name matches can be found. Use this decorator on a From 12850b34bd3b64704f8abd30341a647a73719267 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 21 Dec 2024 14:47:38 +0100 Subject: [PATCH 08/10] Add support for libmpv's new args to key binding handlers This changes the API, check your code if you use key bindings. --- mpv.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mpv.py b/mpv.py index 03b7828..c227b33 100644 --- a/mpv.py +++ b/mpv.py @@ -1698,9 +1698,10 @@ class MPV(object): def _binding_name(callback_or_cmd): return 'py_kb_{:016x}'.format(hash(callback_or_cmd)&0xffffffffffffffff) - def on_key_press(self, keydef, mode='force'): + def on_key_press(self, keydef, mode='force', repetition=False): """Function decorator to register a simplified key binding. The callback is called whenever the key given is - *pressed*. + *pressed*. When the ``repetition=True`` is passed, the callback is called again repeatedly while the key is held + down. To unregister the callback function, you can call its ``unregister_mpv_key_bindings`` attribute:: @@ -1720,8 +1721,8 @@ class MPV(object): def register(fun): @self.key_binding(keydef, mode) @wraps(fun) - def wrapper(state='p-', name=None, char=None): - if state[0] in ('d', 'p'): + def wrapper(state='p-', name=None, char=None, *_): + if state[0] in ('d', 'p') or (repetition and state[0] == 'r'): fun() return wrapper return register @@ -1729,8 +1730,11 @@ class MPV(object): def key_binding(self, keydef, mode='force'): """Function decorator to register a low-level key binding. - The callback function signature is ``fun(key_state, key_name)`` where ``key_state`` is either ``'U'`` for "key - up" or ``'D'`` for "key down". + The callback function signature is ``fun(key_state, key_name, key_char, scale, arg)``. + + The key_state contains up to three chars, corresponding to the regex ``[udr]([m-][c-]?)?``. ``[udr]`` means + "key up", "key down", or "repetition" for when the key is held down. "m" indicates mouse events, and "c" + indicates key up events resulting from a logical cancellation. For details check out the mpv man page. The keydef format is: ``[Shift+][Ctrl+][Alt+][Meta+]`` where ```` is either the literal character the key produces (ASCII or Unicode character), or a symbolic name (as printed by ``mpv --input-keylist``). @@ -1785,12 +1789,12 @@ class MPV(object): raise TypeError('register_key_binding expects either an str with an mpv command or a python callable.') self.command('enable-section', binding_name, 'allow-hide-cursor+allow-vo-dragging') - def _handle_key_binding_message(self, binding_name, key_state, key_name=None, key_char=None): + def _handle_key_binding_message(self, binding_name, key_state, key_name=None, key_char=None, scale=None, arg=None, *_): binding_name = binding_name.decode('utf-8') key_state = key_state.decode('utf-8') key_name = key_name.decode('utf-8') if key_name is not None else None key_char = key_char.decode('utf-8') if key_char is not None else None - self._key_binding_handlers[binding_name](key_state, key_name, key_char) + self._key_binding_handlers[binding_name](key_state, key_name, key_char, scale, arg) def unregister_key_binding(self, keydef): """Unregister a key binding by keydef.""" From 0c33c933dd05f05d7e11ca199e2faa44e1d583a4 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 25 Apr 2025 11:48:30 +0200 Subject: [PATCH 09/10] Version 1.0.8 --- mpv.py | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mpv.py b/mpv.py index c227b33..1d3f469 100644 --- a/mpv.py +++ b/mpv.py @@ -17,7 +17,7 @@ # # You can find copies of the GPLv2 and LGPLv2.1 licenses in the project repository's LICENSE.GPL and LICENSE.LGPL files. -__version__ = '1.0.7' +__version__ = '1.0.8' from ctypes import * import ctypes.util diff --git a/pyproject.toml b/pyproject.toml index dbea61f..f68661e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ py-modules = ['mpv'] [project] name = "mpv" -version = "v1.0.7" +version = "v1.0.8" description = "A python interface to the mpv media player" readme = "README.rst" authors = [{name = "jaseg", email = "mpv@jaseg.de"}] From b26b72e183bcbcf968dfaa192c819acf38580713 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 25 Apr 2025 11:54:22 +0200 Subject: [PATCH 10/10] Fix typo in MPV.osd_overlay Closes #293 --- mpv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mpv.py b/mpv.py index 1d3f469..f9bf561 100644 --- a/mpv.py +++ b/mpv.py @@ -1529,7 +1529,7 @@ class MPV(object): self.command('overlay_remove', overlay_id) def osd_overlay(self, overlay_id, data, res_x=0, res_y=720, z=0, hidden=False): - self.command('osd_overlay', id=overlay_id, data=data, res_x=res_x, res_y=res_Y, z=z, hidden=hidden, + self.command('osd_overlay', id=overlay_id, data=data, res_x=res_x, res_y=res_y, z=z, hidden=hidden, format='ass-events') def osd_overlay_remove(self, overlay_id):