Android screen recording using FFMPEG and Java OpenCV

Think of the feature where activities you perform on mobile app screen is recorded as a video. Screen video recording can be useful in drawing kind of apps where you are recording video of your drawing as you progress.
Android SDK comes with many powerful features to implement wide variety of the apps starting from utility apps to enterprise apps. But sometimes it really becomes cumbersome to implement desired feature due to limitation of android OS. Screen video recording feature in the android app is one of them. Android OS has restricted access to mobile screen through any app. There are few applications over Google play store which provide screen video recording feature. But they demand for root access on your device which means your android device has to be rooted to use such applications. If you are developing app for general users then rooting of the device can’t be solution just to have screen video recording feature as not everybody likes to play with their phones by rooting it.

Kitkat 4.4 built in screen video recording feature

Kitkat came with in built screen recording feature using ADB(Android Debug Bridge). But it does not serve above mentioned requirement as screen recording is done from the command line. This can be useful when you only have to create screen recording video for complete application to show how does app work.

Libraries to create video from images in JAVA/Android

FFMPEG is library which can be integrated in android app using android NDK(native development kit). It needs working android NDK copy to build this. However you can still find already built files which can be imported in you android app source code without NDK using java OpenCV library. Java openCV library is a wrapper for FFMPEG written in java. Java OpenCV can be simplest way to integrate FFMPEG to create video from sequence of images in android app.
You can download libraries needed from http://search.maven.org/remotecontent?filepath=org/bytedeco/javacv/0.8/javacv-0.8-bin.zip. You can always look on web for latest version of these jars. Once you download zip from above location, you need to copy javacpp.jar, javacv.jar to your project’s libs folder(project folder/libs). Another required jar is opencv-android-arm.jar. Extract opencv-android-arm.jar to get armeabi folder. Copy this armeabi to your project’s libs folder(project folder/libs).

Creating video from sequence of images stored on SD card

Once you import all required jars into your project. You are all set to start coding to create video from sequence of images. You can find pieces of codes as follows - You need import following packages.

import com.googlecode.javacv.FFmpegFrameRecorder;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

Following function will create video from list of images. All one has to do is to provide path of images on SD Card.

private void record() {

        Log.d("Record", "Environment.getExternalStorageDirectory().getPath()  : " + Environment.getExternalStorageDirectory().getPath());
        String path = Environment.getExternalStorageDirectory().getPath() + "/Video_images"; // You can provide SD Card path here.

        File folder = new File(path);

        File[] listOfFiles = folder.listFiles();
        IplImage[] iplimage = null;
        if (listOfFiles.length > 0) {

            iplimage = new opencv_core.IplImage[listOfFiles.length];

            for (int j = 0; j < listOfFiles.length; j++) {

                String files = "";

                if (listOfFiles[j].isFile()) {
                    files = listOfFiles[j].getName();
                    System.out.println(" j " + j + listOfFiles[j]);
                }

                String[] tokens = files.split("\\.(?=[^\\.]+$)");
                String name = tokens[0];

                Toast.makeText(getBaseContext(), "size" + listOfFiles.length, Toast.LENGTH_SHORT).show();

                iplimage[j] = cvLoadImage(Environment.getExternalStorageDirectory().getPath() + "/Video_images/" + name + ".jpg");

            }

        }

        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(Environment.getExternalStorageDirectory().getPath() + "/Video_images/output" + System.currentTimeMillis() + ".mp4", 200, 150);

        try {
            recorder.setVideoCodec(13); // CODEC_ID_MPEG4 //CODEC_ID_MPEG1VIDEO
                                        // //http://stackoverflow.com/questions/14125758/javacv-ffmpegframerecorder-properties-explanation-needed

            recorder.setFrameRate(1); // This is the frame rate for video. If you really want to have good video quality you need to provide large set of images.
            recorder.setPixelFormat(0); // PIX_FMT_YUV420P

            recorder.start();

            for (int i = 0; i < iplimage.length; i++) {

                recorder.record(iplimage[i]);

            }
            recorder.stop();
            Toast.makeText(MainActivity.this, "Record Completed", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Above code creates video from images stored on SD card in Video_images folder. It creates video and stores created video on SD Card in folder- /Video_images/output/ with video name as current time-stamp with mp4 extension. In above code you can choose your own frame rate using following line -

recorder.setFrameRate(1);

If you have large number of images to create video then you can set higher frame rate. But drawback of this method is this method takes time to create video and save it on SD Card.

Using above method in drawing like app to create video of screen drawing

If you want to use above method in drawing like app to capture drawing video best way would be to capture images of parent layout per second and store on SD card. You can initiate this process when user clicks on start recording button. When user clicks on stop recording, you can stop capturing images and call above method providing path of your temporary folder where you saved captured images.
Hope this helps you to create video of screen recording without rooting your device!