有没有办法在Android Studio中提取视频的所有帧?

huangapple go评论94阅读模式
英文:

Is there any way to extract all the frames of a video in android studio?

问题

我正在开发一个应用程序,其中我正在提取视频的所有帧。我尝试了多种方法,但没有获得任何结果。所以有人可以建议如何做到这一点吗?

我已经尝试了在Android Studio中使用OpenCV和MediaMetadataRetriever。

英文:

I am working on an application in which i'm extracting all the frames of a video. I have tried multiple approaches but not able to get any results. So can anyone suggest how to do this?

I have tried OpenCV and MediaMetadataRetriever in android studio.

答案1

得分: 1

答案很简单,是的!您可以轻松地使用FmpegMediaMetadataRetriever库来实现这个。

在模块app的build.gradle中添加以下两行:

implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever-core:1.0.19'
implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever native:1.0.19'

同步您的项目。

用法:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
// 视频的持续时间
String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
int duration = Integer.parseInt(time);
// 每隔一定时间获取一帧
int period = 1000000; // 每1秒获取1帧,如果需要更精确,请减小间隔
int len = duration / period;
List<Bitmap> frames = new ArrayList();

for (int i = 0; i < len; i++) {
    Bitmap frame = mmr.getFrameAtTime(i * period, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // 在2秒处获取帧
    frames.add(frame);
}

mmr.release();

// 在frames数组列表中,您可以自由操作帧
for (Bitmap frame : frames) {

    // 在frame中,您可以自由操作帧

}
英文:

Simple answer is Yes !, you can do that easily using FmpegMediaMetadataRetriever library.

Add those two lines in your build.graddle under module app:

implementation &#39;com.github.wseemann:FFmpegMediaMetadataRetriever-core:1.0.19&#39;
implementation &#39;com.github.wseemann:FFmpegMediaMetadataRetriever native:1.0.19&#39;

Sync your project.

Usage:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST;
// duration of the video
String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
int duration = Integer.parseInt(time);
//frame every selected period
int period = 1000000//each 1 second will get 1 frame, if you want more precision, minimise the period 
int len = duration/period;
List&lt;Bitmap&gt; frames = ArrayList();

for(int i = 0; i&lt;len; i++){
Bitmap frame = mmr.getFrameAtTime(i * period, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds 
frames.add(frame);

}   
    mmr.release();

//here you can do what ever you want from the frames arraylist
for(Bitmap frame: frames){

   //here you can do what ever you want from the frame

}

huangapple
  • 本文由 发表于 2023年1月9日 17:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定