How can we get frames in Android

偶尔善良 提交于 2019-12-10 11:15:57

问题


Actually i need to get all the frames from videos but I use to get first frame repeatedly then specific time frames while using Mediametadatareteriver,thumbnail,timestamp to get frames.

I'd tried a lot for fixing by changing all GetFrameAtTime(options) but remains the same.

my Duration of video is 127040(2:07sec) here i Splited as 32 frames and save frames in SDCard can view all 32 but repeatedly same frames for all 32 frames.

I should get these without the help of FFMPEG and xuggler.

Is there any alternatives methods or any other Library files for using in Android if there please help me Friends.

Here is my full code

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File videoFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Movies/MEMO.mp4";

        Uri videoFileUri=Uri.parse(videoFile.toString());

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(videoFile.getAbsolutePath());
        ArrayList<Bitmap> rev=new ArrayList<Bitmap>();


        //Create a new Media Player
        MediaPlayer mp = MediaPlayer.create(getBaseContext(), videoFileUri);
        int millis = mp.getDuration();
        for(int i=0;i<millis;i+=100){
           Bitmap bitmap=retriever.getFrameAtTime(i,MediaMetadataRetriever.OPTION_CLOSEST);
           rev.add(bitmap);

          try {
              saveFrames(rev);
        } catch (IOException e) {

            e.printStackTrace();
        }
        }
    }



    public void saveFrames(ArrayList<Bitmap> saveBitmapList) throws IOException{

        String folder = Environment.getExternalStorageDirectory().toString();
        File saveFolder = new File(folder + "/Movies/new /";
        if(!saveFolder.exists()){
           saveFolder.mkdirs();
        }


        int i=1;
        for (Bitmap b : saveBitmapList){
           ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

            File f = new File(saveFolder,("frame"+i+".jpg");

            f.createNewFile();

            FileOutputStream fo = new FileOutputStream;
            fo.write(bytes.toByteArray());

               fo.flush();
               fo.close();

            i++;
        }

    }

回答1:


FFmpegMediaMetadataRetriever should solve your problem. A sample APK to test with can be found here. Just click on a file on your sdcard and you will be prompted to open it with the demo application.

To use the library simply untar the contents of this file into your projects "libs" folder. and change your existing import statements from:

import android.media.MetadataRetriever;

to:

import wseemann.media.FFmpegMediaMetadataRetriever;

and change:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();

to:

FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();


来源:https://stackoverflow.com/questions/23776177/how-can-we-get-frames-in-android

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