@@ -378,10 +378,10 @@ def test_device_instance(self):
378378 def test_getitem_fails (self , device , seek_mode ):
379379 decoder = VideoDecoder (NASA_VIDEO .path , device = device , seek_mode = seek_mode )
380380
381- with pytest .raises (IndexError , match = "out of bounds " ):
381+ with pytest .raises (IndexError , match = "Invalid frame index " ):
382382 frame = decoder [1000 ] # noqa
383383
384- with pytest .raises (IndexError , match = "out of bounds " ):
384+ with pytest .raises (IndexError , match = "Invalid frame index " ):
385385 frame = decoder [- 1000 ] # noqa
386386
387387 with pytest .raises (TypeError , match = "Unsupported key type" ):
@@ -490,10 +490,13 @@ def test_get_frame_at_tuple_unpacking(self, device):
490490 def test_get_frame_at_fails (self , device , seek_mode ):
491491 decoder = VideoDecoder (NASA_VIDEO .path , device = device , seek_mode = seek_mode )
492492
493- with pytest .raises (IndexError , match = "out of bounds" ):
493+ with pytest .raises (
494+ IndexError ,
495+ match = "negative indices must have an absolute value less than the number of frames" ,
496+ ):
494497 frame = decoder .get_frame_at (- 10000 ) # noqa
495498
496- with pytest .raises (IndexError , match = "out of bounds " ):
499+ with pytest .raises (IndexError , match = "must be less than " ):
497500 frame = decoder .get_frame_at (10000 ) # noqa
498501
499502 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
@@ -552,13 +555,13 @@ def test_get_frames_at(self, device, seek_mode):
552555 def test_get_frames_at_fails (self , device , seek_mode ):
553556 decoder = VideoDecoder (NASA_VIDEO .path , device = device , seek_mode = seek_mode )
554557
555- expected_converted_index = - 10000 + len (decoder )
556558 with pytest .raises (
557- RuntimeError , match = f"Invalid frame index={ expected_converted_index } "
559+ IndexError ,
560+ match = "negative indices must have an absolute value less than the number of frames" ,
558561 ):
559562 decoder .get_frames_at ([- 10000 ])
560563
561- with pytest .raises (RuntimeError , match = "Invalid frame index=390" ):
564+ with pytest .raises (IndexError , match = "Invalid frame index=390" ):
562565 decoder .get_frames_at ([390 ])
563566
564567 with pytest .raises (RuntimeError , match = "Expected a value of type" ):
@@ -775,6 +778,58 @@ def test_get_frames_in_range(self, stream_index, device, seek_mode):
775778 empty_frames .duration_seconds , NASA_VIDEO .empty_duration_seconds
776779 )
777780
781+ @pytest .mark .parametrize ("device" , cpu_and_cuda ())
782+ @pytest .mark .parametrize ("seek_mode" , ("exact" , "approximate" ))
783+ def test_get_frames_in_range_slice_indices_syntax (self , device , seek_mode ):
784+ decoder = VideoDecoder (
785+ NASA_VIDEO .path ,
786+ stream_index = 3 ,
787+ device = device ,
788+ seek_mode = seek_mode ,
789+ )
790+
791+ # high range ends get capped to num_frames
792+ frames387_389 = decoder .get_frames_in_range (start = 387 , stop = 1000 )
793+ assert frames387_389 .data .shape == torch .Size (
794+ [
795+ 3 ,
796+ NASA_VIDEO .get_num_color_channels (stream_index = 3 ),
797+ NASA_VIDEO .get_height (stream_index = 3 ),
798+ NASA_VIDEO .get_width (stream_index = 3 ),
799+ ]
800+ )
801+ ref_frame387_389 = NASA_VIDEO .get_frame_data_by_range (
802+ start = 387 , stop = 390 , stream_index = 3
803+ ).to (device )
804+ assert_frames_equal (frames387_389 .data , ref_frame387_389 )
805+
806+ # negative indices are converted
807+ frames387_389 = decoder .get_frames_in_range (start = - 3 , stop = 1000 )
808+ assert frames387_389 .data .shape == torch .Size (
809+ [
810+ 3 ,
811+ NASA_VIDEO .get_num_color_channels (stream_index = 3 ),
812+ NASA_VIDEO .get_height (stream_index = 3 ),
813+ NASA_VIDEO .get_width (stream_index = 3 ),
814+ ]
815+ )
816+ assert_frames_equal (frames387_389 .data , ref_frame387_389 )
817+
818+ # "None" as stop is treated as end of the video
819+ frames387_None = decoder .get_frames_in_range (start = - 3 , stop = None )
820+ assert frames387_None .data .shape == torch .Size (
821+ [
822+ 3 ,
823+ NASA_VIDEO .get_num_color_channels (stream_index = 3 ),
824+ NASA_VIDEO .get_height (stream_index = 3 ),
825+ NASA_VIDEO .get_width (stream_index = 3 ),
826+ ]
827+ )
828+ reference_frame387_389 = NASA_VIDEO .get_frame_data_by_range (
829+ start = 387 , stop = 390 , stream_index = 3
830+ ).to (device )
831+ assert_frames_equal (frames387_None .data , reference_frame387_389 )
832+
778833 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
779834 @pytest .mark .parametrize ("seek_mode" , ("exact" , "approximate" ))
780835 @patch ("torchcodec._core._metadata._get_stream_json_metadata" )
0 commit comments