2

I have a list of mp4 files that i need to extract a thumbnail for each one.

Thumbnail criteria:

  1. The thumbnail must be in Base64 format

  2. The thumbnail has a specific size which will be provided as a method parameter

  3. It must be extracted from the frame in the middle of the file (e.g. if the video duration is 10s then the thumbnail must be from the frame in 5th second.

1 and 2 are currently achieved but I'm not sure how to do 3.

This is my code:

public static String getVideoDrawable(String path, int height, int width) throws OutOfMemoryError{

    try {
        Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, android.provider.MediaStore.Images.Thumbnails.MINI_KIND);
        bitmap = Bitmap.createScaledBitmap(bitmap, height, width, false);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();

        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
man-r
  • 208
  • 3
  • 14

1 Answers1

2

You need to use the MediaMetadataRetriever for that.

MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
    try {
        metadataRetriever.setDataSource(path);
        String duration=metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long time=Long.valueOf(duration)/2;
        Bitmap bitmap = metadataRetriever.getFrameAtTime(time,MediaMetadataRetriever.OPTION_NEXT_SYNC);
    //now convert to base64
    } catch (Exception ex) {
    }

http://developer.android.com/intl/es/reference/android/media/MediaMetadataRetriever.html#getFrameAtTime%28long,%20int%29

isma3l
  • 3,833
  • 1
  • 22
  • 28