Skip to content

Commit b74366b

Browse files
authored
Fixed missing audio with video_reader backend (#3934)
* Fixed missing audio with video_reader backend * Added unit test
1 parent 496cb40 commit b74366b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

test/test_video_reader.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,17 @@ def test_invalid_file(self):
12441244
with self.assertRaises(RuntimeError):
12451245
io.read_video('foo.mp4')
12461246

1247+
def test_audio_present(self):
1248+
"""Test if audio frames are returned with video_reader backend."""
1249+
set_video_backend('video_reader')
1250+
for test_video, _ in test_videos.items():
1251+
full_path = os.path.join(VIDEO_DIR, test_video)
1252+
container = av.open(full_path)
1253+
if container.streams.audio:
1254+
_, audio, _ = io.read_video(full_path)
1255+
self.assertGreaterEqual(audio.shape[0], 1)
1256+
self.assertGreaterEqual(audio.shape[1], 1)
1257+
12471258

12481259
if __name__ == "__main__":
12491260
unittest.main()

torchvision/io/_video_opt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def _align_audio_frames(aframes, aframe_pts, audio_pts_range):
155155
e_idx = num_samples
156156
if start < audio_pts_range[0]:
157157
s_idx = int((audio_pts_range[0] - start) / step_per_aframe)
158-
if end > audio_pts_range[1]:
158+
if audio_pts_range[1] != -1 and end > audio_pts_range[1]:
159159
e_idx = int((audio_pts_range[1] - end) / step_per_aframe)
160160
return aframes[s_idx:e_idx, :]
161161

0 commit comments

Comments
 (0)