2

I'm trying to get last frame of mp4 video using MediaMetadataRetriever but it always return first Frame for short videos (like 3s long videos), it work fine for long videos. FFmpegMediaMetadataRetriever also give same result.

      MediaMetadataRetriever retriever = new MediaMetadataRetriever();
      retriever.setDataSource(video);
      String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
      Bitmap frameAtTime = retriever.getFrameAtTime(Long.parseLong(time)*1000, MediaMetadataRetriever.OPTION_CLOSEST);
      mImage.setImageBitmap(frameAtTime);

Any suggestions would be appreciated.

Salah Hammouda
  • 303
  • 2
  • 19
  • From the description of getFrameAtTime function, it will return 0 if the constraints cannot be satisfied. Can you try using `int frame = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT)` to retrieve the final frame of the video and then use `retriever.getFrameAtIndex(frame-1)` to get the frame? Should also always check that extractMetadata isn't returning null. – charliebeckwith Apr 29 '19 at 16:04
  • getFrameAtIndex : Call requires API level 28 (current min is 21). I know that getFrameAtTime takes microseconds, not milliseconds that's why i * by 1000 – Salah Hammouda Apr 29 '19 at 16:28
  • Dope. My mistake, I read that as conversion to ms. Can you verify that the metadata returns the correct length of the video? [Idk if this is any help](https://stackoverflow.com/a/33247728/3508192), but it's another way to get the duration – charliebeckwith Apr 29 '19 at 17:04
  • ya the time is fine, as i said it work fine for long video the issue with short video – Salah Hammouda Apr 29 '19 at 17:13
  • just checking. I'd try messing with the option param, like using `OPTION_NEXT_SYNC`? – charliebeckwith Apr 29 '19 at 17:20
  • 1
    tried all of them .. – Salah Hammouda Apr 29 '19 at 17:21
  • It looks like the function may be bugged - found a [github issue](https://github.com/wseemann/FFmpegMediaMetadataRetriever/issues/180#issuecomment-419792104) for FFmpegMediaMetadataRetriever, which you mentioned also didn't work. Their suggestion is to pass a time a few ms before the time of the final frame. Best of luck. – charliebeckwith Apr 30 '19 at 22:23
  • @charliebeckwith thx for ur time :) – Salah Hammouda May 01 '19 at 15:58

0 Answers0