简体   繁体   English

如何使用ExtractMpegFrames.java将mp4中的帧正确保存为png文件?

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

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 我试图使用在http://bigflake.com/mediacodec/ExtractMpegFramesTest.java.txt中找到的ExtractMpegFrames.java类从mp4文件中获取所有帧。

What I currently do is create a temp file (File.createTempFile) in a directory that stores all the frames, create a FileOutputStream and do 我目前要做的是在存储所有帧的目录中创建一个临时文件(File.createTempFile),创建一个FileOutputStream并执行

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

where fOut is the OutputStream with the file. 其中fOut是带有文件的OutputStream。

Currently, the saved images look like this: https://imgur.com/a/XpsV2 当前,保存的图像如下所示: https : //imgur.com/a/XpsV2

Using the Camera2 Api, I record a video and save it as an mp4. 我使用Camera2 Api录制视频并将其另存为mp4。 According to VLC, the color space for the video is Planar 4:2:0 YUV Full Scale. 根据VLC,视频的色彩空间为4:2:0 YUV Full Scale。

Looking around, it seems that each vendor uses different color spaces https://stackoverflow.com/a/21266510/7351748 . 环顾四周,似乎每个供应商都使用不同的色彩空间https://stackoverflow.com/a/21266510/7351748 I know ffmpeg can conversions with color spaces, but I cannot use it. 我知道ffmpeg可以使用颜色空间进行转换,但是我不能使用它。

I am not sure where to start to solve this issue of the strange output pngs. 我不确定从哪里开始解决奇怪的输出png的问题。 I am assuming that this is a color space issue, but I can be completely wrong here. 我假设这是一个色彩空间问题,但是我在这里可能是完全错误的。

You can get all Frames of Video Using ffmpeg library here is working code. 您可以使用ffmpeg库获取所有视频帧,这里是工作代码。

add dependancy 增加依赖

compile 'com.writingminds:FFmpegAndroid:0.3.2'

to your gradle 到你的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. 如果有任何查询,请注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM