How to Create a Video from an Array of Images in Android

How to create a video from an array of images in Android?

You can use jcodec SequenceEncoder to convert sequence of images to MP4 file.

Sample code :

import org.jcodec.api.awt.SequenceEncoder;
SequenceEncoder enc = new SequenceEncoder(new File("filename"));
// GOP size will be supported in 0.2
// enc.getEncoder().setKeyInterval(25);
for(...) {
BufferedImage image = ... // Obtain an image to encode

It's a java library so it's easy to import it into Android project, you don't have to use NDK unlike ffmpeg.

Refer for sample code & downloads.

How to make a video from a list of images?

Try using animation set in android that can help you achieve what you are after it is called FrameAnimation, here is an example on how to use it:

FrameAnimation Example

or checkout below code snippet if it helps :

` final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
int randomNum = random.nextInt(6);
handler.postDelayed(this, 500);
}, 500);`

Efficiently converting an array of bitmaps to video

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

but ffmpeg increase apk size approx 18-20 mb

Android: How to make video from set of images

Android does not provide any built-in APIs which would allow to create video files from single frames. Consider using Sony Vegas or Adobe After Effects instead.

However you can emulate the video effect in an ImageView (not really a viable solution indeed, but would do in some simple cases). For that you would need to create an array containing your frames and loop through the array using a CountDownTimer or similar. Setting the next frame in the ImageView with of a tick rate of 40ms would correspond to a frame rate of 25 FPS. You could also provide background sounds using MediaPlayer or SoundPool.

Related Topics

Leave a reply