【问题标题】:Android convert gif to animated webpAndroid 将 gif 转换为动画 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();

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

【问题讨论】:

    标签: android gif converters webp


    【解决方案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 文件
    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2019-11-24
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多