heic-convert icon indicating copy to clipboard operation
heic-convert copied to clipboard

5MB image take time almost 20 sec in AWS Lambda function

Open bhandurejitu opened this issue 2 years ago • 3 comments

I am trying to convert heic image into jpeg.

1 => take image from s3 bucket. 2 => convert image ( quality => 0.35) 3 => save converted image on s3 bucket.

it takes almost 20+ sec for the whole process.

is there any way to reduce this time?

bhandurejitu avatar Sep 01 '21 05:09 bhandurejitu

So let's break this down a little bit and hopefully this info will help:

First, it seems you are talking about timing an operation where only part of it is affected by this library. You are not really telling me whether this library is the problem, whether some other part of your function is the problem, or whether something about Lamba itself is a problem. I will really only attempt to help with issues using this library, as this is not the right forum for general software advice. Can you please make sure you are timing only using this library and provide that information?

Second, the size of the image alone is not super helpful. The size of the HEIC file itself and the size of the compressed content that is stored inside the HEIC can vary significantly based on many variables. The data that you are decoding is what is important. Would you be able to provide an example file that you are debugging this problem with?

Finally, this library is very likely solving a different problem than what you are solving. There are many ways to convert a HEIC image to another format. This library tries to solve the problem by converting the image in JavaScript, for users who are limited to using JavaScript. It does not try to solve the problem of converting it as fast as possible for users who do not have technology limitations. Since you are running in Lambda, you can use pretty much any tool. It might be best to find the tool that tries to solve the problem the fastest rather than a tool that is most convenient due to solving the problem with only JavaScript.

catdad avatar Sep 01 '21 11:09 catdad

Indeed, using a native tool to convert instead of JS is a good idea.

For example this command converts from HEIC to JPG: heif-convert sample1.heic sample1.jpg

In JS, you could do something like:

import { spawn } from 'child_process';

function convertHEIF(input, output) {
  return new Promise((resolve, reject) => {
    const cmd = spawn(
      'heif-convert',
      [input, output],
      { cwd: process.cwd() },
    );
    let hasFailed = false;
    cmd.stderr.on('data', (buffer) => {
      hasFailed = true;
      reject(new Error(buffer.toString()));
    });
    cmd.on('error', (error) => {
      hasFailed = true;
      reject(error);
    });
    cmd.on('close', (errCode) => {
      if (!hasFailed) {
        resolve(errCode);
      }
    });
  });
}

convertHEIF('sample1.heic', 'sample1.sample1.jpg')
  .then(() => {
    // conversion done
  })
  .catch((err) => {
    console.error(err.message);
  });

Of course you need to install the program (ex on Ubuntu): apt install libheif-examples

jalik avatar Oct 23 '21 04:10 jalik

@bhandurejitu I'm working on a Lambda function to convert heic images to jpeg. I got some troubles with the convention. Could you please post here your implementation code? I did try several ways, but it doesn't work at all.

youngvo avatar Dec 01 '23 08:12 youngvo