Skip to content

Commit 7de657a

Browse files
Add process_command
This allows dynamic control of filter parameters without stopping and restarting the filtering process. Wraps avfilter_process_command --------- Co-authored-by: WyattBlue <wyattblue@auto-editor.com>
1 parent e3f2efd commit 7de657a

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

av/filter/context.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ class FilterContext:
1212
def graph(self) -> Graph: ...
1313
def push(self, frame: Frame) -> None: ...
1414
def pull(self) -> Frame: ...
15+
def process_command(
16+
self, cmd: str, arg: str | None = None, res_len: int = 1024, flags: int = 0
17+
) -> str | None: ...

av/filter/context.pyx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cimport libav as lib
2+
13
import weakref
24

35
from av.audio.frame cimport alloc_audio_frame
@@ -132,3 +134,41 @@ cdef class FilterContext:
132134
frame._init_user_attributes()
133135
frame.time_base = avrational_to_fraction(&self.ptr.inputs[0].time_base)
134136
return frame
137+
138+
def process_command(self, cmd, arg=None, int res_len=1024, int flags=0):
139+
if not cmd:
140+
raise ValueError("Invalid cmd")
141+
142+
cdef char *c_cmd = NULL
143+
cdef char *c_arg = NULL
144+
145+
c_cmd = cmd
146+
if arg is not None:
147+
c_arg = arg
148+
149+
cdef char *c_res = NULL
150+
cdef int ret
151+
cdef bytearray res_buf = None
152+
cdef unsigned char[:] view
153+
cdef bytes b
154+
cdef int nul
155+
156+
if res_len > 0:
157+
res_buf = bytearray(res_len)
158+
view = res_buf
159+
c_res = <char*>&view[0]
160+
else:
161+
c_res = NULL
162+
163+
with nogil:
164+
ret = lib.avfilter_process_command(self.ptr, c_cmd, c_arg, c_res, res_len, flags)
165+
err_check(ret)
166+
167+
if res_buf is not None:
168+
b = bytes(res_buf)
169+
nul = b.find(b'\x00')
170+
if nul >= 0:
171+
b = b[:nul]
172+
if b:
173+
return b.decode("utf-8", "strict")
174+
return None

include/libavfilter/avfilter.pxd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ cdef extern from "libavfilter/avfilter.h" nogil:
6262
# custom
6363
cdef set pyav_get_available_filters()
6464

65+
int avfilter_process_command(AVFilterContext *filter,
66+
const char *cmd,
67+
const char *arg,
68+
char *res,
69+
int res_len,
70+
int flags)
71+
72+
cdef int AVFILTER_CMD_FLAG_FAST
73+
6574

6675
cdef extern from "libavfilter/buffersink.h" nogil:
6776
cdef void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)

0 commit comments

Comments
 (0)