Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

python video

Preetham Pemmasani4cb7a02e1135
diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt
index cea4bb6..3136a83 100644
--- a/bindings/python/CMakeLists.txt
+++ b/bindings/python/CMakeLists.txt
@@ -10,7 +10,7 @@ endif()
message(STATUS "BUI: building buipy Python bindings for Python ${Python3_VERSION}")
-Python3_add_library(buipy MODULE WITH_SOABI src/buipy_module.c src/buipy_kit.c)
+Python3_add_library(buipy MODULE WITH_SOABI src/buipy_module.c src/buipy_kit.c src/buipy_video.c)
target_link_libraries(buipy PRIVATE bui_api bui_gpu bui_kit)
# ($<0:> keeps multi-config generators from adding a Release/ subdirectory.)
@@ -124,6 +124,12 @@ if(BUI_BUILD_TESTS)
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_images.py
)
set_tests_properties(buipy_images PROPERTIES ENVIRONMENT "PYTHONPATH=${BUIPY_TEST_PYTHONPATH}")
+ # video: the component and player, playing, seeking, ending, its controls
+ add_test(
+ NAME buipy_video
+ COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_video.py
+ )
+ set_tests_properties(buipy_video PROPERTIES ENVIRONMENT "PYTHONPATH=${BUIPY_TEST_PYTHONPATH}")
if(TARGET _buiimgui)
add_test(
NAME buipy_imgui
diff --git a/bindings/python/bui/__init__.py b/bindings/python/bui/__init__.py
index f0988a7..1134dbf 100644
--- a/bindings/python/bui/__init__.py
+++ b/bindings/python/bui/__init__.py
@@ -138,6 +138,15 @@ image_stats = buipy.image_stats
set_image_budget = buipy.image_set_budget
image_decode_frame = buipy.image_decode_frame
+# Video: the kit's <video> player (one for every binding). Video(src) plays
+# (muted autoplay) where it is attached and drawn; state is one of the
+# VIDEO_* below; video_backends names the decoders this build has ("mf",
+# "avfoundation", "gstreamer", ...; "" without video). The component is
+# bui.components.Video.
+Video = buipy.Video
+video_backends = buipy.video_backends
+VIDEO_LOADING, VIDEO_PLAYING, VIDEO_PAUSED, VIDEO_ENDED, VIDEO_ERROR = range(5)
+
class AnimatedTexture:
"""A GIF or animated WebP as a BUI texture (a Dear ImGui ``ImTextureID``)
diff --git a/bindings/python/bui/components/__init__.py b/bindings/python/bui/components/__init__.py
index 257fac3..6c23671 100644
--- a/bindings/python/bui/components/__init__.py
+++ b/bindings/python/bui/components/__init__.py
@@ -38,7 +38,7 @@ from bui.components.views import GridView, ListView, TreeItem, TreeView
from bui.components.widgets import (Button, Checkbox, ColorSwatch, Decorator, Dropdown,
EnumRow, HotkeyField, IconButton, Image, Label, NumberField,
NumberSlider, ProgressBar, SearchField, Separator, Spacer,
- TextField, Toggle, eval_number)
+ TextField, Toggle, Video, eval_number)
__all__ = [
"App", "Component", "ValueComponent", "unit",
@@ -46,7 +46,7 @@ __all__ = [
"Column", "Row", "Split", "Grid", "Box", "Panel", "Tabs", "SideTabs", "Sidebar", "ScrollView", "Area", "AreaHeader",
"VectorField",
# widgets
- "Label", "Image", "Separator", "Spacer", "ProgressBar", "Button", "IconButton", "Toggle",
+ "Label", "Image", "Video", "Separator", "Spacer", "ProgressBar", "Button", "IconButton", "Toggle",
"Checkbox", "Decorator", "TextField", "SearchField", "HotkeyField", "NumberField",
"NumberSlider", "Dropdown", "EnumRow", "ColorSwatch", "eval_number",
# views
diff --git a/bindings/python/bui/components/widgets.py b/bindings/python/bui/components/widgets.py
index 60f29b3..683938a 100644
--- a/bindings/python/bui/components/widgets.py
+++ b/bindings/python/bui/components/widgets.py
@@ -93,6 +93,47 @@ class Image(Component):
self.set_image_source(self._src or None, self.FITS[self._fit])
+VIDEO_ENDED = 3 # buipy.Video.state
+
+
+class Video(Component):
+ """A video file (MP4, WebM, MOV, ... whatever the OS decodes: Media
+ Foundation, AVFoundation, GStreamer), fitted like CSS ``object-fit``.
+ Muted autoplay unless ``autoplay=False``; it decodes at the size drawn
+ and only while it shows. ``controls=True`` draws Blender's play/pause
+ button and a scrubber with the time. ``player`` is the
+ :class:`bui.Video` (play, pause, seek, rate, time, duration, on_state)."""
+
+ TYPE = "video"
+ FITS = Image.FITS
+
+ def __init__(self, src: str, *, autoplay: bool = True, loop: bool = False,
+ controls: bool = False, poster: Optional[str] = None, fit: str = "contain",
+ on_ended: Optional[Callable[[], None]] = None, **kw):
+ super().__init__(**kw)
+ if fit not in self.FITS:
+ raise ValueError(f"fit must be one of {sorted(self.FITS)}, not {fit!r}")
+ self.player = buipy.Video(str(src)) # kept by the node: it borrows the player
+ self.player.loop = loop
+ self.player.controls = controls
+ if poster:
+ self.player.poster = str(poster)
+ if not autoplay:
+ self.player.pause()
+ if on_ended is not None:
+ self.player.on_state = lambda s: on_ended() if s == VIDEO_ENDED else None
+ self.player.attach(self, self.FITS[fit])
+
+ def play(self) -> None:
+ self.player.play()
+
+ def pause(self) -> None:
+ self.player.pause()
+
+ def seek(self, seconds: float) -> None:
+ self.player.seek(seconds)
+
+
class Separator(Component):
"""Vertical space (``factor`` widget units), optionally with a line."""
diff --git a/bindings/python/src/buipy_kit.c b/bindings/python/src/buipy_kit.c
index cce1b48..b1d7856 100644
--- a/bindings/python/src/buipy_kit.c
+++ b/bindings/python/src/buipy_kit.c
@@ -123,6 +123,16 @@ static int kitnode_init(KitNodeObject *self, PyObject *args, PyObject *kw)
return 0;
}
+/* The node of a KitNode (buipy_video.c attaches videos); NULL with an error. */
+BKNode *buipy_kit_node(PyObject *obj)
+{
+ if (!obj || !PyObject_TypeCheck(obj, &KitNode_Type) || !((KitNodeObject *)obj)->node) {
+ PyErr_SetString(PyExc_TypeError, "expected a live KitNode");
+ return NULL;
+ }
+ return ((KitNodeObject *)obj)->node;
+}
+
static void kitnode_dealloc(KitNodeObject *self)
{
PyObject_GC_UnTrack(self);
diff --git a/bindings/python/src/buipy_module.c b/bindings/python/src/buipy_module.c
index 66fd247..19aa213 100644
--- a/bindings/python/src/buipy_module.c
+++ b/bindings/python/src/buipy_module.c
@@ -1300,7 +1300,8 @@ BuiRaster *buipy_raster_of(PyObject *obj)
return NULL;
}
-int buipy_kit_init(PyObject *module); /* buipy_kit.c */
+int buipy_kit_init(PyObject *module); /* buipy_kit.c */
+int buipy_video_init(PyObject *module); /* buipy_video.c */
PyMODINIT_FUNC PyInit_buipy(void)
{
@@ -1312,7 +1313,7 @@ PyMODINIT_FUNC PyInit_buipy(void)
return NULL;
}
if (PyModule_AddObjectRef(m, "Raster", (PyObject *)&BuiPyRaster_Type) < 0 ||
- buipy_kit_init(m) < 0)
+ buipy_kit_init(m) < 0 || buipy_video_init(m) < 0)
{
Py_DECREF(m);
return NULL;
diff --git a/bindings/python/src/buipy_video.c b/bindings/python/src/buipy_video.c
new file mode 100644
index 0000000..13e5930
--- /dev/null
+++ b/bindings/python/src/buipy_video.c
@@ -0,0 +1,265 @@
+/* buipy.Video: the kit's <video> player (BK_video_*, kit/intern/image/video.c),
+ * the same one every binding uses. UI thread: the state listener runs inside
+ * the window loop's calls (timers, image polls), with the GIL held.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later */
+
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+
+#include "BUI_kit.h"
+
+BKNode *buipy_kit_node(PyObject *obj); /* buipy_kit.c */
+
+typedef struct VideoObject {
+ PyObject_HEAD
+ BKVideo *v;
+ PyObject *on_state; /* callable(state: int) or NULL */
+} VideoObject;
+
+static void heard(BKVideo *v, int state, void *data)
+{
+ (void)v;
+ VideoObject *self = (VideoObject *)data;
+ const PyGILState_STATE gil = PyGILState_Ensure(); /* window loops may run without it */
+ if (self->on_state) {
+ PyObject *r = PyObject_CallFunction(self->on_state, "i", state);
+ if (!r) {
+ PyErr_WriteUnraisable(self->on_state);
+ }
+ Py_XDECREF(r);
+ }
+ PyGILState_Release(gil);
+}
+
+static int video_init(VideoObject *self, PyObject *args, PyObject *kw)
+{
+ static char *kwlist[] = {"src", NULL};
+ const char *src;
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "s:Video", kwlist, &src)) {
+ return -1;
+ }
+ if (self->v) {
+ PyErr_SetString(PyExc_RuntimeError, "Video already initialized");
+ return -1;
+ }
+ self->v = BK_video_new(src);
+ if (!self->v) {
+ PyErr_SetString(PyExc_ValueError, "an empty video path");
+ return -1;
+ }
+ BK_video_set_listener(self->v, heard, self);
+ return 0;
+}
+
+static void video_dealloc(VideoObject *self)
+{
+ PyObject_GC_UnTrack(self);
+ if (self->v) {
+ BK_video_set_listener(self->v, NULL, NULL);
+ BK_video_free(self->v);
+ }
+ Py_CLEAR(self->on_state);
+ Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
+static int video_traverse(VideoObject *self, visitproc visit, void *arg)
+{
+ Py_VISIT(self->on_state);
+ return 0;
+}
+
+static int video_clear(VideoObject *self)
+{
+ Py_CLEAR(self->on_state);
+ return 0;
+}
+
+#define LIVE(self) \
+ if (!(self)->v) { \
+ PyErr_SetString(PyExc_RuntimeError, "Video not initialized"); \
+ return NULL; \
+ }
+
+static PyObject *video_play(VideoObject *self, PyObject *unused)
+{
+ (void)unused;
+ LIVE(self);
+ BK_video_play(self->v);
+ Py_RETURN_NONE;
+}
+
+static PyObject *video_pause(VideoObject *self, PyObject *unused)
+{
+ (void)unused;
+ LIVE(self);
+ BK_video_pause(self->v);
+ Py_RETURN_NONE;
+}
+
+static PyObject *video_seek(VideoObject *self, PyObject *arg)
+{
+ LIVE(self);
+ const double t = PyFloat_AsDouble(arg);
+ if (t == -1.0 && PyErr_Occurred()) {
+ return NULL;
+ }
+ BK_video_seek(self->v, t);
+ Py_RETURN_NONE;
+}
+
+static PyObject *video_texture(VideoObject *self, PyObject *args)
+{
+ LIVE(self);
+ int tex = 0, max_w = 0, max_h = 0;
+ if (!PyArg_ParseTuple(args, "|iii:texture", &tex, &max_w, &max_h)) {
+ return NULL;
+ }
+ return PyLong_FromLong(BK_video_texture(self->v, tex, max_w, max_h));
+}
+
+static PyObject *video_attach(VideoObject *self, PyObject *args)
+{
+ LIVE(self);
+ PyObject *node;
+ int fit = BK_FIT_CONTAIN;
+ if (!PyArg_ParseTuple(args, "O|i:attach", &node, &fit)) {
+ return NULL;
+ }
+ BKNode *n = buipy_kit_node(node);
+ if (!n) {
+ return NULL;
+ }
+ BK_node_set_video(n, self->v, fit);
+ Py_RETURN_NONE;
+}
+
+static PyMethodDef video_methods[] = {
+ {"play", (PyCFunction)video_play, METH_NOARGS, "Play (an ended video starts over)."},
+ {"pause", (PyCFunction)video_pause, METH_NOARGS, "Pause."},
+ {"seek", (PyCFunction)video_seek, METH_O, "seek(seconds): that frame shows, paused or not."},
+ {"texture", (PyCFunction)video_texture, METH_VARARGS,
+ "texture(tex=0, max_w=0, max_h=0) -> texture id (Dear ImGui): the current frame, updated in "
+ "place; calling it each frame keeps the video playing."},
+ {"attach", (PyCFunction)video_attach, METH_VARARGS,
+ "attach(node, fit=BK_FIT_CONTAIN): show it on a \"video\" or \"image\" node (the node "
+ "borrows it: keep the Video alive while the node shows it)."},
+ {NULL, NULL, 0, NULL},
+};
+
+/* -- properties ---------------------------------------------------------------------- */
+
+static PyObject *get_state(VideoObject *self, void *c)
+{
+ (void)c;
+ LIVE(self);
+ return PyLong_FromLong(BK_video_state(self->v));
+}
+
+static PyObject *get_time(VideoObject *self, void *c)
+{
+ (void)c;
+ LIVE(self);
+ return PyFloat_FromDouble(BK_video_time(self->v));
+}
+
+static int set_time(VideoObject *self, PyObject *value, void *c)
+{
+ (void)c;
+ const double t = value ? PyFloat_AsDouble(value) : -1.0;
+ if (!self->v || (t == -1.0 && PyErr_Occurred())) {
+ return -1;
+ }
+ BK_video_seek(self->v, t);
+ return 0;
+}
+
+static PyObject *get_duration(VideoObject *self, void *c)
+{
+ (void)c;
+ LIVE(self);
+ return PyFloat_FromDouble(BK_video_duration(self->v));
+}
+
+static PyObject *get_size(VideoObject *self, void *c)
+{
+ (void)c;
+ LIVE(self);
+ int w, h;
+ if (!BK_video_size(self->v, &w, &h)) {
+ Py_RETURN_NONE;
+ }
+ return Py_BuildValue("(ii)", w, h);
+}
+
+/* write-only settings: the kit keeps them */
+#define SETTER(name, call) \
+ static int set_##name(VideoObject *self, PyObject *value, void *c) \
+ { \
+ (void)c; \
+ if (!self->v || !value) { \
+ PyErr_SetString(PyExc_AttributeError, "can't delete or set on an uninitialized Video"); \
+ return -1; \
+ } \
+ call; \
+ return PyErr_Occurred() ? -1 : 0; \
+ }
+SETTER(rate, BK_video_set_rate(self->v, PyFloat_AsDouble(value)))
+SETTER(loop, BK_video_set_loop(self->v, PyObject_IsTrue(value)))
+SETTER(controls, BK_video_set_controls(self->v, PyObject_IsTrue(value)))
+SETTER(poster, BK_video_set_poster(self->v, value == Py_None ? NULL : PyUnicode_AsUTF8(value)))
+
+static PyObject *get_on_state(VideoObject *self, void *c)
+{
+ (void)c;
+ return Py_NewRef(self->on_state ? self->on_state : Py_None);
+}
+
+static int set_on_state(VideoObject *self, PyObject *value, void *c)
+{
+ (void)c;
+ if (value && value != Py_None && !PyCallable_Check(value)) {
+ PyErr_SetString(PyExc_TypeError, "on_state must be callable or None");
+ return -1;
+ }
+ Py_XSETREF(self->on_state, value && value != Py_None ? Py_NewRef(value) : NULL);
+ return 0;
+}
+
+static PyGetSetDef video_getset[] = {
+ {"state", (getter)get_state, NULL, "LOADING 0, PLAYING 1, PAUSED 2, ENDED 3, ERROR 4", NULL},
+ {"time", (getter)get_time, (setter)set_time, "Seconds (setting it seeks).", NULL},
+ {"duration", (getter)get_duration, NULL, "Seconds; 0 until loaded.", NULL},
+ {"size", (getter)get_size, NULL, "(w, h) once loaded, else None.", NULL},
+ {"rate", NULL, (setter)set_rate, "Playback speed (write-only), 1/16 .. 16.", NULL},
+ {"loop", NULL, (setter)set_loop, "Loop (write-only).", NULL},
+ {"controls", NULL, (setter)set_controls, "Blender-style controls on video nodes.", NULL},
+ {"poster", NULL, (setter)set_poster, "An image file shown until the first frame.", NULL},
+ {"on_state", (getter)get_on_state, (setter)set_on_state, "callable(state) on changes.", NULL},
+ {NULL, NULL, NULL, NULL, NULL},
+};
+
+static PyTypeObject Video_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0).tp_name = "buipy.Video",
+ .tp_basicsize = sizeof(VideoObject),
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ .tp_doc = "Video(src): a <video> player (muted autoplay; loads when first drawn).",
+ .tp_new = PyType_GenericNew,
+ .tp_init = (initproc)video_init,
+ .tp_dealloc = (destructor)video_dealloc,
+ .tp_traverse = (traverseproc)video_traverse,
+ .tp_clear = (inquiry)video_clear,
+ .tp_methods = video_methods,
+ .tp_getset = video_getset,
+};
+
+int buipy_video_init(PyObject *module)
+{
+ if (PyType_Ready(&Video_Type) < 0 ||
+ PyModule_AddObjectRef(module, "Video", (PyObject *)&Video_Type) < 0 ||
+ PyModule_AddStringConstant(module, "video_backends", BK_video_backends()) < 0)
+ {
+ return -1;
+ }
+ return 0;
+}
diff --git a/bindings/python/tests/test_video.py b/bindings/python/tests/test_video.py
new file mode 100644
index 0000000..19a9463
--- /dev/null
+++ b/bindings/python/tests/test_video.py
@@ -0,0 +1,93 @@
+"""Video from Python: the bui.components.Video component and the buipy.Video
+player (play, pause, seek, rate, loop, state events), drawn by a context
+like a window loop would. Skipped where no backend decodes the H.264 clip.
+
+SPDX-License-Identifier: GPL-2.0-or-later
+"""
+
+import os
+import sys
+import time
+
+sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
+
+import bui # noqa: E402
+import buipy # noqa: E402
+
+CLIP = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..", "tests", "videos",
+ "clip.mp4")
+
+
+class Loop:
+ """A context driven like a window: its one timer, image results, renders."""
+
+ def __init__(self, node, w=200, h=120):
+ self.ctx = buipy.KitContext(node)
+ self.due = None
+ self.ctx.set_host(schedule=self.schedule)
+ self.r = buipy.Raster(w, h)
+ self.w, self.h = w, h
+ self.ctx.render(w, h, self.r, True)
+
+ def schedule(self, ms, timer):
+ self.due = (time.perf_counter() + ms / 1000.0, timer)
+
+ def run(self, seconds, until=None):
+ end = time.perf_counter() + seconds
+ while time.perf_counter() < end and not (until and until()):
+ if self.due and time.perf_counter() >= self.due[0]:
+ timer, self.due = self.due[1], None
+ self.ctx.timer(timer)
+ buipy.image_poll()
+ if self.ctx.needs_render():
+ self.ctx.render(self.w, self.h, self.r, False)
+ time.sleep(0.001)
+
+
+def test_video():
+ if not bui.video_backends:
+ print("no video backend in this build: skipped")
+ return
+ from bui.components import Video
+ states = []
+ v = Video(CLIP, controls=True, loop=False)
+ v.player.on_state = states.append
+ loop = Loop(v)
+ loop.run(5.0, lambda: v.player.state != bui.VIDEO_LOADING)
+ if v.player.state == bui.VIDEO_ERROR:
+ print(f"{bui.video_backends} doesn't decode H.264 here: skipped")
+ return
+ assert v.player.state == bui.VIDEO_PLAYING, v.player.state
+ assert v.player.size == (96, 54) and abs(v.player.duration - 2.0) < 0.15
+ loop.run(0.5)
+ assert v.player.time > 0.2, v.player.time
+ v.pause()
+ v.seek(1.5)
+ loop.run(0.4)
+ assert abs(v.player.time - 1.5) < 0.11 and v.player.state == bui.VIDEO_PAUSED
+ v.player.rate = 4.0
+ v.play()
+ loop.run(2.0, lambda: v.player.state == bui.VIDEO_ENDED)
+ assert v.player.state == bui.VIDEO_ENDED and bui.VIDEO_ENDED in states, states
+ # the controls: a click on the play button plays it again
+ x, y, w, h = v.rect
+ loop.ctx.press(x + 8, y + h - 8, 0)
+ loop.ctx.release(x + 8, y + h - 8, 0)
+ loop.run(0.2)
+ assert v.player.state == bui.VIDEO_PLAYING, v.player.state
+ try:
+ Video(CLIP, fit="stretch")
+ except ValueError:
+ pass
+ else:
+ raise AssertionError("bad fit must raise")
+
+
+def main():
+ test_video()
+ print("video tests passed")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/kit/intern/image/play.c b/kit/intern/image/play.c
index d8ab3cb..e6716ba 100644
--- a/kit/intern/image/play.c
+++ b/kit/intern/image/play.c
@@ -199,9 +199,9 @@ void imb_play_pause(BKImPlay *p, bool paused)
{
if (p->paused && !paused) {
p->due = 0.0; /* resumed: the frame gets its time again */
- if (p->finished) { /* playing an ended video starts it over */
- imb_play_seek(p, 0.0);
- }
+ }
+ if (!paused && p->finished) { /* playing an ended video starts it over */
+ imb_play_seek(p, 0.0);
}
p->paused = paused;
}