0

Is there a way to detect if an AVIF image is animated using JavaScript?

Absolutely no frameworks or libraries.

John
  • 1
  • 13
  • 98
  • 177

1 Answers1

1

The new ImageDecoder API can tell this to you.

You'd pass a ReadableStream of your data to it, and then check if one of the decoder's tracks has its animated metadata set to true:

if (!window.ImageDecoder) {
  console.warn("Your browser doesn't support the ImageDecoder API yet, we'd need to load a library");
}
// from https://colinbendell.github.io/webperf/animated-gif-decode/avif.html
fetch("https://colinbendell.github.io/webperf/animated-gif-decode/6.avif").then((resp) => test("animated", resp.body));
// from https://github.com/link-u/avif-sample-images cc-by-sa 4.0 Kaede Fujisaki
fetch("https://raw.githubusercontent.com/link-u/avif-sample-images/master/fox.profile1.8bpc.yuv444.avif").then((resp) => test("static", resp.body));

document.querySelector("input").onchange = ({target}) => test("your image", target.files[0].stream());


async function test(name, stream) {
  const decoder = new ImageDecoder({ data: stream, type: "image/avif" });
  // wait for we have some metadata
  await decoder.tracks.ready;
  // log if one of the tracks is animated
  console.log(name, [...decoder.tracks].some((track) => track.animated));
}
<input type=file>

However beware this API is still not widely supported, since only Chromium based browsers have an implementation currently.

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • I tested this in Chrome and it seems to work. However I'm going to need a polyfill as I specifically need this for Safari 16. Suggestions on a polyfill please? – John Nov 03 '22 at 09:12
  • 1
    Not really no. There is https://github.com/YuriTu/avic.js that apparently can decode it, so it shouldn't be too hard to fork it so that it only outputs the info, but I'm not sure either. – Kaiido Nov 03 '22 at 09:21
  • Thank you, that looks like a several hour long project so I'll have to sift through that tomorrow night. If it supports Chrome 57 I think there is a fair chance it should work in Safari 16. I'll let you know how it works out, thank you! – John Nov 03 '22 at 09:30
  • So I've decided to abandon detecting Safari 16.1 the proper way because there is no *sane* proper way, they screwed up on the release. I'll accept the answer because it's valid in it's own right even though it doesn't help in the context that I needed it for. Thank you nonetheless. – John Nov 05 '22 at 03:10
  • Note that the current Technology Preview does support animated avif. Also, it seems that an error event is fired on the HTMLImageElement in currwnt stable when it tries to load such a file. – Kaiido Nov 05 '22 at 06:06
  • I'd be willing to award a full 500 point bounty for a completely self-contained script that would `return true` when detecting animated AVIF in Safari 16.1 and `return false` in Safari 16.0. If you're willing to answer it in that context I'll go make the question and post the URL in my next comment. Are you are interested? – John Nov 05 '22 at 08:34