MCPcopy Create free account
hub / github.com/Wscats/chrome-devtools-cli / dropBlackFrames

Function dropBlackFrames

src/__tests__/MediaStreamRecorder.js:1970–2044  ·  view source on GitHub ↗
(_frames, _framesToCheck, _pixTolerance, _frameTolerance)

Source from the content-addressed store, hash-verified

1968 }
1969
1970 function dropBlackFrames(_frames, _framesToCheck, _pixTolerance, _frameTolerance) {
1971 var localCanvas = document.createElement('canvas');
1972 localCanvas.width = canvas.width;
1973 localCanvas.height = canvas.height;
1974 var context2d = localCanvas.getContext('2d');
1975 var resultFrames = [];
1976
1977 var checkUntilNotBlack = _framesToCheck === -1;
1978 var endCheckFrame = (_framesToCheck && _framesToCheck > 0 && _framesToCheck <= _frames.length) ?
1979 _framesToCheck : _frames.length;
1980 var sampleColor = {
1981 r: 0,
1982 g: 0,
1983 b: 0
1984 };
1985 var maxColorDifference = Math.sqrt(
1986 Math.pow(255, 2) +
1987 Math.pow(255, 2) +
1988 Math.pow(255, 2)
1989 );
1990 var pixTolerance = _pixTolerance && _pixTolerance >= 0 && _pixTolerance <= 1 ? _pixTolerance : 0;
1991 var frameTolerance = _frameTolerance && _frameTolerance >= 0 && _frameTolerance <= 1 ? _frameTolerance : 0;
1992 var doNotCheckNext = false;
1993
1994 for (var f = 0; f < endCheckFrame; f++) {
1995 var matchPixCount, endPixCheck, maxPixCount;
1996
1997 if (!doNotCheckNext) {
1998 var image = new Image();
1999 image.src = _frames[f].image;
2000 context2d.drawImage(image, 0, 0, canvas.width, canvas.height);
2001 var imageData = context2d.getImageData(0, 0, canvas.width, canvas.height);
2002 matchPixCount = 0;
2003 endPixCheck = imageData.data.length;
2004 maxPixCount = imageData.data.length / 4;
2005
2006 for (var pix = 0; pix < endPixCheck; pix += 4) {
2007 var currentColor = {
2008 r: imageData.data[pix],
2009 g: imageData.data[pix + 1],
2010 b: imageData.data[pix + 2]
2011 };
2012 var colorDifference = Math.sqrt(
2013 Math.pow(currentColor.r - sampleColor.r, 2) +
2014 Math.pow(currentColor.g - sampleColor.g, 2) +
2015 Math.pow(currentColor.b - sampleColor.b, 2)
2016 );
2017 // difference in color it is difference in color vectors (r1,g1,b1) <=> (r2,g2,b2)
2018 if (colorDifference <= maxColorDifference * pixTolerance) {
2019 matchPixCount++;
2020 }
2021 }
2022 }
2023
2024 if (!doNotCheckNext && maxPixCount - matchPixCount <= maxPixCount * frameTolerance) {
2025 // console.log('removed black frame : ' + f + ' ; frame duration ' + _frames[f].duration);
2026 } else {
2027 // console.log('frame is passed : ' + f);

Callers 1

WhammyRecorderHelperFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected