'Dart: How to Encode Decode animated webp files?
I am trying to decode animated webp image file and encode it's frames to animated webp image file in Dart (cli)
Dart Code:
import 'dart:io';
import 'package:image/image.dart';
void main() {
final image = 'asset/test.webp';
File bytes = File(image);
List<int> list = bytes.readAsBytesSync();
var anim = decodeWebPAnimation(list)!;
var obj = WebPEncoder();
for (var frame in anim) {
obj.addFrame(frame, duration: 1);
}
var finished = obj.finish();
print(finished); // [] empty, no frames get added
// final encodedAnim = obj.encodeAnimation();
File('asset/output.webp').writeAsBytesSync(finished!,
flush: true); // pass something like encodeWebp(input)
}
pubspec.yaml
dependencies:
image: ^3.1.3
input file: https://raw.githubusercontent.com/WhatsApp/stickers/main/Android/app/src/main/assets/2/07_OK.webp
Output:
There is zero frame after calling finish()
, so the output image file is invalid. i.e. file size: 0 byte
Ref of Dart APIs:
WebpEncoder: https://pub.dev/documentation/image/latest/image/WebPEncoder-class.html
DecodeWebpAnimation: https://pub.dev/documentation/image/latest/image/decodeWebPAnimation.html
What went wrong? How to fix this? Thank You!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|