code: def get_video_reader(
      source: str,
      save_dir: str,
      frame_skip_interval: int,
      export_visual: bool = False,
      view_visual: bool = False,
  ):
      """
      Creates OpenCV video capture object from given video file path.
  
      Args:
          source: Video file path
          save_dir: Video export directory
          frame_skip_interval: Frame skip interval
          export_visual: Set True if you want to export visuals
          view_visual: Set True if you want to render visual
  
      Returns:
          iterator: Pillow Image
          video_writer: cv2.VideoWriter
          video_file_name: video name with extension
      """
      # get video name with extension
      video_file_name = os.path.basename(source)
      # get video from video path
      video_capture = cv2.VideoCapture(source)
  
      num_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
      if view_visual:
          num_frames /= frame_skip_interval + 1
          num_frames = int(num_frames)
  
      def read_video_frame(video_capture, frame_skip_interval):
          if view_visual:
              cv2.imshow(
                  "Prediction of {}".format(str(video_file_name)),
                  cv2.WINDOW_AUTOSIZE,
              )
  
              while video_capture.isOpened:
                  frame_num = video_capture.get(cv2.CAP_PROP_POS_FRAMES)
                  video_capture.set(
                      cv2.CAP_PROP_POS_FRAMES,
                      frame_num + frame_skip_interval,
                  )
  
                  k = cv2.waitKey(20)
                  frame_num = video_capture.get(cv2.CAP_PROP_POS_FRAMES)
  
                  if k == 27:
                      print(
                          "\n===========================Closing==========================="
                      )  # Exit the prediction, Key = Esc
                      exit()
                  if k == 100:
                      frame_num += 100  # Skip 100 frames, Key = d
                  if k == 97:
                      frame_num -= 100  # Prev 100 frames, Key = a
                  if k == 103:
                      frame_num += 20  # Skip 20 frames, Key = g
                  if k == 102:
                      frame_num -= 20  # Prev 20 frames, Key = f
                  video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
  
                  ret, frame = video_capture.read()
                  if not ret:
                      print(
                          "\n=========================== Video Ended ==========================="
                      )
                      break
                  yield Image.fromarray(frame)
  
          else:
              while video_capture.isOpened:
                  frame_num = video_capture.get(cv2.CAP_PROP_POS_FRAMES)
                  video_capture.set(
                      cv2.CAP_PROP_POS_FRAMES,
                      frame_num + frame_skip_interval,
                  )
  
                  ret, frame = video_capture.read()
                  if not ret:
                      print(
                          "\n=========================== Video Ended ==========================="
                      )
                      break
                  yield Image.fromarray(frame)
  
      if export_visual:
          # get video properties and create VideoWriter object
          if frame_skip_interval != 0:
              fps = video_capture.get(
                  cv2.CAP_PROP_FPS
              )  # original fps of video
              # The fps of export video is increasing during view_image because frame is skipped
              fps = (
                  fps / frame_skip_interval
              )  # How many time_interval equals to original fps. One time_interval skip x frames.
          else:
              fps = video_capture.get(cv2.CAP_PROP_FPS)
  
          w = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
          h = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
          size = (w, h)
          fourcc = cv2.VideoWriter_fourcc(*"mp4v")
          video_writer = cv2.VideoWriter(
              os.path.join(save_dir, video_file_name), fourcc, fps, size
          )
      else:
          video_writer = None
  
      return (
          read_video_frame(video_capture, frame_skip_interval),
          video_writer,
          video_file_name,
          num_frames,
      )
  
imports: import copy
  import os
  import random
  import time
  from typing import List, Optional, Union
  import cv2
  import numpy
  import requests
  from PIL import Image
  from sahi.utils.file import Path
label_desc: Use image node_id 8 for calling a global function with example usage: get_video_reader(source, save_dir, frame_skip_interval, export_visual, view_visual) and returns: read_video_frame,  video_writer,  video_file_name,  num_frames