Skip to content

Instantly share code, notes, and snippets.

@sup39
Created January 20, 2023 04:37
Show Gist options
  • Save sup39/301da346a2b15ca1a99c936dd68cdf3b to your computer and use it in GitHub Desktop.
Save sup39/301da346a2b15ca1a99c936dd68cdf3b to your computer and use it in GitHub Desktop.
## install required packages
# $ pip install numpy opencv-python
# import required packages
import numpy as np
import cv2
from tqdm.notebook import tqdm
# specify the input video file name
fileName = './2.mkv'
# open video file
cap = cv2.VideoCapture(fileName)
# count black frame
count = 0
## use tqdm to show a progress bar
bar = tqdm(total=int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), unit='f')
## for each frame in the video
while True:
# read a frame
valid, frame = cap.read()
if not valid: break
# each `frame` has shape (H, W, C)
H, W, C = frame.shape
## check the center (1/2)*(1/2) only
## since there may be some noise at the edges
## count += 1 if all pixels in the center are black (0,0,0)
h, w = H>>2, W>>2
count += frame[h:-h, w:-w].max() == 0
## update the progress bar
bar.update()
## mark progress bar as done
bar.disp(bar_style='success', check_delay=False)
# close
cap.release()
# print the result
print('Black frame count:', count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment