Today, let’s briefly talk about how to get the first frame of a video file in Android as a thumbnail to display it on the interface.
Previously, I said that I recently needed to download video files from the server, but after downloading, I definitely need to display the thumbnail of the video for users to see on the interface, so I thought of displaying the first frame of the video as a thumbnail. But I didn't know how to write it specifically, so I searched for information online and finally solved the problem. Let’s record it here.
1. Use MediaMetadataRetriever to obtain the first frame of the video as a thumbnail
/** * Get screenshot of video file * * @param path The path of the video file * @return Bitmap Returns the obtained Bitmap */ public static Bitmap getVideoThumb(String path) { MediaMetadataRetriever media = new MediaMetadataRetriever(); (path); return (); }
One thing to note here is that starting from API 10, a new type of MediaMetadataRetriever can be used to obtain media file information and can obtain thumbnails of any frame of the video. So the minimum API using MediaMetadataRetriever is 10.
Here are some other functions for MediaMetadataRetriever to obtain video:
//Get the original size picture of the first frame(); //Get the original size picture of the specified location. Note that the timeUs passed here is microseconds(timeUs, option); //Get a thumbnail for the specified position and height(timeUs, MediaMetadataRetrieverCompat.OPTION_CLOSEST, width, height); //Get a thumbnail for the specified position, specified width and height and rotation(timeUs, MediaMetadataRetrieverCompat.OPTION_CLOSEST, width, height, rotate);
Let me briefly talk about it here. () is actually called (-1, OPTION_CLOSEST_SYNC); that is, to obtain the keyframe at the nearest position after -1 second, which is actually the first frame of data. Let’s briefly talk about public Bitmap getFrameAtTime(long timeUs, int option):
public Bitmap getFrameAtTime(long timeUs, int option) The first parameter is the incoming time, which can only be us (microseconds). At that time, the ms I passed in were always the first frame, so this problem has been solved for a long time.
Then there is the second parameter, let’s take a look at the official explanation:
OPTION_CLOSEST At a given time, the most recent frame is retrieved, and this frame is not necessarily a keyframe.
OPTION_CLOSEST_SYNC At a given time, retrieve the most recent frame (keyframe) associated with the data source.
OPTION_NEXT_SYNCRetrieve a synchronous keyframe associated with the data source after a given time.
OPTION_PREVIOUS_SYNCAs the name implies, the same as above
2. Use ThumbnailUtils to get the first frame of the video as a thumbnail
/** * Get a thumbnail of the video * First, use ThumbnailUtils to create a thumbnail of a video, and then use ThumbnailUtils to generate a thumbnail of a specified size. * If the width and height of the desired thumbnail are smaller than MICRO_KIND, then the type should use MICRO_KIND as the value of kind, which will save memory. * @param videoPath Video path * @param width Specifies the width of the output video thumbnail * @param height Specifies the height of the output video thumbnail * @param kind Refer to (Video).Constants MINI_KIND and MICRO_KIND in the Thumbnails class. * Where, MINI_KIND: 512 x 384, MICRO_KIND: 96 x 96 * @return Video thumbnail of specified size */ public static Bitmap getVideoThumbnail(String videoPath, int width, int height,int kind) { Bitmap bitmap = null; // Get a thumbnail of the video bitmap = (videoPath, kind); //Click the static method of ThumbnailUtils to createVideoThumbnail to get the screenshot of the video; if(bitmap!= null){ bitmap = (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);//Click the static method extractThumbnailUtils of ThumbnailUtils to convert the original image (i.e. the image caught above) to the specified size; } return bitmap; }
To put it simply, this is actually very simple. It is to call (path, kind) to obtain the first frame of data, and then convert the picture to the specified size through bitmap = (bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
The following also lists how to save bitmap to a file, because after obtaining the thumbnail of the video, it may need to be saved locally. You can view it directly when entering the app next time.
/** * Save Bitmap to 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(() + name + ".jpg"); if (()) (); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); (, 100, fOut); (); (); } catch (IOException e) { return null; } return (); }
android gets the first frame of the video as a thumbnail and finishes it.
It's that simple. The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.