Created
February 15, 2019 10:04
-
-
Save zafergurel/b5c5b170d561828145df783945ad6be9 to your computer and use it in GitHub Desktop.
Is Video Dark?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isVideoDark(videoEl) { | |
var fuzzy = 0.1; | |
// create canvas | |
var canvas = document.createElement("canvas"); | |
var videoWidth = videoEl.videoWidth; | |
var videoHeight = videoEl.videoHeight; | |
canvas.width = videoWidth; | |
canvas.height = videoHeight; | |
var ctx = canvas.getContext("2d"); | |
ctx.drawImage(videoEl, 0, 0); | |
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
var data = imageData.data; | |
var r, g, b, max_rgb; | |
var light = 0, dark = 0; | |
for(var x = 0, len = data.length; x < len; x+=4) { | |
r = data[x]; | |
g = data[x+1]; | |
b = data[x+2]; | |
max_rgb = Math.max(Math.max(r, g), b); | |
if (max_rgb < 128) | |
dark++; | |
else | |
light++; | |
} | |
var dl_diff = ((light - dark) / (videoWidth * videoHeight)); | |
if (dl_diff + fuzzy < 0) | |
return true; /* Dark. */ | |
else | |
return false; /* Not dark. */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a modified version of the script @ http://jsfiddle.net/s7Wx2/328/