I am using Lottie Animation Library and I want to record that animation view as video but this library does not have any video exporting support so I am trying to find my own solution and,
This is what I have tried so far.
animationView.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
if((animationView.getFrame()) != previous ){
// getting single frame
previous = animationView.getFrame();
FrameLayout view = frameLayout;
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmaps.add(bitmap);
}
}
});
So above code addAnimatorUpdateListener
runs every time when the animation is being updated so this function store all the bitmaps in the array.
for(Bitmap bitmap:bitmaps){
OutputStream stream;
try {
counter++;
stream = new FileOutputStream(imageFolder.getAbsolutePath()+"/test-"+counter+".png");
bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
Log.i(tag, "onAnimationEnd: " + "converting... " + String.valueOf(counter) + "of " + String.valueOf(animationView.getMaxFrame()));
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(tag, "onAnimationEnd: " + e.getMessage());
}
}
When the animation ends Than above code convert all the images from the bitmaps array and stores them in the temporary folder. and then I am using FFmpeg-Android to convert the video from the images in mp4.
FFmpeg command
String[] fcommand = new String[]{"-i", path + "test-%1d.png", "-c:v", "libx264", "-vf", "fps=30", "-pix_fmt", "yuv420p", path + String.valueOf(new Date()) +".mp4"};
From this command, I am generating video at 30 FPS.
Issues that I am facing right now
- Video is not so smooth even at 30 fps (420p).
(Bitmap.createBitmap(view.getDrawingCache());
30 times in one second? is it possible?
TL;DR
I just want to capture the bitmaps of a specific view at 30 fps how I can achieve this?