如何使用ExtractMpegFrames.java将mp4中的帧正确保存为png文件? [英] How to properly save frames from mp4 as png files using ExtractMpegFrames.java?

查看:255
本文介绍了如何使用ExtractMpegFrames.java将mp4中的帧正确保存为png文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此处的ExtractMpegFrames.java类从mp4文件获取所有帧 http://bigflake.com/mediacodec/ExtractMpegFramesTest.java.txt

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

我目前正在做的是创建一个临时文件(File.createTempFile)存储所有帧的目录,创建FileOutputStream并执行

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) 

其中fOut是包含文件的OutputStream。

where fOut is the OutputStream with the file.

目前,保存的图片如下所示: https://imgur.com/a/XpsV2

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

使用Camera2 Api,我录制视频并将其另存为mp4。根据VLC,视频的色彩空间是Planar 4:2:0 YUV Full Scale。

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.

环顾四周,似乎每个供应商使用不同的颜色空间
https://stackoverflow.com/a/21266510/7351748 。我知道ffmpeg可以使用颜色空间进行转换,但我无法使用它。

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.

我不知道从哪里开始解决这个奇怪的输出pngs问题。我假设这是一个色彩空间问题,但我在这里完全错了。

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.

推荐答案

你可以使用ffmpeg库获取所有视频帧这里是工作代码。

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

添加依赖性

compile 'com.writingminds:FFmpegAndroid:0.3.2'

到你的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);
        }
    }

这里是图像提取方法

 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

评论如果有任何查询。

这篇关于如何使用ExtractMpegFrames.java将mp4中的帧正确保存为png文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆