Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: video

Android Get video thumbnail from local or network video

In build.gradle

compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'

1. From Local Video

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
Log.i("img_url",img_url);

Uri uri = Uri.parse(img_url);
String scheme = uri.getScheme();
Log.i("uri",""+uri+" "+scheme);
retriever.setDataSource(act,uri);
bitmap = retriever.getFrameAtTime(-1, MediaMetadataRetriever.OPTION_CLOSEST);
Log.d("bitmappp", " "+bitmap.getWidth()+" "+bitmap.getHeight());
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
Log.d("IllegalArgumentException",""+ex.getMessage());
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
Log.d("RuntimeException",""+ex.getMessage());
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
Log.d("RuntimeException",""+ex.getMessage());
}
}

To create a Thumbnail, many example suggested to use Thumbnailcreater to get a small photo.
I suggest to determinate some sample size to re-size it by your self.

bitmap = AlbumInputUI.scaleBitmapInSample(act, bitmap);

handler.post(new Runnable() {
@Override
public void run() {
iv.setImageBitmap(bitmap);
}
});
public static Bitmap scaleBitmapInSample(Activity act, Bitmap bmp){
		float width, height;
		int w = bmp.getWidth();
		int h = bmp.getHeight();
		//Log.d("before","w:"+w+" h:"+h);
		if(w>h){
			width = SizeConverter.convertDpToPixel((float)250, act);
			height = SizeConverter.convertDpToPixel((float)180, act);
		}else if(h>w){
			width = SizeConverter.convertDpToPixel((float)180, act);
			height = SizeConverter.convertDpToPixel((float)250, act);
		}else{
			width = SizeConverter.convertDpToPixel((float)180, act);
			height = SizeConverter.convertDpToPixel((float)180, act);
		}
		Log.d("after","w:"+width+" h:"+height);
		return SmartUriDecoder.scaleBitmap(bmp, width, height);
	}

public static Bitmap scaleBitmap(Bitmap origBitmap, float width, float height) {
		float w = (float)origBitmap.getWidth();
		float h = (float)origBitmap.getHeight();
		Log.d("scaleBitmap","w:"+w+" h:"+h+" width:"+width+" height:"+height);
		//w = SizeConverter.convertDpToPixel(w, MyApp.getCurrent_activity());
		//h = SizeConverter.convertDpToPixel(w, MyApp.getCurrent_activity());

		if(w>=width && h>= height){
			float scale = Math.min(
					((float)width) / ((float)origBitmap.getWidth()),
					((float)height) / ((float)origBitmap.getHeight())
					);

			Bitmap result = Bitmap.createScaledBitmap(origBitmap,
					(int)(((float)origBitmap.getWidth()) * scale),
					(int)(((float)origBitmap.getHeight()) * scale),
					false
					);
			//origBitmap.recycle();
			//origBitmap = null;
			return result;
		}else{
			return origBitmap;
		}
	}

2. From Network Video

https://github.com/wseemann/FFmpegMediaMetadataRetriever

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds
byte [] artwork = mmr.getEmbeddedPicture();

mmr.release();

3. You can use uri.getScheme to find if it is a file or http to find out it is a local file or http video

Uri uri = Uri.parse(video_url);
String scheme = uri.getScheme();
Advertisements