Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pad audio arrays to same shape if sample rates differ #1880

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docarray/typing/bytes/video_bytes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from io import BytesIO
from typing import TYPE_CHECKING, List, NamedTuple, TypeVar

Expand Down Expand Up @@ -80,6 +81,11 @@ class MyDoc(BaseDoc):

video_frames.append(frame.to_ndarray(format='rgb24'))

# Pad audio arrays to same shape if sample rates differ
if len({arr.shape for arr in audio_frames}) > 1:
warnings.warn('Audio frames have different sample rates')
audio_frames = self._pad_arrays_to_same_shape(audio_frames)

if len(audio_frames) == 0:
audio = parse_obj_as(AudioNdArray, np.array(audio_frames))
else:
Expand All @@ -89,3 +95,15 @@ class MyDoc(BaseDoc):
indices = parse_obj_as(NdArray, keyframe_indices)

return VideoLoadResult(video=video, audio=audio, key_frame_indices=indices)

@staticmethod
def _pad_arrays_to_same_shape(arrays: List[np.ndarray]) -> List[np.ndarray]:
# Calculate the maximum number of samples in any array
max_samples = max(arr.shape[1] for arr in arrays)

# Pad arrays with fewer samples
for i, arr in enumerate(arrays):
if arr.shape[1] < max_samples:
arrays[i] = np.pad(arr, ((0, 0), (0, max_samples - arr.shape[1])))

return arrays