[docs]defread_video(filename="video.mp4"):"""**Reads a video file into an array** Reads a video file (e.g., .mp4) into a numpy array of shape. This function requires OpenCV to be installed via the ``opencv-python`` package. Parameters ---------- filename : str The path of a video file. Returns ------- array numpy array of shape (frame, RGB-channel, height, width). int Sampling rate in frames per second. Examples -------- .. ipython:: python import neurokit2 as nk # video, sampling_rate = nk.read_video("video.mp4") """# Try loading cv2try:importcv2exceptImportError:raiseImportError("The 'cv2' module is required for this function to run. ","Please install it first (`pip install opencv-python`).",)# Check if file existsassertos.path.isfile(filename)isTrue,f"No file found with the specified name ({filename})."capture=cv2.VideoCapture(filename)sampling_rate=int(capture.get(cv2.CAP_PROP_FPS))frames=[]whilecapture.isOpened():success,frame=capture.read()# By default frame is BGRifnotsuccess:breakframes.append(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))# Convert to RGBcapture.release()# Swap axes to be (frame, RGB-channel, height, width)returnnp.array(frames).swapaxes(3,1).swapaxes(3,2),sampling_rate