Skip to content

Commit 7dddc27

Browse files
committed
plugins - fixed data blob dimension handlings
1 parent 2d6007a commit 7dddc27

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

src/ffmpegio/plugins/rawdata_bytes.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,21 @@ def video_info(obj: BytesRawDataBlob) -> Tuple[ShapeTuple, DTypeString]:
4545
"""
4646

4747
try:
48-
return obj["shape"][-3:], obj["dtype"]
48+
shape = obj["shape"]
49+
dtype = obj["dtype"]
4950
except:
5051
return None
5152

53+
ndim = len(shape)
54+
if ndim == 2:
55+
shape = (*shape, 1)
56+
elif ndim == 3 and shape[-1] > 4:
57+
shape = (*shape[1:], 1)
58+
else:
59+
shape = shape[-3:]
60+
61+
return shape, dtype
62+
5263

5364
@hookimpl
5465
def audio_info(obj: BytesRawDataBlob) -> Tuple[ShapeTuple, DTypeString]:
@@ -103,10 +114,19 @@ def video_frames(obj: BytesRawDataBlob) -> int:
103114
"""
104115

105116
try:
106-
return obj["shape"][0]
107-
except:
117+
shape = obj["shape"]
118+
except KeyError:
108119
return None
109120

121+
ndim = len(shape)
122+
if ndim > 3:
123+
return shape[0]
124+
elif ndim < 3:
125+
return 1
126+
else:
127+
# ndim==3, single frame if the last dim is likely number of components (1-4)
128+
return shape[0] if shape[-1] > 4 else 1
129+
110130

111131
@hookimpl
112132
def audio_samples(obj: BytesRawDataBlob) -> int:
@@ -119,9 +139,11 @@ def audio_samples(obj: BytesRawDataBlob) -> int:
119139
"""
120140

121141
try:
122-
return obj["shape"][0]
123-
except:
142+
shape = obj["shape"]
143+
except KeyError:
124144
return None
145+
else:
146+
return shape[0]
125147

126148

127149
@hookimpl

src/ffmpegio/plugins/rawdata_numpy.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ def video_info(obj: ArrayLike) -> tuple[ShapeTuple, DTypeString]:
3131
:return dtype: data type in numpy dtype str expression
3232
"""
3333
try:
34-
return obj.shape[-3:] if obj.ndim != 2 else [*obj.shape, 1], obj.dtype.str
34+
a = np.asarray(obj)
35+
if a.ndim == 2:
36+
shape = (*a.shape, 1)
37+
elif a.ndim == 3 and a.shape[-1] > 4:
38+
shape = (*a.shape[1:], 1)
39+
else:
40+
shape = a.shape[-3:]
41+
return shape, a.dtype.str
3542
except:
3643
return None
3744

@@ -45,7 +52,8 @@ def audio_info(obj: ArrayLike) -> tuple[ShapeTuple, DTypeString]:
4552
:return dtype: sample data type in numpy dtype str expression
4653
"""
4754
try:
48-
return obj.shape[-1:] if obj.ndim > 1 else [1], obj.dtype.str
55+
a = np.asarray(obj)
56+
return a.shape[-1:] if a.ndim > 1 else [1], a.dtype.str
4957
except:
5058
return None
5159

@@ -60,7 +68,15 @@ def video_frames(obj: ArrayLike) -> int:
6068
"""
6169

6270
try:
63-
return obj.shape[0]
71+
a = np.asarray(obj)
72+
shape = a.shape
73+
ndim = a.ndim
74+
if ndim > 3:
75+
return shape[0]
76+
elif ndim < 3:
77+
return 1
78+
else:
79+
return shape[0] if shape[-1] > 4 else 1
6480
except:
6581
return None
6682

@@ -76,7 +92,7 @@ def audio_samples(obj: ArrayLike) -> int:
7692
"""
7793

7894
try:
79-
return obj.shape[0]
95+
return np.asarray(obj).shape[0]
8096
except:
8197
return None
8298

@@ -90,7 +106,7 @@ def video_bytes(obj: ArrayLike) -> memoryview:
90106
"""
91107

92108
try:
93-
return np.ascontiguousarray(obj).view("b")
109+
return np.ascontiguousarray(obj).reshape(-1).view("b")
94110
except:
95111
return None
96112

@@ -104,7 +120,7 @@ def audio_bytes(obj: ArrayLike) -> memoryview:
104120
"""
105121

106122
try:
107-
return np.ascontiguousarray(obj).view("b")
123+
return np.ascontiguousarray(obj).reshape(-1).view("b")
108124
except:
109125
return None
110126

0 commit comments

Comments
 (0)