Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions src/torchcodec/decoders/_video_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ def get_frames_at(self, indices: list[int]) -> FrameBatch:
Returns:
FrameBatch: The frames at the given indices.
"""
if isinstance(indices, torch.Tensor):
# TODO we should avoid converting tensors to lists and just let the
# core ops and C++ code natively accept tensors. See
# https://github.com/pytorch/torchcodec/issues/879
indices = indices.to(torch.int).tolist()

data, pts_seconds, duration_seconds = core.get_frames_at_indices(
self._decoder, frame_indices=indices
)
Expand Down Expand Up @@ -301,6 +307,12 @@ def get_frames_played_at(self, seconds: list[float]) -> FrameBatch:
Returns:
FrameBatch: The frames that are played at ``seconds``.
"""
if isinstance(seconds, torch.Tensor):
# TODO we should avoid converting tensors to lists and just let the
# core ops and C++ code natively accept tensors. See
# https://github.com/pytorch/torchcodec/issues/879
seconds = seconds.to(torch.float).tolist()

data, pts_seconds, duration_seconds = core.get_frames_by_pts(
self._decoder, timestamps=seconds
)
Expand Down
11 changes: 11 additions & 0 deletions test/test_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,17 @@ def test_10bit_videos_cpu(self, asset):
# custom_frame_mappings=custom_frame_mappings,
# )

def test_get_frames_at_tensor_indices(self):
# Non-regression test for tensor support in get_frames_at() and
# get_frames_played_at()
decoder = VideoDecoder(NASA_VIDEO.path)

decoder.get_frames_at(torch.tensor([0, 10], dtype=torch.int))
decoder.get_frames_at(torch.tensor([0, 10], dtype=torch.float))

decoder.get_frames_played_at(torch.tensor([0, 1], dtype=torch.int))
decoder.get_frames_played_at(torch.tensor([0, 1], dtype=torch.float))


class TestAudioDecoder:
@pytest.mark.parametrize("asset", (NASA_AUDIO, NASA_AUDIO_MP3, SINE_MONO_S32))
Expand Down
Loading