MP4 Video Last frame - android

I'm trying to get last frame of mp4 video using MediaMetadataRetriever but it always return first Frame for short videos (like 3s long videos), it work fine for long videos. FFmpegMediaMetadataRetriever also give same result.
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(video);
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Bitmap frameAtTime = retriever.getFrameAtTime(Long.parseLong(time)*1000, MediaMetadataRetriever.OPTION_CLOSEST);
mImage.setImageBitmap(frameAtTime);
Any suggestions would be appreciated.

Related

How to get frame numbers from video in android?

Is there any way for getting the total number of frames from a video in android? I'm using com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14 library for getting a frame on specific time. In order to get frames and save them as an image with specific time intervals, I need to get frame numbers.
I found the solution for getting frame numbers and frame rates using FFmpeg library. If anyone is struggling with frame extraction, you can refer to https://github.com/wseemann/FFmpegMediaMetadataRetriever. Below is my code to get the total number of frames.
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
try {
//path of the video of which you want frames
mmr.setDataSource(absolutePath);
} catch (Exception e) {
System.out.println("Exception= "+e);
}
long duration = mmr.getMetadata().getLong("duration");
double frameRate = mmr.getMetadata().getDouble("framerate");
int numberOfFrame = (int) (duration/frameRate);

How to get video thumbnail as GIF from video URL?

I want to show video thumbnail as GIF from video URL like TikTok - including musical.ly app.
See below video for getting more details
I have used this to get first 10-15 video frames to generate GIF. but it's not working.
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever .setDataSource(url, new HashMap<String, String>());
Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(1000); //unit in microsecond
capturedImageView.setImageBitmap(bmFrame);

Android: Get frame from network video resource

I want to get a pointed frame from network video by MediaMetadataRetriever. But when I called MediaMetadataRetriever.setDataSource(myUrl), app will crash becauseof IllegalArgumentException. Is there anyone has this problem and could you tell me how to deal with it? Thanks a lot!
MediaMetadataRetriever rev = new MediaMetadataRetriever();
rev.setDataSource(myUrl); //will crash at here
Bitmap bitmap = rev.getFrameAtTime(mVideoView.getCurrentPosition() * 1000,
MediaMetadataRetriever.OPTION_CLOSEST_SYNC)

How to extract 24 different frames per second from video?

I want to create a GIF from mp4 video. So I need to extract the frames from video first. Here is the code I use to extract frames:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(mFilePath);
Bitmap bitmap = retriever.getFrameAtTime(i,
MediaMetadataRetriever.OPTION_CLOSEST);
Note that variable i is time in microseconds. Since I want to get 24 frames/second, I call retriever.getFrameAtTime() with i = 42000, 84000, .... (microseconds).
The problem is: when I collect extracted frames to a video, I see only 4-5 different frames. In other words, I didn't get a smooth video. It seems that MediaMetadataRetriever often returns the same frame with different given time. Please help me!

How can I get a video thumbnail in android if android can not decoding the video?

I have successfuly get thumbnail for video with code like this:
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(mediaFile.getAbsolutePath(),
MediaStore.Video.Thumbnails.MINI_KIND);
But this code can only get bitmap for few videos because android can not decode most part of videos for android video decoder is weak.
if I just want to get a thumbnail for video, do I have any good solution. If I do not need to use ndk is much better! But if ndk solution is much more efficient or easy, I think it also OK.
You can use-
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(filePath);
//here 5 means frame at the 5th sec.
bitmap = retriever.getFrameAtTime(5);
} catch (Exception ex) {
// Assume this is a corrupt video file
}

Categories

Resources