如何从视频中提取位图?

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

How to extract a bitmap from a video?

问题

我正在使用Android Studio开发Java应用程序。我想要一段代码,可以从视频中获取Bitmap图像。我使用绝对路径获取视频。视频的帧率为每秒8帧。

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(absolutePath);

Bitmap bitmap = retriever.getFrameAtTime(); // 这将从视频中获取一个Bitmap图像

我只想从视频中获取Bitmap图像。非常感谢您的帮助。

英文:

I am working on a Java Application on Android Studio. I wanted any code where we can get Bitmap from video. I take video using absolute path. The input from video is 8 FPS.

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(absolutePath);

Just wanted to take Bitmap from video. Any help would be appreciated.

答案1

得分: 1

我尝试了下面的代码,它有效:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
    // 要获取帧的视频的路径
    retriever.setDataSource(absolutePath);
} catch (Exception e) {

}

String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int duration_millisec = Integer.parseInt(duration); // 毫秒为单位的持续时间
int duration_second = duration_millisec / 1000;  // 毫秒转换为秒
int frames_per_second = 30;  // 每秒想要获取的帧数
int numeroFrameCaptured = frames_per_second * duration_second;
long frame_us = 1000000 / 30;
capture == "" + numeroFrameCaptured);

for (int i = 0; i < numeroFrameCaptured; i++) {
    // 在想要检索帧的时间位置设置时间位置
    MEventsManager.getInstance().inject(MEventsManager.IMAGE, retriever.getFrameAtTime(frame_us * i, MediaMetadataRetriever.OPTION_CLOSEST));
}

retriever.release();
英文:

I tried the following code and it worked

 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
   try {
        //path of the video of which you want frames
        retriever.setDataSource(absolutePath);
    }catch (Exception e) {
       
    }

    String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    int duration_millisec = Integer.parseInt(duration); //duration in millisec
    int duration_second = duration_millisec / 1000;  //millisec to sec.
    int frames_per_second = 30;  //no. of frames want to retrieve per second
    int numeroFrameCaptured = frames_per_second * duration_second;
    long frame_us=1000000/30;
    capture=="+numeroFrameCaptured);

    for (int i = 0; i < numeroFrameCaptured; i++)
    {
        //setting time position at which you want to retrieve frames

        MEventsManager.getInstance().inject(MEventsManager.IMAGE,retriever.getFrameAtTime(frame_us*i,MediaMetadataRetriever.OPTION_CLOSEST));
    }

    retriever.release();

答案2

得分: 0

如果您想从视频中获取图像,您可以使用一个名为 FFMPEG 的库,通过在 gradle 中进行实现:

implementation 'com.arthenica:mobile-ffmpeg-min:4.3.1.LTS'

通过使用多个命令,您可以将视频的所有帧转换为图像,或者您也可以获取单个帧。

获取更多信息,请访问:

https://github.com/tanersener/mobile-ffmpeg

如果您只想要将帧显示在 ImageView 中,您可以使用 Glide/Picasso 库。

希望这能帮助到您...!!!

英文:

If you want image from video then you can use library called FFMPEG by implementing in gradle with this:

 implementation 'com.arthenica:mobile-ffmpeg-min:4.3.1.LTS'

By using several commands you can convert all frames of video into image or you can get single frame also

for more information please visit:-

 https://github.com/tanersener/mobile-ffmpeg

if you want only frame for displaying in imageview then you can use glide/picasso library.

Hope this will Help you..!!!

huangapple
  • 本文由 发表于 2020年3月16日 19:03:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/60704760.html
匿名

发表评论

匿名网友

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

确定