MediaMetadataRetriever.getFrameAtTime() returns only first frame

前端 未结 7 1567
不思量自难忘°
不思量自难忘° 2020-12-01 12:12

I have extracted frames from a video using MetadataRetriever, and have stored all images in an ArrayList. I want to store all of them on an SD car

7条回答
  •  庸人自扰
    2020-12-01 12:33

    @Navneet Krishna

    Videos have key-frames (sometimes called "sync" frames). The issue might be that: the first frame is the closest sync frame when you use OPTION_CLOSEST_SYNC...

    Try replacing this line:

    Bitmap bitmap=retriever.getFrameAtTime(i,OPTION_CLOSEST_SYNC);
    

    with this (which gets closest-available frame to the given time):

    Bitmap bitmap=retriever.getFrameAtTime(i,OPTION_CLOSEST);
    

    Read about: CLOSEST & CLOSEST_SYNC.

    Code below is untested, but is a general idea of "how to...", so let me now if it helps you...
    Result should be one picture per second of video duration. Test with a short video.

    //Create a new Media Player
    MediaPlayer mp = MediaPlayer.create(getBaseContext(), videoFileUri);
    
    int millis = mp.getDuration();
    
    for (int i = 0; i < millis; i += 1000) 
    {
        Bitmap bmp = retriever.getFrameAtTime( i * 1000, MediaMetadataRetriever.OPTION_CLOSEST );
        if (bmp != null) { rev.add( bmp ); }
    }
    
    retriever.release ();
    

提交回复
热议问题