1. 程式人生 > >Android獲取網路視訊檔案縮圖

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