32

I am working on a video app. I am streaming a video from server link , is it possible for me to generate a video thumbnail from the URL without downloading the video.

KishuDroid
  • 5,411
  • 4
  • 30
  • 47
DKV
  • 1,767
  • 3
  • 28
  • 49

9 Answers9

82

Without downloading video you can generate thumbnail from below code:

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable
{
    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try
    {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
   //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

NOTE : Video is stored as Intra and non Intra (Picture frames) getFrameAtTime will return the closest non- Intra frame as Bitmap. So basically it won't download the entire video.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • @OmarRehman: Your welcome. I have checked it and didn't get any delay. – KishuDroid Feb 08 '16 at 05:27
  • Any idea if this downloads the entire video? – lostintranslation Feb 24 '16 at 03:57
  • @lostintranslation: Can you please elaborate what you want to say? – KishuDroid Feb 24 '16 at 03:59
  • If getFrameAtTime() downloads the entire video it defeats the purpose a little. I want to know if that method downloads the entire video. If so I would rather just download the video. – lostintranslation Feb 24 '16 at 04:03
  • 1
    @lostintranslation Video is stored as Intra and non Intra (Picture frames) getFrameAtTime will return the closest non- Intra frame as Bitmap. So basically it won't download the entire video. Hope you got your answer. – KishuDroid Feb 24 '16 at 04:07
  • Hello guys can you please help me on this , how can i attach a thumbnail on an ImageView ,coz i tried to replace mediaMetadataRetriever with an ImageView but it didnt work – Lutaaya Huzaifah Idris Nov 29 '16 at 08:59
  • @LutaayaHuzaifahIdris : you will get bitmap after passing video url in this method and can set that bitmap in your imageview. – KishuDroid Nov 30 '16 at 08:51
  • 2
    Thanks @KishuDroid , i did it , but the challenge with this , it's takes too much time to load on Lollipop 5 , isn't there any cool library to load URL videos into the RecyclerView – Lutaaya Huzaifah Idris Nov 30 '16 at 13:23
  • 1
    @LutaayaHuzaifahIdris: If you are getting videos link from server then you can also ask for thumbnail image of video from the server and then you can load it into the recyclerview. This is the easy and fast way to load videos. – KishuDroid Dec 01 '16 at 03:56
  • @KishuDroid, can you show it to me please , an Example – Lutaaya Huzaifah Idris Dec 09 '16 at 19:57
  • @KishuDroid the `String videoPath` is the URL of the video? – ken Oct 25 '17 at 09:01
  • @ken: Sorry for late reply. But yes it is the URL of the video. – KishuDroid Nov 10 '17 at 08:49
  • 1
    @KishuDroid I use your code..It work fines..But have 1 problem,when the video is short,sometime the video is short,then the thumbnails does not come out..just blank – ken Nov 10 '17 at 08:51
14

I tried this with glide and it worked , Glide version 4.3.1

    GlideApp.with(context)
                .asBitmap()
                .load(FILE_URL)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .into(iv_picture);

Edit : Glide was working slow for me

The Top answer was not giving result for some videos , here is how i did it

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
 //give YourVideoUrl below
retriever.setDataSource("YourVideoUrl", new HashMap<String, String>());
// this gets frame at 2nd second
Bitmap image = retriever.getFrameAtTime(2000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); 
//use this bitmap image
Manohar
  • 22,116
  • 9
  • 108
  • 144
13

It is not possible to create thumbnail from steaming link, you have to show it from server. Better upload a thumbnail along the video. Use the below code to generate thumbnail

Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("picturePath", MediaStore.Video.Thumbnails.MINI_KIND);
devu mani
  • 337
  • 6
  • 12
DKV
  • 1,767
  • 3
  • 28
  • 49
5

Here's your link:

  1. Android: Is it possible to display video thumbnails?
  2. http://developer.android.com/reference/android/media/ThumbnailUtils.html

In my opinion, Server side should create thumbnail from a video and transfer thumbnail video images through your service.

Community
  • 1
  • 1
koyot3
  • 51
  • 3
  • hi i am able to create the thumbnail from the recorded video.But while streaming is it possible ? – DKV Apr 09 '14 at 09:14
4

You can generate Thumbnail from server URL like given below

RequestOptions requestOptions = new RequestOptions();
requestOptions.isMemoryCacheable();
requestOptions.override(70,70);             
Glide.with(context).setDefaultRequestOptions(requestOptions).load(model.getMediaURL()).into(myViewHolder.imageView);
Rahul Kamble
  • 202
  • 2
  • 3
1
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("VIDEO FILE ADDRESS", MediaStore.Video.Thumbnails.MINI_KIND);
Ryan M
  • 18,333
  • 31
  • 67
  • 74
0

This method will give you thumbnails of all the video files in your phone... feel free to ask questions

public static Bitmap[] getThumbnail(Context context){
        Uri uri=MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection=new String[] { MediaStore.Video.Thumbnails._ID };
        Cursor ca = context.getContentResolver().query(uri, projection, null, null, null);
        Bitmap[] b=new Bitmap[ca.getCount()];
        int i=0;
        ca.moveToFirst();
        while(i<ca.getCount()) {
            int id = ca.getInt(ca.getColumnIndex(MediaStore.Video.Thumbnails._ID));
            b[i++]=MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),id, MediaStore.Images.Thumbnails.MINI_KIND, null );
            ca.moveToNext();
        }
        ca.close();
        return b;
    }
dkackman
  • 15,179
  • 13
  • 69
  • 123
0

Here is the best solution

"https://img.youtube.com/vi/" + videoID + "/0.jpg"

Here videoID you get from youtube Url.

sk sakil
  • 186
  • 1
  • 12
-2

We fetch all video in Android Phone. http://sunilkmrnishad.blogspot.in/2017/03/read-files-apps-photos-media-from.html

public class ThumbnailExtract extends AsyncTask<String, long[], Bitmap> {

    private final String videoUrl;
    private final ImageView mThumbnail;
    private final boolean mIsVideo;
    private MediaMetadataRetriever mmr;

    public ThumbnailExtract(String videoLocalUrl, ImageView thumbnail, boolean isVideo) {
        this.videoUrl = videoLocalUrl;
        mThumbnail = thumbnail;
        mIsVideo = isVideo;
        if (!isVideo) {
            mmr = new MediaMetadataRetriever();
        }
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        if (!mIsVideo) {
            return getBitmap(videoUrl);
        } else {
            return ThumbnailUtils.createVideoThumbnail(videoUrl,
                    MediaStore.Images.Thumbnails.MINI_KIND);
        }
    }

    @Override
    protected void onPostExecute(Bitmap thumb) {
        if (thumb != null) {
            mThumbnail.setImageBitmap(thumb);
        }
    }

    private Bitmap getBitmap(String fileUrl) {
        mmr.setDataSource(fileUrl);
        byte[] data = mmr.getEmbeddedPicture();
        Bitmap bitmap = null;
        // convert the byte array to a bitmap
        if (data != null) {
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

        }
        return bitmap != null ? ScalingUtilities.createScaledBitmap(bitmap, 40, 40, ScalingUtilities.ScalingLogic.FIT) : bitmap;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Sunil
  • 3,211
  • 3
  • 21
  • 21