Skip to content Skip to sidebar Skip to footer

Get Raw Pixel Data From HTML5 Video

I would like to get the raw data (TypedArray or something) from video element and manipulate them with JavaScript. Currently I create a new canvas, draw the video into canvas and t

Solution 1:

Please read

https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript

Because you don't actually show any code, mention the browser or give out information about the video (resolution, FPS, encoding) it is impossible to tell why your code is slow or why it creating show much garbage. Real-time video effects with Javasrcipt are possible with some resolution constrain.

Here is an Mozilla example of real-time video filtering using .

https://developer.mozilla.org/samples/video/chroma-key/index.xhtml https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Manipulating_video_using_canvas

You won't get any raw access to video data without blitting a video frame on <canvas> first. However, I believe this operation should be HW accelerated as it happens all in GPU memory. Downloading pixels down from GPU manipulating them in Javascript and then uploading back to the display memory might be the slowest step for high resolution.

Optimization tips


Solution 2:

clearRect() is what I think you are looking for.

To the best of my knowledge, uploading images on canvases takes space, but clearRect() deletes all memory on the canvas.

What I did on JS code:

  1. On the Code down below is I set a stream on a video tag. See https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm for more WebCam info.

  2. I set an interval of one second(calls function every second) to Clear the canvas and then draw what the stream was on that frame.

  3. Now It your turn. Minimizing the video or canvas should have no effect on what to do. now you can grab the pixle colors on the webcam or video whenever.

Here is the Working Code:

Note: I found that this Example does not work with the built in web viewer. This example needs it's own web page.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
  
<style>
#container {
    margin: 0px auto;
    width: 500px;
    height: 375px;
    border: 10px #333 solid;
}
#videoElement {
    width: 500px;
    height: 375px;
    background-color: #666;
}
#canvas {
  background-color: #666;
}
</style>
</head>
  
<body>
    <video autoplay="true" id="videoElement"></video>
    <canvas id="canvas" onclick= canvasOnclick()></canvas>

<script>
var video = document.querySelector("#videoElement");
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
if (navigator.mediaDevices.getUserMedia) {       
    navigator.mediaDevices.getUserMedia({video: true})
  .then(function(stream) {
    video.srcObject = stream;
  })
  .catch(function(err0r) {
    console.log("Something went wrong!");
  });
}



var myVar = setInterval(intervalTimer, 1000);

function intervalTimer() {
  ctx.clearRect(0,0,400,400);
  ctx.drawImage(video,0,0,400,400);

}

//var data = ctx.getImageData(0, 0, w, h).data;
</script>
</body>
</html>

Solution 3:

canvas{display:none} may help reducing some overhead. Canvas remains accessible to js.


Post a Comment for "Get Raw Pixel Data From HTML5 Video"