Get video thumbnail from url of video

In android, we can use the MediaMetadataRetriever to retrieve a thumbnail image of an online video from its url. MediaMetadataRetriever can be used to retrieve frame and metadata from an input media file.

See example below:

String url = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4";
// Create a MediaMetaDataRetriever
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// Set video url as data source
retriever.setDataSource(url, new HashMap<String, String>());
// Get frame at 2nd second as Bitmap image
Bitmap bitmap = retriever.getFrameAtTime(2000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
// Display the Bitmap image in an ImageView
imageview3.setImageBitmap(bitmap);

Note that the above code takes some time to get the image. The code when used on main UI thread may hang the app till the time the image is retrieved. And it may not be a good idea to use the code for retrieving multiple thumbnails in a list.

Leave a Reply