cv2.VideoCapture() is not opening the web camera when executing the command in Google Colab because the code runs on the “cloud” (i.e. on a remote server). The processing happens on google’s servers, they’ll have no physical access to your device. So when you trying to have VideoCapture read the first local (local to where the code runs) camera won’t work.

Since Colab is running in your browser, you need to use web APIs to access local hardware like a camera.

Google provided code to capture an image inside Google Colab but there no code to capture the video there.

The following code lets you take video inside of Google Colab. It uses Javascript inside of colab to access the client computer’s camera.

from IPython.display import display, Javascript,HTML
from google.colab.output import eval_js
from base64 import b64decode

def record_video(filename):
    async function recordVideo() {
      const options = { mimeType: "video/webm; codecs=vp9" };
      const div = document.createElement('div');
      const capture = document.createElement('button');
      const stopCapture = document.createElement("button");
      capture.textContent = "Start Recording"; = "orange"; = "white";

      stopCapture.textContent = "Stop Recording"; = "red"; = "white";

      const video = document.createElement('video');
      const recordingVid = document.createElement("video"); = 'block';

      const stream = await navigator.mediaDevices.getUserMedia({audio:true, video: true});
      let recorder = new MediaRecorder(stream, options);

      video.srcObject = stream;
      video.muted = true;


      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

      await new Promise((resolve) => {
        capture.onclick = resolve;

      await new Promise((resolve) => stopCapture.onclick = resolve);
      let recData = await new Promise((resolve) => recorder.ondataavailable = resolve);
      let arrBuff = await;
      // stop the stream and remove the video element

      let binaryString = "";
      let bytes = new Uint8Array(arrBuff);
      bytes.forEach((byte) => {
        binaryString += String.fromCharCode(byte);
    return btoa(binaryString);
    with open(filename,"wb") as video_file:
    print(f"Finished recording video at:{filename}")
  except Exception as err:

If you run OpenCV on Google Colab it will look for the camera on the server and not the client computer. Since this code runs in javascript it uses the client’s computer. Since Colab is running in your browser, you need to use web APIs to access local hardware like a camera.

video_path = "test.mp4"
Colab Capture video

Play Video in Google Colab

If you want to play the video in Google Colab without downloading you need to compress the video file to play it in google colab, if the format is not supported.

from IPython.display import HTML
from base64 import b64encode

def show_video(video_path, video_width = 600):
  video_file = open(video_path, "r+b").read()

  video_url = f"data:video/mp4;base64,{b64encode(video_file).decode()}"
  return HTML(f"""<video width={video_width} controls><source src="{video_url}"></video>""")

Play Video in Google Colab

Run this code in Google Colab