Display OpenCV (cv2.VideoCapture) Video in Jupyter/Colab

The following code will loop the video, perform some processing and write the output to a video file.

import cv2from google.colab.patches import cv2_imshowvideo_path = "./video.mp4"cap = cv2.VideoCapture(video_path)if cap.isOpened():    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))    res=(int(width), int(height))    # this format fail to play in Chrome/Win10/Colab    # fourcc = cv2.VideoWriter_fourcc(*'MP4V') #codec    fourcc = cv2.VideoWriter_fourcc(*'H264') #codec    out = cv2.VideoWriter('output.mp4', fourcc, 20.0, res)    frame = None    while True:        try:            is_success, frame = cap.read()        except cv2.error:            continue        if not is_success:            break        # OPTIONAL: do some processing        # convert cv2 BGR format to RGB        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)        out.write(image)    out.release()     # OPTIONAL: show last image    if frame:      cv2_imshow(frame)cap.release()

Show the output video (this will not work for large video file)

from IPython.display import HTMLfrom base64 import b64encodemp4 = open(video_path, "rb").read()data_url = "data:video/mp4;base64," + b64encode(mp4).decode()HTML(f"""<video width=400 controls>      <source src="{data_url}" type="video/mp4"></video>""")

You could display each frame via method such as cv2_imshow, but you will have a long list of images.

You could perform a show and clear, but you will have some performance issues as the UI doesn't display the output fast enough (will have many skipped frames)

from IPython.display import clear_outputfrom google.colab.patches import cv2_imshowcv2_imshow(image)clear_output(wait=True)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.