android Method for Obtaining First Video Frame as Thumbnail

  • 2021-11-10 10:51:51
  • OfStack

Today, let's talk briefly about how to get the first frame of a video file and display it on the interface as a thumbnail in android.

Before, I said that I need to download video files from the server recently, but after downloading, I definitely need to display thumbnails of the video on the interface for users to see, so I thought of displaying the first frame of the video as thumbnails. But I don't know how to write it, so I searched for information on the Internet and finally solved the problem. Record 1 here.

1. Use MediaMetadataRetriever to get the first frame of the video as a thumbnail


/** 
*  Get screenshots of video files  
* 
* @param path  Path of video file  
* @return Bitmap  Returns the Bitmap 
*/
 
public static Bitmap getVideoThumb(String path) { 
 
MediaMetadataRetriever media = new MediaMetadataRetriever(); 
 
media.setDataSource(path); 
 
return media.getFrameAtTime(); 
 
} 

One point should be noted here. Starting from API 10, a new class of MediaMetadataRetriever can be used to obtain the information of media files, and can obtain thumbnails of any 1 frame of video. Therefore, the minimum API using MediaMetadataRetriever is 10.

Here are several other functions of MediaMetadataRetriever to obtain video:


// Get the 1 Frame original size picture 
mmrc.getFrameAtTime();
 
// Gets the original size picture at the specified position   Pay attention to the transmission here timeUs It's microseconds 
mmrc.getFrameAtTime(timeUs, option);
 
// Gets a thumbnail with a specified width and height at the specified position 
mmrc.getScaledFrameAtTime(timeUs, MediaMetadataRetrieverCompat.OPTION_CLOSEST, width, height);
 
// Gets a rotated thumbnail with a specified width and height at the specified position 
mmrc.getScaledFrameAtTime(timeUs, MediaMetadataRetrieverCompat.OPTION_CLOSEST, width, height, rotate);

Here, briefly, media. getFrameAtTime () is actually calling mmrc. getFrameAtTime (-1, OPTION_CLOSEST_SYNC); That is to say, the key frame of the nearest position after-1 second is actually the first frame data. Let's briefly talk about public Bitmap getFrameAtTime (long timeUs, int option):

The first parameter of public Bitmap getFrameAtTime (long timeUs, int option) is the incoming time, which can only be us (microseconds). At that time, when I passed ms, I always got the first frame, so this problem has been solved for a long time.

Then there is the second parameter. Let's look at the official explanation first:

OPTION_CLOSEST at a given time, retrieves the most recent frame, which is not a key frame.

OPTION_CLOSEST_SYNC retrieves the frame (key frame) associated with the data source for the last synchronization at a given time.

OPTION_NEXT_SYNC Retrieves 1 key frame that synchronizes with the data source after a given time.

OPTION_PREVIOUS_SYNC, as the name implies, as above

2. Use ThumbnailUtils to get the first frame of the video as a thumbnail


/**
   *  Get thumbnails of video 
   *  Pass first ThumbnailUtils To create 1 Thumbnails of videos, and then reuse ThumbnailUtils To generate thumbnails of the specified size. 
   *  If the width and height of the thumbnail you want are less than MICRO_KIND The type uses the MICRO_KIND As kind This saves memory. 
   * @param videoPath  Path of video 
   * @param width  Specifies the width of the output video thumbnail 
   * @param height  Specifies the height of the output video thumbnail 
   * @param kind  Reference MediaStore.Images(Video).Thumbnails Constants in the class MINI_KIND And MICRO_KIND . 
   *       Among them, MINI_KIND: 512 x 384 , MICRO_KIND: 96 x 96
   * @return  Video thumbnails of specified size 
   */
  public static Bitmap getVideoThumbnail(String videoPath, int width, int height,int kind) {
    Bitmap bitmap = null;
    //  Get thumbnails of video 
    bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); // Use ThumbnailUtils Method of createVideoThumbnail Take the cut; 
    if(bitmap!= null){
      bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
          ThumbnailUtils.OPTIONS_RECYCLE_INPUT);// Use ThumbnailUtils Method of extractThumbnail The original piece (that is, the piece intercepted from above) is sized to the specified size; 
    }
    return bitmap;
  }

To put it simply, this is actually very simple, that is, call ThumbnailUtils. createVideoThumbnail (path, kind) to obtain the first frame data, and then pass bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); Converts the picture to the specified size.

The following also lists how to save bitmap to a file, because after obtaining thumbnails of videos, you may need to save them locally, and you can view them directly when you enter app next time.


/** 
* Bitmap Save as File 
* 
* @param bitmap input bitmap 
* @param name output file's name 
* @return String output file's path 
*/
 
public static String bitmap2File(Bitmap bitmap, String name) { 
 
File f = new File(Environment.getExternalStorageDirectory() + name + ".jpg"); 
 
if (f.exists()) f.delete(); 
 
FileOutputStream fOut = null; 
 
try { 
 
fOut = new FileOutputStream(f); 
 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
 
fOut.flush(); 
 
fOut.close(); 
 
} catch (IOException e) { 
 
return null; 
 
} 
 
return f.getAbsolutePath(); 
 
}

android takes the first frame of the video as a thumbnail.


Related articles: