Python Scripting Support [RFC]#11574
Conversation
|
i have some concerns regarding add python Scripting Support just like JavaScript look like i need to do more research |
|
Would the per interpreter-GIL in the upcoming python version allow for the elimination of a lot of the python-specific logic that's in this current implementation and let it behave more like lua/js do? |
|
@Dudemanguy I hope so. |
|
To an end user It shouldn't seem anything different to any other scripts (js/lua) although the underlying implementation is different. |
|
@Dudemanguy , FYI, this version of it will work with older python nicely maybe down to python3.3. |
|
Well if python 3.12 makes it possible to solve some of the technical challenges with embedding python in mpv, I'd think it'd be better to make that the minimum version. That's not to say that the current implementation is bad, but it'd definitely be better if python could just be yet another scripting backend. Plus it's only about a month until python 3.12 releases. |
|
Python 3.12 is 6 months away. I don't think that waiting for Python 3.12 should block this PR. |
|
Oh my mistake. I thought it was next month for some reason. |
|
There are some existing issues with lower level threads (threads created from the scope outside of python). On such a thread python loses its I/O wrappers (and god knows what else). These problems will still exist in 3.12. But the isolation proposed in pep-0684 can be done from the python context. In my opinion and in performance-wise, there'd be hardly any difference between the two (that is if the former approach was doable). |
|
This sounds very interesting! I imagine this could pave the way for a variety of scripts, including ones to easily modify the UI/UX, like context menus, and make mpv scripting more accessible to casual users |
|
@versedwildcat , it's still a work in progress. We have just developed a working solution to some problems that we had with embedding python on mpv. We are still a step away from getting you this feature. mpv client API hooks still need to be mapped to python functions. I am working on some part of it as I am replying to you. Thank you for your patience. |
|
I've converted this PR to a draft for now until it's ready for review-for-merge. |
|
Hello guys, Long time! |
|
@DeadSix27 , the issues you've mentioned should go away now. |
Github said this pr has conflicts with master branch. |
|
@zhongfly , There you have it. |
Same error's still.. any idea? EDIT: Sometimes I encounter this one: I'm using your example script, it definitely has the import. |
|
@8lurry from mpvclient import mpv
mpv.info("Python script work!")mpv crash after script run. gdb: Build file can be download here: https://github.com/zhongfly/mpv-winbuild/actions/runs/5745111914 |
^ This may come if your client (testfile.py) is not valid. (i.e. testfile.py has SyntaxError or something)
^ It won't come if you have "from mpvclient import mpv" on it. |
|
@8lurry syntax is 100% correct and the import is also correct. EDIT: Shouldn't mpv notify if the syntax is wrong including traceback? Also same error as zhong: |
|
@DeadSix27 , Could you try it again with doing the following changes?
And afterwards let me know if the issue persists. |
I have tried this,but same issue.This fix not work. |
|
@zhongfly , @DeadSix27 , Would you get me the complete log when you run it (please be sure to set the log level to 'debug'). |
The logs have little relevant information,I have set If I tried to run from mpvclient import mpv
mpv.info("Python script start!") # mpv crash after here
mpv.info("Python script end!") |
@zhongfly, compile it on a windows system instead. |
- update documentation - update event handler signature - optimize print_parse_error - add function command_node_async - add function command_node_async_callback - add function abort_async_command - optimize run_event_loop - optimize observe_property
- debug mpv.command - add class Ass - add class OSDOverlay - add function mpv.create_osd_overlay - add function mpv.ass_new - add function mpv.add_periodic_timer - add function mpv.disable_timer
- lint - better error message through check_error when mpv api(s) return with an error - add function hook_add - add function hook_continue - add event handler for MPV_EVENT_HOOK
Co-authored-by: Kacper Michajłow <kasper93@gmail.com>
|
@zhongfly, I did a rebase, can you try now? |
I think these problems will go away for good in python3.14 or actually on free threaded python. Which I will try next in the coming months when Ununtu-26.04LTS is released. |

Dear mpv community,
I and @8lurry are excited to introduce Python scripting support for mpv through python-embed. With this PR merged, you can use Python for scripting in mpv just how you can use Lua/JS for scripting currently. And I am sure that Python will bring numerous benefits to mpv as a scripting language like:
Some People who script mpv are using Lua to act as bridge between mpv and Python. And with Python scripting, this will stop.
We have solved Python-embed's threading issue currently thanks to @8lurry. And when we get per interpreter-GIL in Python 3.12 (https://peps.python.org/pep-0684/), it can be improved even further making the implementation analogous to Lua/Mujs implementations.
With this PR, you can run Python scripts in mpv but we still need to map many mpv functions to Python (We will be updating them in upcoming commits in PR). This PR acts as POC for time being.
We are seeking Feedback on this PR and hope to get this merged within mpv.
CC: @avih , @sfan5 , @Dudemanguy