Skip to content

Commit 787917b

Browse files
committed
Make parameters same, test channel_last
1 parent 73baa3d commit 787917b

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

av/video/frame.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class VideoFrame(Frame):
6060
def to_rgb(self, **kwargs: Any) -> VideoFrame: ...
6161
def to_image(self, **kwargs: Any) -> Image.Image: ...
6262
def to_ndarray(
63-
self, force_channel_last: bool = False, **kwargs: Any
63+
self, channel_last: bool = False, **kwargs: Any
6464
) -> _SupportedNDarray: ...
6565
@staticmethod
6666
def from_image(img: Image.Image) -> VideoFrame: ...

av/video/frame.pyx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,16 @@ cdef class VideoFrame(Frame):
282282

283283
return Image.frombytes("RGB", (plane.width, plane.height), bytes(o_buf), "raw", "RGB", 0, 1)
284284

285-
def to_ndarray(self, force_channel_last=False, **kwargs):
285+
def to_ndarray(self, channel_last=False, **kwargs):
286286
"""Get a numpy array of this frame.
287287
288288
Any ``**kwargs`` are passed to :meth:`.VideoReformatter.reformat`.
289289
290290
The array returned is generally of dimension (height, width, channels).
291291
292-
:param bool force_channel_last: If False (default), the shape for the yuv444p and yuvj444p
293-
will be (channels, height, width) rather than (height, width, channels) as usual.
294-
This is for backward compatibility and also for keeping that
295-
`bytes(to_ndarray(frame))` should be the same as the ffmpeg cli
296-
when returning the pix_fmt with `-c:v rawvideo`.
292+
:param bool channel_last: If True, the shape of array will be
293+
(height, width, channels) rather than (channels, height, width) for
294+
the "yuv444p" and "yuvj444p" formats.
297295
298296
.. note:: Numpy must be installed.
299297
@@ -374,7 +372,7 @@ cdef class VideoFrame(Frame):
374372
array[:, :, 0] = array[:, :, 2]
375373
array[:, :, 2] = array[:, :, 1]
376374
array[:, :, 1] = buffer
377-
if not force_channel_last and frame.format.name in {"yuv444p", "yuvj444p"}:
375+
if not channel_last and frame.format.name in {"yuv444p", "yuvj444p"}:
378376
array = np.moveaxis(array, 2, 0)
379377
return array
380378

tests/test_videoframe.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ def test_ndarray_yuv444p() -> None:
440440
assert frame.format.name == "yuv444p"
441441
assertNdarraysEqual(frame.to_ndarray(), array)
442442

443+
array = numpy.random.randint(0, 256, size=(3, 480, 640), dtype=numpy.uint8)
444+
frame = VideoFrame.from_ndarray(array, channel_last=False, format="yuv444p")
445+
assert frame.width == 640 and frame.height == 480
446+
assert frame.format.name == "yuv444p"
447+
assertNdarraysEqual(frame.to_ndarray(channel_last=False), array)
448+
assert array.shape != frame.to_ndarray(channel_last=True).shape
449+
assert (
450+
frame.to_ndarray(channel_last=False).shape
451+
!= frame.to_ndarray(channel_last=True).shape
452+
)
453+
443454

444455
def test_ndarray_yuvj444p() -> None:
445456
array = numpy.random.randint(0, 256, size=(3, 480, 640), dtype=numpy.uint8)
@@ -458,7 +469,7 @@ def test_ndarray_yuv444p16() -> None:
458469
assertNdarraysEqual(frame.to_ndarray(), array)
459470

460471

461-
def test_ndarray_yuv444p16_allign() -> None:
472+
def test_ndarray_yuv444p16_align() -> None:
462473
array = numpy.random.randint(0, 65536, size=(238, 318, 3), dtype=numpy.uint16)
463474
for format in ("yuv444p16be", "yuv444p16le"):
464475
frame = VideoFrame.from_ndarray(array, format=format)
@@ -476,7 +487,7 @@ def test_ndarray_yuva444p16() -> None:
476487
assertNdarraysEqual(frame.to_ndarray(), array)
477488

478489

479-
def test_ndarray_yuva444p16_allign() -> None:
490+
def test_ndarray_yuva444p16_align() -> None:
480491
array = numpy.random.randint(0, 65536, size=(238, 318, 4), dtype=numpy.uint16)
481492
for format in ("yuva444p16be", "yuva444p16le"):
482493
frame = VideoFrame.from_ndarray(array, format=format)

0 commit comments

Comments
 (0)