extract all video frames in Android

给你一囗甜甜゛ 提交于 2019-11-28 10:24:08
Milorenus Lomaliza

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");

and in your loop you can grab frame using:

 Bitmap bmp = med.getFrameAtTime(i*1000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!