Making an Infinite Action Movie Trailer

July 20, 2014

Marios Katsakioris presenting our hack

We're going to discuss the technology behind hacking together an infinite action film trailer, a project we worked on with Marios Katsakioris during the AND #filmdatahack.

Watch the infinite action movie trailer

Our simple method for making a film trailer

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.

Finding black frames

Once we have downloaded a film trailer (youtube-dl may be helpful), we can use opencv python bindings to process each frame in the video and detect if it is black.

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