Android 本地视频加载(FFmpegMediaMetadataRetriever)

使用库:FFmpegMediaMetadataRetriever()

相关版本:

 

 

0、配置FFmpegMediaMetadataRetriever库

app中的build.gradle中添加dependencies,可以直接下载arr(https://github.com/wseemann/FFmpegMediaMetadataRetriever/releases)放到libs目录中(我下载的是1.0.14),也可以直接网上加载(上边注释掉的),然后主函数中加载(import wseemann.media.FFmpegMediaMetadataRetriever;)。

apply plugin: 'com.android.application'

android {
//compileSdkVersion 24
// buildToolsVersion "29.0.2"

compileSdkVersion 29
buildToolsVersion "28.0.3"

defaultConfig {
applicationId "com.tencent.mobilenetssdncnn"
archivesBaseName = "$applicationId"

ndk {
moduleName "ncnn"
abiFilters "armeabi-v7a", "arm64-v8a"
}
minSdkVersion 24

}

externalNativeBuild {
cmake {
version "3.10.2"
path file('src/main/jni/CMakeLists.txt')
}
}
repositories {
flatDir {
dirs 'libs'
}
}
}

//dependencies {
// implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever-core:1.0.16'
// implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever-native:1.0.16'
//}

dependencies {
//noinspection GradleCompatible
implementation('com.android.support:support-v4:27.1.0')
implementation(name:'all-fmmrhttp',ext:'aar')
//implementation(name:'FFmpegMediaMetadataRetriever-core:1.0.15',ext:'aar')

}

库加载成功可以在externalLibraries中看到:

 

 

1、申请权限(必须有申请权限),打开本地视频

Button buttonVideo = (Button)findViewById(R.id.buttonVideo);
        buttonVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                int permission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(
                            MainActivity.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            777
                    );
                } else {
                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setType("video/*");
                    startActivityForResult(intent, SELECT_VIDEO);
                }

//                Intent intent = new Intent(Intent.ACTION_PICK);
//                intent.setType("video/*");
//                startActivityForResult(intent, SELECT_VIDEO);
            }
        });

问题记录:刚开始没申请权限,虽然有路径,但是后面的mmr.setDataSource(path)加载的时候会出错。

2、加载视频

Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                mmr = new FFmpegMediaMetadataRetriever();
                mmr.setDataSource(path);
                String dur = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);  // ms
                String sfps = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_FRAMERATE);  // fps
                String rota = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);  // rotation
                int duration = Integer.parseInt(dur);
                float fps = Float.parseFloat(sfps);
                float rotate = 0;
                if (rota != null) {
                    rotate = Float.parseFloat(rota);
                }
                //sbVideo.setMax(duration * 1000);
                float frameDis = 1.0f / fps * 1000 * 1000 * videoSpeed;
                videoCurFrameLoc = 0;

                while (detectVideo.get() && (videoCurFrameLoc) < (duration * 1000)) {
                    videoCurFrameLoc = (long) (videoCurFrameLoc + frameDis);
                    // sbVideo.setProgress((int) videoCurFrameLoc);
                    // final Bitmap b = mmr.getFrameAtTime(videoCurFrameLoc, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
                    bitmap = mmr.getFrameAtTime(videoCurFrameLoc, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
                    if (bitmap == null) {
                        continue;
                    }
                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotate);
                    yourSelectedImage = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                    startTime = System.currentTimeMillis();
                    MobilenetSSDNcnn.Obj[] objects = mobilenetssdncnn.Detect(yourSelectedImage, false,true,true);
//                    final long durT = System.currentTimeMillis() - start;
                    showObjects(objects);
                    frameDis = 1.0f / fps * 1000 * 1000 * videoSpeed;
                }
                mmr.release();

                if (detectVideo.get()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sbVideo.setVisibility(View.GONE);
                            sbVideoSpeed.setVisibility(View.GONE);
                            Toast.makeText(MainActivity.this, "Video end!", Toast.LENGTH_LONG).show();
                        }
                    });
                }
                detectVideo.set(false);
            }
        }, "video detect");
        thread.start();

 

posted on 2021-12-30 17:49  WenJXUST  阅读(1051)  评论(0编辑  收藏  举报