From 6f4874fa20d4fa156dd762d5dacefd8a2e656bf0 Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Tue, 8 Mar 2022 23:07:09 -0500 Subject: Lots of changes --- webAO/utils/calculateWebpLength.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 webAO/utils/calculateWebpLength.js (limited to 'webAO/utils/calculateWebpLength.js') diff --git a/webAO/utils/calculateWebpLength.js b/webAO/utils/calculateWebpLength.js new file mode 100644 index 0000000..38da7c1 --- /dev/null +++ b/webAO/utils/calculateWebpLength.js @@ -0,0 +1,22 @@ +const calculateWebpLength = (webpFile) => { + let d = new Uint8Array(webpFile); + // https://developers.google.com/speed/webp/docs/riff_container#animation + let duration = 0; + for (var i = 0; i < d.length; i++) { + // Find ANMF header (41 4E 4D 46) + if (d[i] === 0x41 + && d[i + 1] === 0x4E + && d[i + 2] === 0x4D + && d[i + 3] === 0x46) { + // Swap 5th and 6th bytes to get the delay per frame + let delay = (d[i + 21] << 8) | (d[i + 20] & 0xFF); + + // Should be aware browsers have a minimum frame delay + // e.g. 6ms for IE, 2ms modern browsers (50fps) + duration += delay < 2 ? 10 : delay; + } + } + return duration; +} + +export default calculateWebpLength \ No newline at end of file -- cgit