Skip to content

Instantly share code, notes, and snippets.

@my5t3ry
Created April 30, 2020 16:19
Show Gist options
  • Save my5t3ry/6fad576eb2f37c0c11f42dd5416fdf0e to your computer and use it in GitHub Desktop.
Save my5t3ry/6fad576eb2f37c0c11f42dd5416fdf0e to your computer and use it in GitHub Desktop.
function removeAnimatedFrames() {
let result = [];
let i = 0;
let freezeLastFrame = 200;
let stillFrames = 0;
let dropFrames = 5;
let stillFrameThreshold = 10;
let frame = {
duration: recordedFrames[i].duration,
frame: recordedFrames[i].frame,
buffer: recordedFrames[i].buffer
}
result.push(frame);
while (i + dropFrames < recordedFrames.length) {
let dif = pixelmatch(recordedFrames[i].buffer, recordedFrames[i + 1].buffer, null, w, h, {threshold: 0.1});
if (dif == 0) {
if (stillFrames == stillFrameThreshold) {
frame = {
duration: recordedFrames[i - stillFrameThreshold].duration,
frame: recordedFrames[i - stillFrameThreshold].frame,
buffer: recordedFrames[i - stillFrameThreshold].buffer
};
result.push(frame)
stillFrames = 0;
} else {
result[result.length - 1].duration = result[result.length - 1].duration + recordedFrames[i + 1].duration;
stillFrames++;
}
} else {
result[result.length - 1].duration = result[result.length - 1].duration + recordedFrames[i + 1].duration;
}
i++;
}
frame = {
duration: recordedFrames[i].duration,
frame: recordedFrames[i].frame,
buffer: recordedFrames[i].buffer
};
frame.duration = frame.duration + freezeLastFrame;
result.push(frame);
let secondStageResult = [];
secondStageResult.push(result[0]);
for (let ii = 1; ii < result.length - 1; ii++) {
if (pixelmatch(secondStageResult[secondStageResult.length - 1].buffer, result[ii].buffer, null, w, h, {threshold: 0.1}) == 0) {
secondStageResult[secondStageResult.length - 1].duration = secondStageResult[secondStageResult.length - 1].duration + result[ii].duration;
} else {
secondStageResult.push(result[ii]);
}
}
return secondStageResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment