20 Jul 2014
We would create an algorithm to automatically cut a film trailer into clips.
Action film trailers make use of the "fade-to-black" transition. If we could detect black(ish) frames on the video then we could cut the trailer at these points.
import cv2 import numpy cap = cv2.VideoCapture('trailer.mp4') # Quit on 'q' key press while cv2.waitKey(1) & 0xFF != ord('q'): # Process the trailer frame-by-frame grabbed, frame = cap.read() if not grabbed: # End of the video break # Convert to grayscale, as we are only interested in black, we don't # care about colors. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect if everything is black, i.e., zero pixel value num_nonzeros = numpy.count_nonzero(gray) if num_nonzeros == 0: # Display a white square to show when a black frame is detected. gray[0:100] = 255 # Display the frame cv2.imshow('frame', gray) # Release the capture cap.release() cv2.destroyAllWindows()
The whole project can be found on github