How to compress animated webp without converting it to static in java?

Viewed 259

It seems that Bitmap compressions is making animated WEBP files static or something else that when I use them as animated stickers files I get an error from WhatsApp that there is a problem with this sticker pack. That error does not occur if I use static webp by setting the animated _sticker flag to false. Then those same animated WEBP files get accepted by WhatsApp as a static sticker pack. After looking at all codes I stuck in the method where WEBP get compressed and saved. I should confirm if those WEBP files still contain multiple frames. How to check the frame count or check if the file is animated or static after compressed/saved?

public static void Save(Bitmap bitmap1, String name1, String folder) {
        
        File dir = new File(path + "/" + folder).mkdirs();
        
        File file = new File(dir, name1);
        
        if (file.exists())
            file.delete();
    
        
        try {
            FileOutputStream ops = new FileOutputStream(file);
            Bitmap1.compress(Bitmap.CompressFormat.WEBP, 90, ops); 
            ops.flush();
            ops.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 }
2 Answers

Do not use Bitmap.compress. It will store single frame. Rather you can store it without compression using

InputStream inputStream = new URL(sUrl).openStream();
copyInputStreamToFile(inputStream,file);

copyInputStreamToFile(): you have to define the method which will read inputstream in butes array and write to FileOutputStream;

Iterate through each frame and compress each frame using Bitmap.compress ass usual. Encode all bitmaps with into animated webp using any webp encoder which support aniamtion.

So how would you get frames and its delay? Webp Decoder can extract all frames as bitmaps and the delay for each frames.

so you need to [bitmaps, delays] = decode(file) compress (bitmaps) encode (bitmaps, delays)

There are few encoders decoders and transcoders. ImageMagick, ffmp, glide, etc

but I used APNG4Android. It support Gif and Webp both. Although I could not use it for decoding due to logic issue in my code. I used glide to decode animated webp.

If you can try to use any of them either glide or APNG4Android as transcoder(encoder+decoder)

Related