|
2 | 2 | # vim: ts=4 sw=4 et |
3 | 3 | # |
4 | 4 | # Python MPV library module |
5 | | -# Copyright (C) 2017-2022 Sebastian Götte <[email protected]> |
| 5 | +# Copyright (C) 2017-2024 Sebastian Götte <[email protected]> |
6 | 6 | # |
7 | 7 | # python-mpv inherits the underlying libmpv's license, which can be either GPLv2 or later (default) or LGPLv2.1 or |
8 | 8 | # later. For details, see the mpv copyright page here: https://github.com/mpv-player/mpv/blob/master/Copyright |
|
24 | 24 | import threading |
25 | 25 | import queue |
26 | 26 | import os |
| 27 | +import os.path |
27 | 28 | import sys |
28 | 29 | from warnings import warn |
29 | 30 | from functools import partial, wraps |
|
35 | 36 |
|
36 | 37 | if os.name == 'nt': |
37 | 38 | # Note: mpv-2.dll with API version 2 corresponds to mpv v0.35.0. Most things should work with the fallback, too. |
38 | | - dll = ctypes.util.find_library('mpv-2.dll') or ctypes.util.find_library('libmpv-2.dll') or ctypes.util.find_library('mpv-1.dll') |
39 | | - if dll is None: |
40 | | - 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"].') |
41 | | - # flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
42 | | - # cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa |
| 39 | + names = ['mpv-2.dll', 'libmpv-2.dll', 'mpv-1.dll'] |
| 40 | + for name in names: |
| 41 | + dll = ctypes.util.find_library(name) |
| 42 | + if dll: |
| 43 | + break |
| 44 | + else: |
| 45 | + for name in names: |
| 46 | + dll = os.path.join(os.path.dirname(__file__), name) |
| 47 | + if os.path.isfile(dll): |
| 48 | + break |
| 49 | + else: |
| 50 | + 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"].') |
| 51 | + |
43 | 52 | try: |
| 53 | + # flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
| 54 | + # cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa |
44 | 55 | backend = CDLL(dll, 0x00001000 | 0x00000100) |
45 | 56 | except Exception as e: |
46 | | - if not os.path.isabs(dll): |
| 57 | + if not os.path.isabs(dll): # can only be find_library, not the "look next to mpv.py" thing |
47 | 58 | 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 |
48 | 59 | else: |
49 | 60 | raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it.') from e |
50 | 61 | fs_enc = 'utf-8' |
| 62 | + |
51 | 63 | else: |
52 | 64 | import locale |
53 | 65 | lc, enc = locale.getlocale(locale.LC_NUMERIC) |
|
0 commit comments