Skip to content

Commit 2804c12

Browse files
stephenyan1231fmassa
authored andcommitted
fix a bug when video decoding fails and empty frames are returned (#1506)
1 parent a711c80 commit 2804c12

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

torchvision/io/video.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,14 @@ def _read_from_stream(container, start_offset, end_offset, pts_unit, stream, str
137137
pass
138138
# ensure that the results are sorted wrt the pts
139139
result = [frames[i] for i in sorted(frames) if start_offset <= frames[i].pts <= end_offset]
140-
if start_offset > 0 and start_offset not in frames:
140+
if len(frames) > 0 and start_offset > 0 and start_offset not in frames:
141141
# if there is no frame that exactly matches the pts of start_offset
142142
# add the last frame smaller than start_offset, to guarantee that
143143
# we will have all the necessary data. This is most useful for audio
144-
first_frame_pts = max(i for i in frames if i < start_offset)
145-
result.insert(0, frames[first_frame_pts])
144+
preceding_frames = [i for i in frames if i < start_offset]
145+
if len(preceding_frames) > 0:
146+
first_frame_pts = max(preceding_frames)
147+
result.insert(0, frames[first_frame_pts])
146148
return result
147149

148150

0 commit comments

Comments
 (0)