UHS
UHS

Reputation: 739

How to convert animated webp to gif in android / java?

I want to convert the animated webp files to gif file and looking for solution since couple of days but did not get any easy straight solution yet.

I tried ffmpeg but it could not do the job and after looking for solution I found that webp decoder is not implemented in ffmpeg yet. https://trac.ffmpeg.org/ticket/4907

I also checked libwebp library by google and they also says animated webp is not supported. https://developers.google.com/speed/webp/docs/dwebp

I tried demo of aspose this library also fail to convert animated webp to gif. https://products.aspose.com/imaging/java/conversion/webp-to-gif/

I think ImageMagick can convert animated webp to gif but do not know how to implement that in my project. How can I avoid dropouts when using (imagemagick) `mogrify` to convert webp files to animated gif?

So is there any easy way to convert webp to gif

Upvotes: 2

Views: 1553

Answers (1)

CrackerKSR
CrackerKSR

Reputation: 1917

Decode gif using gif encoder and then encode into webp using webp encoder. You have to iterate through each frame for encodeding and decoding.

GifDecoder gifDecoder = new GifDecoder();
boolean isSucceeded = gifDecoder.load(src_file);
if (isSucceeded){
 for (int i = 0; i < gifDecoder.frameNum(); ++i) {
     Bitmap bitmap = gifDecoder.frame(i);
     // push bitmaps into arrary
     // push gifDecoder.delay into arryay for encoding
     // you can also resize each frame
 }
}

WebpBitmapEncoder webpEncoder = new WebpBitmapEncoder(dest_file)                
for (int i = 0; i < bitmap.length; ++i) {
    // gifEncoder.encodeFrame(bitmap[i], delays[i]);
    webpEncoder.setDuration(delays[i]);
    webpEncoder.writeFrame(bitmap[i], 80);
}); 

webp (animated) encoder: https://github.com/b4rtaz/android-webp-encoder

gif encoder decoder:

implementation ('io.github.waynejo:androidndkgif:1.0.1')

Upvotes: 1

Related Questions