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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@

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)

Check warning on line 87 in docarray/typing/bytes/video_bytes.py

View check run for this annotation

Codecov / codecov/patch

docarray/typing/bytes/video_bytes.py#L86-L87

Added lines #L86 - L87 were not covered by tests

if len(audio_frames) == 0:
audio = parse_obj_as(AudioNdArray, np.array(audio_frames))
else:
Expand All @@ -89,3 +95,15 @@
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)

Check warning on line 102 in docarray/typing/bytes/video_bytes.py

View check run for this annotation

Codecov / codecov/patch

docarray/typing/bytes/video_bytes.py#L102

Added line #L102 was not covered by tests

# 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])))

Check warning on line 107 in docarray/typing/bytes/video_bytes.py

View check run for this annotation

Codecov / codecov/patch

docarray/typing/bytes/video_bytes.py#L105-L107

Added lines #L105 - L107 were not covered by tests

return arrays

Check warning on line 109 in docarray/typing/bytes/video_bytes.py

View check run for this annotation

Codecov / codecov/patch

docarray/typing/bytes/video_bytes.py#L109

Added line #L109 was not covered by tests
Loading