2

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();
        }
 }
UHS
  • 584
  • 4
  • 8
  • 1
    A Bitmap is a single image (or a single animation frame). It's not possible to store an animated WebP with multiple frames like this... – Harald K Jan 03 '22 at 12:10
  • See also https://stackoverflow.com/questions/56276512/animated-webp-enecoder-for-android – Harald K Jan 03 '22 at 12:10
  • Thanks for confirming that. I discovered it later by retrieving frames and duration and it was showing 1 frame 0 duration. I was thinking does bitmap contains a single frame only of any image. I solved it by changing the way of storing the image. I used outputstream and input stream. will post the answer. – UHS Jan 03 '22 at 14:31
  • Did you manage to compress WEBP files? – ᴜsᴇʀ May 13 '22 at 12:50

2 Answers2

2

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;

0

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)

CrackerKSR
  • 1,380
  • 1
  • 11
  • 29