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

后端 未结 9 816
时光说笑
时光说笑 2020-12-04 10:32

I want to call a function and build a video out of list of images, and then save it locally on the device:

public void CreateAndSaveVideoFile(List

        
9条回答
  •  有刺的猬
    2020-12-04 11:33

    jCodec has added Android support.

    You need to add these to your gradle...

    implementation 'org.jcodec:jcodec:0.2.3'
    implementation 'org.jcodec:jcodec-android:0.2.3'
    

    ...and

    android {
        ...
        configurations.all {
            resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
        }
    }
    

    I can confirm this works as expected, but with caveats. First being I tried some full size images and the file wrote, but gave an error on playback. When I scaled down, I would get an error if the width or height of the image was not even because it requires a multiple of 2 for YUV420J colorspace.

    Also worthy of note, this makes your package HEAVY, heavy. My small project went over the dex limit by adding this and required enabling multidex.

    FileChannelWrapper out = null;
    File dir = what ever directory you use...
    File file = new File(dir, "test.mp4");
    
    try { out = NIOUtils.writableFileChannel(file.getAbsolutePath());
          AndroidSequenceEncoder encoder = new AndroidSequenceEncoder(out, Rational.R(15, 1));
          for (Bitmap bitmap : bitmaps) {
              encoder.encodeImage(bitmap);
          }
          encoder.finish();
    } finally {
        NIOUtils.closeQuietly(out);
    }
    

提交回复
热议问题