Skip to content

Commit 268f9da

Browse files
committed
Add Graph.vpush() and Graph.vpull()
1 parent 69cab49 commit 268f9da

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

av/container/input.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class InputContainer(Container):
2121
def close(self) -> None: ...
2222
def demux(self, *args: Any, **kwargs: Any) -> Iterator[Packet]: ...
2323
@overload
24+
def decode(self, video: int) -> Iterator[VideoFrame]: ...
25+
@overload
26+
def decode(self, audio: int) -> Iterator[AudioFrame]: ...
27+
@overload
28+
def decode(self, subtitles: int) -> Iterator[SubtitleSet]: ...
29+
@overload
2430
def decode(self, *args: VideoStream) -> Iterator[VideoFrame]: ...
2531
@overload
2632
def decode(self, *args: AudioStream) -> Iterator[AudioFrame]: ...

av/filter/graph.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ from av.filter.context cimport FilterContext
44

55

66
cdef class Graph:
7-
87
cdef lib.AVFilterGraph *ptr
98

109
cdef readonly bint configured

av/filter/graph.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ class Graph:
4141
) -> FilterContext: ...
4242
def push(self, frame: None | AudioFrame | VideoFrame) -> None: ...
4343
def pull(self) -> VideoFrame | AudioFrame: ...
44+
def vpush(self, frame: VideoFrame | None) -> None: ...
45+
def vpull(self) -> VideoFrame: ...

av/filter/graph.pyx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,15 @@ cdef class Graph:
174174
else:
175175
raise ValueError(f"can only AudioFrame, VideoFrame or None; got {type(frame)}")
176176

177-
if len(contexts) != 1:
178-
raise ValueError(f"can only auto-push with single buffer; found {len(contexts)}")
177+
for ctx in contexts:
178+
ctx.push(frame)
179179

180-
contexts[0].push(frame)
180+
def vpush(self, VideoFrame frame):
181+
for ctx in self._context_by_type.get("buffer", []):
182+
ctx.push(frame)
181183

184+
185+
# TODO: Test complex filter graphs, add `at: int = 0` arg to pull() and vpull().
182186
def pull(self):
183187
vsinks = self._context_by_type.get("buffersink", [])
184188
asinks = self._context_by_type.get("abuffersink", [])
@@ -188,3 +192,11 @@ cdef class Graph:
188192
raise ValueError(f"can only auto-pull with single sink; found {nsinks}")
189193

190194
return (vsinks or asinks)[0].pull()
195+
196+
def vpull(self):
197+
vsinks = self._context_by_type.get("buffersink", [])
198+
nsinks = len(vsinks)
199+
if nsinks != 1:
200+
raise ValueError(f"can only auto-pull with single sink; found {nsinks}")
201+
202+
return vsinks[0].pull()

tests/test_filters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def test_video_buffer(self):
204204

205205
for frame in input_container.decode():
206206
self.assertEqual(frame.time_base, Fraction(1, 30))
207-
graph.push(frame)
207+
graph.vpush(frame)
208208
filtered_frames = pull_until_blocked(graph)
209209

210210
if frame.pts == 0:
@@ -220,7 +220,7 @@ def test_video_buffer(self):
220220
self.assertEqual(filtered_frames[1].pts, (frame.pts - 1) * 2 + 1)
221221
self.assertEqual(filtered_frames[1].time_base, Fraction(1, 60))
222222

223-
def test_EOF(self):
223+
def test_EOF(self) -> None:
224224
input_container = av.open(format="lavfi", file="color=c=pink:duration=1:r=30")
225225
video_stream = input_container.streams.video[0]
226226

@@ -233,12 +233,12 @@ def test_EOF(self):
233233
graph.configure()
234234

235235
for frame in input_container.decode(video=0):
236-
graph.push(frame)
236+
graph.vpush(frame)
237237

238-
graph.push(None)
238+
graph.vpush(None)
239239

240240
# if we do not push None, we get a BlockingIOError
241-
palette_frame = graph.pull()
241+
palette_frame = graph.vpull()
242242

243243
self.assertIsInstance(palette_frame, av.VideoFrame)
244244
self.assertEqual(palette_frame.width, 16)

0 commit comments

Comments
 (0)