Android获取网络视频文件缩略图

一,通过Android系统自带的类获取:

1.  public staticBitmapcreateVideoThumbnail(String filePath,int kind){
   
Bitmap bitmap = null;
   
MediaMetadataRetriever retriever = newMediaMetadataRetriever();
   
try {
       
if (filePath.startsWith("http://")
               
|| filePath.startsWith("https://")
               
|| filePath.startsWith("widevine://")) {
           
retriever.setDataSource(filePath,newHashtable<String,String>());
       
}else {
           
retriever.setDataSource(filePath);
        }
        bitmap =retriever.getFrameAtTime(-1);
   
} catch (IllegalArgumentExceptionex) {
       
// Assume this is a corrupt video file
       
ex.printStackTrace();
   
} catch (RuntimeExceptionex) {
       
// Assume this is a corrupt video file.
       
ex.printStackTrace();
   
} finally {
       
try {
           
retriever.release();
        } catch (RuntimeExceptionex) {
           
// Ignore failures while cleaning up.
           
ex.printStackTrace();
       
}
    }

    if (bitmap==null)returnnull;

   
if (kind== MediaStore.Images.Thumbnails.MINI_KIND) {
       
// Scale down the bitmap if it's too large.
       
int width= bitmap.getWidth();
       
int height= bitmap.getHeight();
       
int max =Math.max(width, height);
      
 if(max >512) {
           
float scale=512f / max;
           
int w =Math.round(scale * width);
           
int h =Math.round(scale * height);
           
bitmap = Bitmap.createScaledBitmap(bitmap,w, h, true);
       
}
    } else if (kind== MediaStore.Images.Thumbnails.MICRO_KIND) {
       
bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                96,
               
96,
               
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
   
}
    return bitmap;
}


1.      根据视频文件的路径,我们可以通过android自带的MediaMetadataRetriever类获取到视频文件的缩略图,支持的视频文件格式为:mkv,mp4等。支持的视频文件格式比较局限,因此我选择使用MediaMetadataRetriever的拓展类:FFmepgMediaMetadataRetriever来取更多格式的视频文件缩略图。

2.      下载prebuilt-aars.zip压缩包,解压后得到fmmr.aar文件(与jar文件类似),将fmmr.aar文件添加到项目中,即可创建FFmepgMediaMetadataRetriever对象,并获取到视频文件缩略图


方案二:

//创建FFmpegMediaMetadataRetriever对象
 FFmpegMediaMetadataRetriever mm=new FFmpegMediaMetadataRetriever();
try{
      //获取视频文件数据
    mm.setDataSource(path);
//获取文件缩略图
    Bitmap bitmap=mm.getFrameAtTime();    
}catch (Exception e){
}
finally {
    mm.release();
}

参考:https://github.com/wseemann/FFmpegMediaMetadataRetriever/

参考:http://blog.csdn.net/zhiyahan/article/details/51793319

个人Demo:https://github.com/15008049860/FFmepg





猜你喜欢

转载自blog.csdn.net/ffb920724/article/details/75430922