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

Commit 939b2e3

Browse files
committed
Allow 1d audio data and 2d video data
1 parent 9e5b032 commit 939b2e3

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/ffmpegio_plugin_numpy/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def video_info(obj: ArrayLike) -> Tuple[Tuple[int, int, int], str]:
2929
:return: shape (height,width,components) and data type str
3030
:rtype: Tuple[Tuple[int, int, int], str]
3131
"""
32-
return obj.shape[-3:], obj.dtype.str
32+
return obj.shape[-3:] if obj.ndim != 2 else [*obj.shape, 1], obj.dtype.str
3333

3434

3535
@hookimpl
@@ -41,7 +41,7 @@ def audio_info(obj: ArrayLike) -> Tuple[int, str]:
4141
:return: number of channels and sample data type in data type str
4242
:rtype: Tuple[Tuple[int], str]
4343
"""
44-
return obj.shape[-1:], obj.dtype.str
44+
return obj.shape[-1:] if obj.ndim > 1 else [1], obj.dtype.str
4545

4646

4747
@hookimpl
@@ -71,7 +71,9 @@ def audio_bytes(obj: ArrayLike) -> memoryview:
7171

7272

7373
@hookimpl
74-
def bytes_to_video(b: bytes, dtype: str, shape: Tuple[int, int, int], squeeze: bool) -> ArrayLike:
74+
def bytes_to_video(
75+
b: bytes, dtype: str, shape: Tuple[int, int, int], squeeze: bool
76+
) -> ArrayLike:
7577
"""convert bytes to rawvideo NumPy array
7678
7779
:param b: byte data of arbitrary number of video frames

tests/test_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_hooks():
2121

2222
data = hook.bytes_to_video(b=b, dtype=dtype, shape=shape, squeeze=True)
2323
assert data.shape == shape
24-
24+
2525
dtype = "<f4"
2626
shape = (2,)
2727
b = b"\0" * (1024 * utils.prod(shape))
@@ -46,8 +46,10 @@ def test_audio():
4646
pprint(probe.full_details(wavfile))
4747
fs1, x1 = audio.read(wavfile)
4848

49+
audio.write(wavfile, fs, x1[:, 0], show_log=True, overwrite=True)
50+
4951
assert fs == fs1
50-
assert x.shape==x1.shape
52+
assert x.shape == x1.shape
5153
assert np.array_equal(x, x1)
5254

5355

@@ -61,6 +63,9 @@ def test_video():
6163
video.write(avifile, fs, F, pix_fmt="bgr24", vcodec="rawvideo", show_log=True)
6264
fs1, F1 = video.read(avifile)
6365

66+
# test 2-D input
67+
video.write(avifile, fs, np.array(F[0, :, :, 0]), show_log=True, overwrite=True)
68+
6469
assert fs == fs1
6570
assert np.array_equal(F, F1)
6671

0 commit comments

Comments
 (0)