Skip to content

Commit c5acc9d

Browse files
committed
Add more tests
1 parent e326b4f commit c5acc9d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

test/test_io.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,24 @@ def test_read_video_timestamps_corrupted_file(self):
253253
self.assertEqual(video_pts, [])
254254
self.assertIs(video_fps, None)
255255

256+
def test_read_video_partially_corrupted_file(self):
257+
with temp_video(5, 4, 4, 5, lossless=True) as (f_name, data):
258+
with open(f_name, 'r+b') as f:
259+
size = os.path.getsize(f_name)
260+
bytes_to_overwrite = size // 10
261+
# seek to the middle of the file
262+
f.seek(5 * bytes_to_overwrite)
263+
# corrupt 10% of the file from the middle
264+
f.write(b'\xff' * bytes_to_overwrite)
265+
# this exercises the container.decode assertion check
266+
video, audio, info = io.read_video(f.name, pts_unit='sec')
267+
# check that size is not equal to 5, but 3
268+
self.assertEqual(len(video), 3)
269+
# but the valid decoded content is still correct
270+
self.assertTrue(video[:3].equal(data[:3]))
271+
# and the last few frames are wrong
272+
self.assertFalse(video.equal(data))
273+
256274
# TODO add tests for audio
257275

258276

torchvision/io/video.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ def read_video(filename, start_pts=0, end_pts=None, pts_unit='pts'):
210210
if container.streams.video:
211211
video_frames = _read_from_stream(container, start_pts, end_pts, pts_unit,
212212
container.streams.video[0], {'video': 0})
213-
info["video_fps"] = float(container.streams.video[0].average_rate)
213+
video_fps = container.streams.video[0].average_rate
214+
# guard against potentially corrupted files
215+
if video_fps is not None:
216+
info["video_fps"] = float(video_fps)
214217

215218
if container.streams.audio:
216219
audio_frames = _read_from_stream(container, start_pts, end_pts, pts_unit,

0 commit comments

Comments
 (0)