Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to capture video in google colab with python

from IPython.display import display, Javascript,HTML
from google.colab.output import eval_js
from base64 import b64decode
 
def record_video(filename='video_output.mp4'):
  js=Javascript("""
    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";
      capture.style.background = "orange";
      capture.style.color = "white";
 
      stopCapture.textContent = "Stop Recording";
      stopCapture.style.background = "red";
      stopCapture.style.color = "white";
      div.appendChild(capture);
 
      const video = document.createElement('video');
      const recordingVid = document.createElement("video");
      video.style.display = 'block';
 
      const stream = await navigator.mediaDevices.getUserMedia({audio:true, video: true});
     
      let recorder = new MediaRecorder(stream, options);
      document.body.appendChild(div);
      div.appendChild(video);
 
      video.srcObject = stream;
      video.muted = true;
 
      await video.play();
 
      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
 
      await new Promise((resolve) => {
        capture.onclick = resolve;
      });
      recorder.start();
      capture.replaceWith(stopCapture);
 
      await new Promise((resolve) => stopCapture.onclick = resolve);
      recorder.stop();
      let recData = await new Promise((resolve) => recorder.ondataavailable = resolve);
      let arrBuff = await recData.data.arrayBuffer();
       
      // stop the stream and remove the video element
      stream.getVideoTracks()[0].stop();
      div.remove();
 
      let binaryString = "";
      let bytes = new Uint8Array(arrBuff);
      bytes.forEach((byte) => {
        binaryString += String.fromCharCode(byte);
      })
    return btoa(binaryString);
    }
  """)
  try:
    display(js)
    data=eval_js('recordVideo({})')
    binary=b64decode(data)
    with open(filename,"wb") as video_file:
      video_file.write(binary)
    print(f"Finished recording video at:{filename}")
  except Exception as err:
    print(str(err))
Comment

PREVIOUS NEXT
Code Example
Python :: pandas count show one column 
Python :: get xlim python 
Python :: django login 
Python :: tables in python 
Python :: pandas.get_dummies 
Python :: how to read json from python 
Python :: Scatter plot with regression line Python 
Python :: argparse print help if no arguments 
Python :: python for loop increment 
Python :: how to remove role from people with a reaction discord bot python 
Python :: python get first occurrence in list 
Python :: Customizable TKinter Buttons Python 
Python :: getting size of list in python 
Python :: # enumerate 
Python :: Python using webbrowser 
Python :: pandas resample friday 
Python :: how to count substring in a string in python 
Python :: How to install a python packagae 
Python :: django create super user 
Python :: python run bat in new cmd window 
Python :: strptime python 
Python :: django from 
Python :: python compare each item of one list 
Python :: slicing of strings in python 
Python :: python pyaudio error 
Python :: drf serializer general validate method 
Python :: pandas filter column with or 
Python :: black code formatter 
Python :: changing names of column pandas 
Python :: removing duplicates using json python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =