-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isAnimated
metadata property
#3984
Comments
Multi-page and animated images are treated as the same thing by libvips, so this logic would probably live in sharp itself and very closely represent the sample |
This sounds useful! I've used the is-animated package in the past for this detection but having it in sharp would be a great addition. Might be worth comparing their implementation (a cursory look at the code also indicates PNG can be animated too) Might be better off assuming all formats with multiple pages are animated besides PDF: const isAnimated = metadata.pages > 1 && metadata.format !== 'pdf' |
I benchmarked @lovell Is this expected? CodeView Code
const sharp = require('sharp');
const isAnimated = require('is-animated');
const { readdirSync, readFileSync } = require('fs');
const { join } = require('path');
const fmt = (name, start, end) => `${name}: ${(end - start).toFixed(3)}ms`;
for (const file of readdirSync('./test/fixtures', { withFileTypes: true })) {
if (file.isDirectory()) {
continue;
}
const buffer = readFileSync(join(file.parentPath, file.name));
const pkgStart = performance.now();
const pkgIsAnimated = isAnimated(buffer);
const pkgEnd = performance.now();
const sharpStart = performance.now();
sharp(buffer).metadata().then((meta) => {
const sharpIsAnimated = meta.pages > 1 && ['gif', 'png', 'webp', 'avif'].includes(meta.format);
const sharpEnd = performance.now();
console.log(`${fmt('sharp', sharpStart, sharpEnd)}, ${fmt('is-animated', pkgStart, pkgEnd)} (${file.name})`);
if (sharpIsAnimated !== pkgIsAnimated) {
console.error('mismatch', { filename: file.name, sharpIsAnimated, pkgIsAnimated });
}
}).catch(e => {
// consume error
});
} ResultsView Results
|
@styfle It looks like this sample code attempts to process everything all at once so will include wait times for libuv worker threads. Pop an
The The one thing that stands out from the sharp/libvips results is that images with CMYK profiles appear to take longer, which might suggest a possible improvement in libvips itself relating to parsing/verifying ICC profiles. |
I took a quick look at why JPEGs with a CMYK profile are slower... libvips tells libjpeg it wants to know about the APP2 marker (used for ICC profiles) here: This makes libjpeg copy the data from the marker byte-by-byte here: Testing via callgrind suggests this The only thing I can think of is to modify the libvips API to allow control over optionally ignoring APP2 (and maybe APP13/14) markers. |
This sounds like issue libjpeg-turbo/libjpeg-turbo#764, which will be fixed in the forthcoming libjpeg-turbo v3.0.4. |
Thanks Kleis, that would make sense as CMYK profiles are big enough to span multiple APP2 markers. The upstream libjpeg report describes millions of markers whereas there are only 18 in the |
Indeed, perhaps it's not the same issue. I wonder if we could avoid calling Note that these functions are mutually exclusive - if you call one it overrides any previous call to the other, for the particular marker type specified. |
Feature request
What are you trying to achieve?
I need to check whether or not an image is animated (eg animated gif or webp). This may already be possible by checking something like
metadata.pages > 1 && (metadata.format === 'gif' || metadata.format === 'webp')
, but there may be subtleties that I am not considering, over time other formats such as avif or jxl will come along with animation support too.Either way, it would be a nice developer experience win to have an easy accessible property for this instead of trying to infer it from the other metadata, is possible. Thoughts?
When you searched for similar feature requests, what did you find that might be related?
Can't quite find a feature request for this, specifically
What would you expect the API to look like?
What alternatives have you considered?
Manually checking the
pages
property and pairing with format checks, I suppose. PDFs are not animated, but does have pages - so it needs to be more than apages
check I believe?Please provide sample image(s) that help explain this feature
Shouldn't be necessary for this one.
The text was updated successfully, but these errors were encountered: