如何解决Android 将 gif 转换为动画 webp
我尝试了很多方法将 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();
我猜最后一步是错误的... 如果你能帮我解决我的问题就好了。
解决方法
将动画 Gif 转换为动画 WEBP 使用 This :) 快乐编码!!
例如:WebpIO.create().toWEBP("hello.gif","hello.webp");
,我能够使用以下方法将 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()));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。