Skip to content

Commit 8b88783

Browse files
author
Daniel Flores
committed
simplify decode method by using VideoDecoder
1 parent 11db2b2 commit 8b88783

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

test/test_ops.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
get_next_frame,
4343
seek_to_pts,
4444
)
45+
from torchcodec.decoders import VideoDecoder
4546

4647
from .utils import (
4748
all_supported_devices,
@@ -1379,19 +1380,13 @@ def test_bad_input(self, tmp_path):
13791380
filename="./bad/path.mp3",
13801381
)
13811382

1382-
def decode(self, file_path=None, tensor=None) -> torch.Tensor:
1383-
if file_path is not None:
1384-
decoder = create_from_file(str(file_path), seek_mode="approximate")
1385-
elif tensor is not None:
1386-
decoder = create_from_tensor(tensor, seek_mode="approximate")
1387-
add_video_stream(decoder)
1388-
frames, *_ = get_frames_in_range(decoder, start=0, stop=60)
1389-
return frames
1383+
def decode(self, source=None) -> torch.Tensor:
1384+
return VideoDecoder(source).get_frames_in_range(start=0, stop=60)
13901385

13911386
@pytest.mark.parametrize("format", ("mov", "mp4", "mkv", "webm"))
13921387
@pytest.mark.parametrize("method", ("to_file", "to_tensor"))
13931388
def test_video_encoder_round_trip(self, tmp_path, format, method):
1394-
# Test that decode(encode(decode(asset))) == decode(asset)
1389+
# Test that decode(encode(decode(frames))) == decode(frames)
13951390
ffmpeg_version = get_ffmpeg_major_version()
13961391
# In FFmpeg6, the default codec's best pixel format is lossy for all container formats but webm.
13971392
# As a result, we skip the round trip test.
@@ -1403,8 +1398,7 @@ def test_video_encoder_round_trip(self, tmp_path, format, method):
14031398
ffmpeg_version == 4 or (IS_WINDOWS and ffmpeg_version in (6, 7))
14041399
):
14051400
pytest.skip("Codec for webm is not available in this FFmpeg installation.")
1406-
asset = TEST_SRC_2_720P
1407-
source_frames = self.decode(file_path=str(asset.path)).data
1401+
source_frames = self.decode(TEST_SRC_2_720P.path).data
14081402

14091403
params = dict(
14101404
frame_rate=30, crf=0
@@ -1416,12 +1410,12 @@ def test_video_encoder_round_trip(self, tmp_path, format, method):
14161410
filename=encoded_path,
14171411
**params,
14181412
)
1419-
round_trip_frames = self.decode(file_path=encoded_path).data
1413+
round_trip_frames = self.decode(encoded_path).data
14201414
else: # to_tensor
14211415
encoded_tensor = encode_video_to_tensor(
14221416
source_frames, format=format, **params
14231417
)
1424-
round_trip_frames = self.decode(tensor=encoded_tensor).data
1418+
round_trip_frames = self.decode(encoded_tensor).data
14251419

14261420
assert source_frames.shape == round_trip_frames.shape
14271421
assert source_frames.dtype == round_trip_frames.dtype
@@ -1453,8 +1447,7 @@ def test_video_encoder_against_ffmpeg_cli(self, tmp_path, format):
14531447
pytest.skip(
14541448
"Codec for webm is not available in the FFmpeg6/7 installation on Windows."
14551449
)
1456-
asset = TEST_SRC_2_720P
1457-
source_frames = self.decode(str(asset.path)).data
1450+
source_frames = self.decode(TEST_SRC_2_720P.path).data
14581451

14591452
# Encode with FFmpeg CLI
14601453
temp_raw_path = str(tmp_path / "temp_input.raw")

0 commit comments

Comments
 (0)