首页 > 解决方案 > Google Colab 无法捕捉超过 60 秒的网络摄像头视频

问题描述

我一直在尝试在 Google Colab 中设置网络摄像头录制。通过调整此处找到的代码:有没有办法在 google colab 中使用网络摄像头捕获实时视频?我能够获得一些视频,但是,如果我尝试录制超过 60 秒,环境就会崩溃。

我尝试了一些粗略的解决方法:

这项工作使我相信问题不是超时,而是由于传入的视频流超出了视频缓冲区大小造成的。

我是 javascript 新手,并意识到这并不是 Colab 的真正用途,但希望能提供任何帮助 - 即使它只是让我知道这不是一件可行的事情!我的代码可以在下面找到,在此先感谢!


# Run the function, get the video path as saved in your notebook, and play it back here.
from IPython.display import HTML, display, Javascript
from base64 import b64encode, b64decode
from google.colab.output import eval_js

def record_video_timed(filename='video.mp4'):
  js = Javascript("""
  async function wait(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}
    async function recordVideo() {
      // mashes together the advanced_outputs.ipynb function provided by Colab, 
      // a bunch of stuff from Stack overflow, and some sample code from:
      // https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API

      // Optional frames per second argument.
      const options = { mimeType: "video/webm; codecs=vp9" };
      const stream = await navigator.mediaDevices.getUserMedia({video: true});
      let recorder = new MediaRecorder(stream, options);
      
      recorder.start();
      await wait(10000);//less than 60 or it crashes
      recorder.stop(); 

      let recData = await new Promise((resolve) => recorder.ondataavailable = resolve);
      let arrBuff = await recData.data.arrayBuffer();
      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. Saved binary under filename in current working directory: {filename}"
    )
  except Exception as err:
      # In case any exceptions arise
      print(str(err))
  return filename

#######

video_path = record_video_timed()

标签: javascriptgoogle-chromegoogle-colaboratorywebcam

解决方案


推荐阅读