Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 7f0f488

Browse files
committed
Added squeeze arg to bytes_to_xxx
1 parent 94546f0 commit 7f0f488

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/ffmpegio_plugin_numpy/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def audio_bytes(obj: ArrayLike) -> memoryview:
6969

7070

7171
@hookimpl
72-
def bytes_to_video(b: bytes, dtype: str, shape: Tuple[int, int, int]) -> ArrayLike:
72+
def bytes_to_video(b: bytes, dtype: str, shape: Tuple[int, int, int], squeeze: bool) -> ArrayLike:
7373
"""convert bytes to rawvideo NumPy array
7474
7575
:param b: byte data of arbitrary number of video frames
@@ -78,15 +78,18 @@ def bytes_to_video(b: bytes, dtype: str, shape: Tuple[int, int, int]) -> ArrayLi
7878
:type dtype: str
7979
:param size: frame dimension in pixels and number of color components (height, width, components)
8080
:type size: Tuple[int, int, int]
81+
:param squeeze: True to remove all the singular dimensions
82+
:type squeeze: bool
8183
:return: rawvideo frames
8284
:rtype: ArrayLike
8385
"""
8486

85-
return np.frombuffer(b, dtype).reshape(-1, *shape)
87+
x = np.frombuffer(b, dtype).reshape(-1, *shape)
88+
return x.squeeze() if squeeze else x
8689

8790

8891
@hookimpl
89-
def bytes_to_audio(b: bytes, dtype: str, shape: Tuple[int]) -> ArrayLike:
92+
def bytes_to_audio(b: bytes, dtype: str, shape: Tuple[int], squeeze: bool) -> ArrayLike:
9093
"""convert bytes to rawaudio NumPy array
9194
9295
:param b: byte data of arbitrary number of video frames
@@ -95,8 +98,11 @@ def bytes_to_audio(b: bytes, dtype: str, shape: Tuple[int]) -> ArrayLike:
9598
:type dtype: str
9699
:param shape: number of audio channels
97100
:type shape: Tuple[int]
101+
:param squeeze: True to remove all the singular dimensions
102+
:type squeeze: bool
98103
:return: raw audio samples
99104
:rtype: ArrayLike
100105
"""
101106

102-
return np.frombuffer(b, dtype).reshape(-1, *shape)
107+
x = np.frombuffer(b, dtype).reshape(-1, *shape)
108+
return x.squeeze() if squeeze else x

tests/test_base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ def test_hooks():
1111
dtype = "|u1"
1212
shape = (2, 2, 3)
1313
b = b"\0" * utils.prod(shape)
14-
data = hook.bytes_to_video(b=b, dtype=dtype, shape=shape)
14+
data = hook.bytes_to_video(b=b, dtype=dtype, shape=shape, squeeze=False)
1515
assert memoryview(data).cast("b") == b
1616
assert data.dtype.str == dtype
1717
assert data.shape[1:] == shape
1818
assert hook.video_info(obj=data) == (shape, dtype)
1919
assert hook.video_bytes(obj=data).cast("b") == b
2020

21+
data = hook.bytes_to_video(b=b, dtype=dtype, shape=shape, squeeze=True)
22+
assert data.shape == shape
23+
2124
dtype = "<f4"
2225
shape = (2,)
2326
b = b"\0" * (1024 * utils.prod(shape))
24-
data = hook.bytes_to_audio(b=b, dtype=dtype, shape=shape)
27+
data = hook.bytes_to_audio(b=b, dtype=dtype, shape=shape, squeeze=False)
2528
assert memoryview(data).cast("b") == b
2629
assert data.dtype.str == dtype
2730
assert data.shape[1:] == shape

0 commit comments

Comments
 (0)