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

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.

解决方案

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(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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值