androidencodingbitmapandroid-canvasandroid-bitmap

Efficiently converting an array of bitmaps to video


Using the Jcodec library, I had success producing and storing a video file on the Android file system from an array of Bitmap objects.

However, the encoding time takes way too long. I thought about scaling down each of the bitmaps to speed this up but this did not seem to work. Despite researching extensively, I struggled to find a helpful answer.

The below code is currently how I'm creating a bitmap, Where canvasView.getWidth() and canvasView.getHeight() correspond to 1920 x 1280 respectively.

public Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap(canvasView.getWidth(), canvasView.getHeight(), Bitmap.Config.RGB_565);
    Log.i("Downloading", "OWidth: " + b.getWidth() + " --- " + "OHeight: " +  b.getHeight());

    Canvas c = new Canvas(b);
    c.drawColor(Color.WHITE);   // Essential

    v.draw(c);
    v.invalidate();
    v.requestLayout();

    return b;
}

I noticed when you change these two parameters to something smaller, IE:

Bitmap.createBitmap(256, 256, Bitmap.Config.RGB_565);

The encoding speeds up dramatically.

The size of the entire view is 1920 x 1280, so the createBitmap method shown above would only capture a small portion of the window (256 x 256), which is not what I require.

I need help finding a way to more efficiently encode an array of bitmaps to video or use the same method I am using but scaling down each bitmap while maintaining the entire image's visibility and speeding up the encoding process.


Solution

  • You can use FFmpeg to speed up your process of making video from images or bitmap

    ffmpeg command:-

            File dir = your directory where image stores;
            String filePrefix = "picture"; //imagename prefix
            String fileExtn = ".jpg";//image extention
            filePath = dir.getAbsolutePath();
            File src = new File(dir, filePrefix + "%03d" + fileExtn);// image name should ne picture001, picture002,picture003 soon  ffmpeg takes as input valid
    
    
    
    complexCommand = new String[]{"-i", src + "", "-c:v", "libx264", "-c:a", "aac", "-vf", "setpts=2*PTS", "-pix_fmt", "yuv420p", "-crf", "10", "-r", "15", "-shortest", "-y", "/storage/emulated/0/" + app_name + "/Video/" + app_name + "_Video" + number + ".mp4"};
    

    here src = directory path which contains multiple images

    you ca use writingminds library fro easily integrate ffmpeg in your project

    https://github.com/WritingMinds/ffmpeg-android-java

    but ffmpeg increase apk size approx 18-20 mb