How to create video thumbnail or snapshot or capture frame on video ads from url?
What's an easy way to create a thumbnail video or snapshot or grab a still from a URL? I am using videoview and there is a black screen before starting.
My code:
String vidAddress = URL_trailers_mp4;
Uri vidUri = Uri.parse(vidAddress);
VideoView vidView=(VideoView)findViewById(R.id.myVideo);
vidView.setVideoURI(vidUri);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
vidView.seekTo(0);
And it doesn't work.
+3
source to share
5 answers
You can try this way:
FileOutputStream out;
File land=new File(URL);// image file use to create image u can give any path.
Bitmap bitmap=ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);//filePath is your video file path.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
out=new FileOutputStream(land.getPath());
out.write(byteArray);
out.close();
0
source to share
Change vidView.seekTo(0);
to
vidView.seekTo(100);
//100 milliseconds (0.1 s) clip of video
And you can create a sketch with the following code:
ImageView imageThumbnail = new ImageView(Video.this);
Bitmap bmThumbnail;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(videoSource, MediaStore.Video.Thumbnails.MINI_KIND);
imageThumbnail.setImageBitmap(bmThumbnail);
0
source to share
You can use MediaMetadataRetriever . Example:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(context, vidUri);
Bitmap bitmap = retriever.getFrameAtTime(expectPosition);
However, there is no guarantee that the data source has a frame positioned. When this happens, a nearby frame will be returned.
0
source to share