Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Create Video Thumbnail at specific time

I already got it to create a thumbnail from my video. The code looks like this:

videoGalleryThumbnails.add(ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail(
                    videoFile.getAbsolutePath(), MediaStore.Images.Thumbnails.MINI_KIND), 500, 200));

But the thumbnail created is at a really bad time. It is exactly when the video is black. Now i have no use of a completely black Thumbnail.

How can i take a Thumbnail of my video at a specific time? E.g. at 00:31 or at 01:44?

Or is it not possible?

I tried also to use MediaMetadataRetriever, but i get only a white image. Code looks like this

File tempVideoList[] = (Environment.getExternalStoragePublicDirectory(PATH_VIDEO_GALLERY))
            .listFiles();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap myBitmap=null;
for (File videoFile : tempVideoList) {
    if (videoFile.isFile()) {
        //from here
        try {
            retriever.setDataSource(videoFile.getAbsolutePath());
            myBitmap = retriever.getFrameAtTime(11); //at 11th second
        } catch (Exception ex) {
            Log.i("MyDebugCode", "MediaMetadataRetriever got exception:" + ex);
        }

    videoGalleryThumbnails.add(myBitmap);
    //to here
}

If i replace the code marked as "from here" to "to here" with the top first code, it works. I also tried MediaMetadataRetriever.OPTION_CLOSEST and OPTION_CLOSEST_SYNC and OPTION_NEXT_SYNC.

like image 433
qweret Avatar asked Mar 11 '14 17:03

qweret


1 Answers

Ok i got it. The MediaMetadataRetriever was absolutely the right way to go. The problem is, that it counts the time in microseconds and not in seconds. Solution looks like this:

try {
        retriever.setDataSource(videoFile.getAbsolutePath());
        int timeInSeconds = 30;
        myBitmap = retriever.getFrameAtTime(timeInSeconds * 1000000,
                    MediaMetadataRetriever.OPTION_CLOSEST_SYNC); 
    } catch (Exception ex) {
        Log.i("MyDebugCode", "MediaMetadataRetriever got exception:" + ex);
    }

videoGalleryThumbnails.add(myBitmap);

I don't know, if OPTION_CLOSEST_SYNC is actually needed, but it looks like this is the better way for programming.

Thanks to William Riley for pointing me in the right direction.

like image 194
qweret Avatar answered Nov 12 '22 12:11

qweret