Android 将 gif 转换为动画 webp

Posted

技术标签:

【中文标题】Android 将 gif 转换为动画 webp【英文标题】:Android convert gif to animated webp 【发布时间】:2021-06-19 12:56:20 【问题描述】:

我尝试了很多方法将 gif 文件转换为动画 webp 文件,但都不起作用。

我首先从 webp/png 文件中创建了一个 gif 并将其加载到文件中以将其保存为 webp:

        //Bitmap from  png / webp
        Bitmap bmpAnimGif = BitmapFactory.decodeFile(String.valueOf(file));

        //Converting it into gif
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(bos);
        encoder.addFrame(bmpAnimGif);
        encoder.finish();
        byte[] array = bos.toByteArray();

        // Save to file (gif)
        File output = new File(StickerPackActivity.BASE_PATH  + "/" + "temp_animated.gif");
        FileOutputStream fos = new FileOutputStream(output);
        fos.write(array);
        fos.close();

        //load gif and saved it as webp

        File output2 = new File(StickerPackActivity.BASE_PATH  + "/" + "temp_animatedwebp.webp");
        fOut = new FileOutputStream(output2);
        compressImage(BitmapFactory.decodeFile(String.valueOf(output)), false).compress(Bitmap.CompressFormat.WEBP_LOSSY, 80, fOut);
        fOut.flush();
        fOut.close();

我猜最后一步是错误的... 如果你能帮我解决我的问题就好了。

【问题讨论】:

【参考方案1】:

要将动画 Gif 转换为动画 WEBP,请使用 This :) 快乐编码!!

例如:WebpIO.create().toWEBP("hello.gif", "hello.webp");

【讨论】:

如何在build.gradle中安装? 只需添加实现 'com.arthenica:ffmpeg-kit-full:4.4.LTS'【参考方案2】:

我能够使用以下方法将 gif 转换为动画 webp:

   implementation 'com.arthenica:ffmpeg-kit-full:4.4.LTS'

FFmpegSession session = FFmpegKit.execute("-i input_path/input.gif -vcodec webp -loop 0 -pix_fmt yuv420p output_path/output.webp");

    
    if (ReturnCode.isSuccess(session.getReturnCode())) 

        tvFilePath.setText("success");
        // SUCCESS

     else if (ReturnCode.isCancel(session.getReturnCode())) 

        // CANCEL

     else 

        // FAILURE
        Log.d("ffmpeg", String.format("Command failed with state %s and rc %s.%s", session.getState(), session.getReturnCode(), session.getFailStackTrace()));

    

【讨论】:

使应用程序的原始大小变为原来的 10 倍,:/ 即使可以使用也无法使用 是的,FFMPEG 库会增加大小,但如果您使用 AAB,应用程序下载大小会更小。我正在使用这个库,我的 AAB 大小为 68 MB,但是当我从 Play 商店下载应用程序时它的 20 MB。 我无法导入。使用此实现构建 gradle 没有错误,但在导入时说该包不存在。我什至尝试使用该 maven repo 中的 jar 文件

以上是关于Android 将 gif 转换为动画 webp的主要内容,如果未能解决你的问题,请参考以下文章

如何在xamarin android中将位图图像转换为gif?

如何将多个 PNG 文件转换为动画 GIF? [关闭]

将动画 .GIF 设置为背景 Android

将精灵表转换为 gif 动画

将每个动画 GIF 帧转换为单独的 BufferedImage

使用 Python 将 MP4视频 转换为GIF动画