How to properly save frames from mp4 as png files using ExtractMpegFrames.java?

社会主义新天地 提交于 2019-12-08 05:26:28

问题


I am trying to get all frames from an mp4 file using the ExtractMpegFrames.java class found here http://bigflake.com/mediacodec/ExtractMpegFramesTest.java.txt

What I currently do is create a temp file (File.createTempFile) in a directory that stores all the frames, create a FileOutputStream and do

bm.compress(Bitmap.CompressFormat.PNG, 100, fOut) 

where fOut is the OutputStream with the file.

Currently, the saved images look like this: https://imgur.com/a/XpsV2

Using the Camera2 Api, I record a video and save it as an mp4. According to VLC, the color space for the video is Planar 4:2:0 YUV Full Scale.

Looking around, it seems that each vendor uses different color spaces https://stackoverflow.com/a/21266510/7351748. I know ffmpeg can conversions with color spaces, but I cannot use it.

I am not sure where to start to solve this issue of the strange output pngs. I am assuming that this is a color space issue, but I can be completely wrong here.


回答1:


You can get all Frames of Video Using ffmpeg library here is working code.

add dependancy

compile 'com.writingminds:FFmpegAndroid:0.3.2'

to your gradle

      private void loadFFMpegBinary() {
        try {
            if (ffmpeg == null) {
                ffmpeg = FFmpeg.getInstance(this);
            }
            ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
//                @Override
//                public void onFailure() {
//                    showUnsupportedExceptionDialog();
//                }

                @Override
                public void onSuccess() {
                    Log.d(TAG, "ffmpeg : correct Loaded");
                }
            });
        } catch (FFmpegNotSupportedException e) {

        } catch (Exception e) {
            Log.d(TAG, "EXception  : " + e);
        }
    }

here is image extratct method

 public void extractImagesVideo() {
        File moviesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES
        );

        String filePrefix = "extract_picture";
        String fileExtn = ".jpg";
        String yourRealPath = getPath(Pick_Video.this, DataModel.selectedVideoUri);
        Log.d("selected url", "" + DataModel.selectedVideoUri);
        File src = new File(yourRealPath).getAbsoluteFile();

        File appDir=new File(moviesDir,"/"+app_name+"/");
        if(!appDir.exists())
            appDir.mkdir();
        DataModel.appDir=appDir;
        File dir = new File(appDir, "testVideo");
        int fileNo = 0;
        while (dir.exists()) {
            fileNo++;
            dir = new File(moviesDir+"/"+app_name+"/", "testVideo" + fileNo);

        }
        dir.mkdir();
        DataModel.dir = dir;

        resultList = new ArrayList<String>(256);

        filePath = dir.getAbsolutePath();
        File dest = new File(dir, filePrefix + "%03d" + fileExtn);


        Log.d(TAG, "startTrim: src: " + src.toString());
        Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());


        String[] complexCommand = { "-i",""+src.toString(),"-qscale:v", "2","-vf", "fps=fps=20/1",dest.getAbsolutePath()};
        //"-qscale:v", "2","-vf", "fps=fps=20/1",//
        //works fine with speed and
        execFFmpegBinary(complexCommand);



    }

call this two method on button click event

Comment If Any query.



来源:https://stackoverflow.com/questions/46882929/how-to-properly-save-frames-from-mp4-as-png-files-using-extractmpegframes-java

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