code: def test_frame_reading(self, test_video, backend):
      torchvision.set_video_backend(backend)
      full_path = os.path.join(VIDEO_DIR, test_video)
      with av.open(full_path) as av_reader:
          if av_reader.streams.video:
              av_frames, vr_frames = [], []
              av_pts, vr_pts = [], []
              # get av frames
              for av_frame in av_reader.decode(
                  av_reader.streams.video[0]
              ):
                  av_frames.append(
                      torch.tensor(
                          av_frame.to_rgb().to_ndarray()
                      ).permute(2, 0, 1)
                  )
                  av_pts.append(av_frame.pts * av_frame.time_base)
  
              # get vr frames
              video_reader = VideoReader(full_path, "video")
              for vr_frame in video_reader:
                  vr_frames.append(vr_frame["data"])
                  vr_pts.append(vr_frame["pts"])
  
              # same number of frames
              assert len(vr_frames) == len(av_frames)
              assert len(vr_pts) == len(av_pts)
  
              # compare the frames and ptss
              for i in range(len(vr_frames)):
                  assert float(av_pts[i]) == approx(vr_pts[i], abs=0.1)
  
                  mean_delta = torch.mean(
                      torch.abs(
                          av_frames[i].float() - vr_frames[i].float()
                      )
                  )
                  # on average the difference is very small and caused
                  # by decoding (around 1%)
                  # TODO: asses empirically how to set this? atm it's 1%
                  # averaged over all frames
                  assert mean_delta.item() < 2.55
  
              del vr_frames, av_frames, vr_pts, av_pts
  
      # test audio reading compared to PYAV
      with av.open(full_path) as av_reader:
          if av_reader.streams.audio:
              av_frames, vr_frames = [], []
              av_pts, vr_pts = [], []
              # get av frames
              for av_frame in av_reader.decode(
                  av_reader.streams.audio[0]
              ):
                  av_frames.append(
                      torch.tensor(av_frame.to_ndarray()).permute(1, 0)
                  )
                  av_pts.append(av_frame.pts * av_frame.time_base)
              av_reader.close()
  
              # get vr frames
              video_reader = VideoReader(full_path, "audio")
              for vr_frame in video_reader:
                  vr_frames.append(vr_frame["data"])
                  vr_pts.append(vr_frame["pts"])
  
              # same number of frames
              assert len(vr_frames) == len(av_frames)
              assert len(vr_pts) == len(av_pts)
  
              # compare the frames and ptss
              for i in range(len(vr_frames)):
                  assert float(av_pts[i]) == approx(vr_pts[i], abs=0.1)
                  max_delta = torch.max(
                      torch.abs(
                          av_frames[i].float() - vr_frames[i].float()
                      )
                  )
                  # we assure that there is never more than 1% difference in signal
                  assert max_delta.item() < 0.001
  
file_path: vision/test/test_videoapi.py
imports: import collections
  import os
  import urllib
  import pytest
  import torch
  import torchvision
  from pytest import approx
  from torchvision.datasets.utils import download_url
  from torchvision.io import _HAS_VIDEO_OPT, VideoReader
label_desc: Use image node_id 1 for calling the TestVideoApi obj's underlying member method code with example usage: obj.test_frame_reading(test_video, backend) without return types