extract all video frames in Android
Asked Answered
D

4

7

I recorded a Video for limited time. Now i want to fetch all frames of video. I am using the below code and by using it i am able to get frames but i am not getting all video frames. 3 to 4 frames are repeated then i got a different frame. But as we all know we can get 25- 30 frames in a second to display smooth video. How to get all frames.

 for (int i = 0; i < 30; i++) {

                    Bitmap bArray = mediaMetadataRetriever.getFrameAtTime(
                            1000000 * i,
                            MediaMetadataRetriever.OPTION_CLOSEST);

                    savebitmap(bArray, 33333 * i);

                }

I don't want to use NDK. I got this link don't know what should be the value for "argb8888". I am getting error here. Can anyone explain how to do it. Getting frames from Video Image in Android

Decolorize answered 27/3, 2014 at 10:11 Comment(6)
Try with OPTION_CLOSEST and see what happens.Bankston
+1 for your valuable answer. ThanksDecolorize
Yes i have done it. Its not required NDK or any thing special. Just try to extract the frames from preview. You will get smooth video. when you will bind all frames.Decolorize
@Akanksha How did you get a smooth video? I'm facing the same problem.Uttermost
@AkankshaRathore have you tried FFmpegMediaMetadataRetriever given below .. you gettting correct frames by FFmpegMediaMetadataRetriever?Cymoid
Can we get savebitmap(bArray, 33333 * i); method!!Hannigan
C
13

I faced the same problem before and the Android's MediaMetadataRetriever seems not appropriated for this task since it doesn't have a good precision. I used a library called "FFmpegMediaMetadataRetriever" in android studio:

  1. Add this line in your build.graddle under module app:

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

  2. Rebuild your project.

  3. Use the FFmpegMediaMetadataRetriever class to grab frames with higher precision:

    FFmpegMediaMetadataRetriever med = new FFmpegMediaMetadataRetriever();
    
    med.setDataSource("your data source");
    
  4. and in your loop you can grab frame using:

     Bitmap bmp = med.getFrameAtTime(i*1000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
    
Chalcocite answered 30/10, 2015 at 1:29 Comment(2)
what is my loop end index ? How can i get no of captured frames? I have to iterate loop till the no of frames capturedEvangelineevangelism
The first parameter of the method "getFrameAtTime" is the location of the frame in video's timeline in unit of micro-sec(1 sec = 1,000,000ms). thus in that specific code we get frame at each interaval of 1 sec. so the loop limit for i might be the video length in seconds. P.S. if you want to get frame in shorter intervals(example milli-sec) you may change the number of 0's.Chalcocite
G
4

To get image frames from video we can use ffmpeg.For integrating FFmpeg in android we can use precompiled libraries like ffmpeg-android.

To extract image frames from a video we can use below command

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-an", "-r", "1/2", "-ss", "" + startMs / 1000, "-t", "" + (endMs - startMs) / 1000, outputFileAbsolutePath};

Here,

-y

Overwrite output files

-i

ffmpeg reads from an arbitrary number of input “files” specified by the -i option

-an

Disable audio recording.

-r

Set frame rate

-ss

seeks to position

-t

limit the duration of data read from the input file

Here in place of inputFileAbsolutePath you have to specify the absolute path of video file from which you want to extract images.

For complete code check out this on my repository .Inside extractImagesVideo() method I am running command for extracting images from video.

For complete tutorial regarding integration of ffmpeg library and using ffmpeg commands to edit videos, check out this post which I have written on my blog.

Greaves answered 26/3, 2017 at 8:24 Comment(0)
B
2

You need to do :

  1. Decode the video.
  2. Present the decoded images at least as fast as 24 images / second. I suppose you can skip this step.
  3. Save the decoded images.

It appears that decoding the video would be the most challenging step. People and companies have spent years developing codecs (encoder / decoder) for various video formats.

Use this library JMF for FFMPEG.

Brownfield answered 27/9, 2014 at 7:28 Comment(0)
F
0

[Updated for 2023]

In this context, you might find this repository useful: https://github.com/duckyngo/Fast-Video-Frame-Extraction. It's a high-performance solution to extract all frames from a video.

On the other hand, if you just want to extract a limited number of frames. You can try "getFrameAtIndex()" function from the MediaMetadataRetriever class.

Fondness answered 29/8, 2023 at 1:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.