Sharp: Animated GIF to WEBP using a Sharp stream is not animated anymore

Created on 9 Sep 2020  路  6Comments  路  Source: lovell/sharp

The installation went well, and I managed to make it work with a file or an URL for the input and a file for the output, but i don't manage to find a way to get it animated when using a stream for the output.

@deftomat Sorry for bothering you, but I think you might have a quick answer 馃懠

Are you using the latest version? Is the version currently in use as reported by npm ls sharp the same as the latest version as reported by npm view sharp dist-tags.latest?
Yes

What are the steps to reproduce?

const app = new Koa();
const router = new Router();
const agent = require('superagent');
const sharp = require('sharp');

router.get('/:spec/i.:extension', ctx => {
  let sharpStream = sharp() // WORKING BUT NOT ANIMATED
  let sharpStream = sharp({ animated: true }) // Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
  let sharpStream = sharp({ pages: -1 }) // Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
  let sharpStream = sharp(undefined, { pages: -1 }) // Error: Unsupported input 'undefined' of type undefined when also providing options of type object
  let sharpStream = sharp(undefined, { animated: true }) // Error: Unsupported input 'undefined' of type undefined when also providing options of type object

  sharpStream = sharpStream.toFormat('webp', {
    quality,
    pages: -1, // doesn't change anything
    animated: true, // doesn't change anything
  });

  ctx.set('Content-Type', 'image/webp');
  ctx.body = agent.get(url).pipe(sharpStream);
  ctx.status = 200;
});

What is the expected behaviour?
The output stream webp image should be animated.

Are you able to provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem?
Kind of, please see below.

Are you able to provide a sample image that helps explain the problem?
I used this image.

What is the output of running npx envinfo --binaries --system?

  System:
    OS: macOS 10.15.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 12.02 GB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 10.22.0 - ~/.nvm/versions/node/v10.22.0/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v10.22.0/bin/npm
bug ready-to-ship

Most helpful comment

v0.26.1 now available, thank you for fixing this @AcrylicShrimp

All 6 comments

Thanks for the report. I think the problem lies here:

https://github.com/lovell/sharp/blob/76dcddfa3d6042b6a23239cf0e139ce93bf53ab5/lib/input.js#L11-L16

Fixing this will probably involve something like:

 function _inputOptionsFromObject (obj) {
-  const { raw, density, limitInputPixels, sequentialRead, failOnError } = obj;
-  return [raw, density, limitInputPixels, sequentialRead, failOnError].some(is.defined)
+  const { raw, density, limitInputPixels, sequentialRead, failOnError, animated } = obj;
+  return [raw, density, limitInputPixels, sequentialRead, failOnError, animated].some(is.defined)

Happy to accept a PR, ideally with a test case for this, if you're able.

Hi, we are still using our custom fork as we didn't upgrade to the latest sharp yet and we are using streams quite extensively with animated files with no issues. So, it is probably caused by the code mentioned by @lovell

It's been reported in #2368 that page and pages will need adding here too.

@lovell Could you explain the purpose of the _inputOptionsFromObject function? I'm interested in PR, but I don't have any idea why it not handles level and create too. Is it intended or bug? Thank you.

Can I ask when will 0.26.1 be released?

v0.26.1 now available, thank you for fixing this @AcrylicShrimp

Was this page helpful?
0 / 5 - 0 ratings