KeyframeDecoder

class KeyframeDecoder(context: Context, uri: Uri)(source)

Decodes chosen moments of a video fast: one hardware decoder for the whole video, fed from the keyframe before each moment up to it, and only those moments drawn by the GPU at the size we want (colour conversion, scaling and rotation in one pass) and read back as a bitmap.

Exact moments, not just the nearest keyframes: edits from TikTok and Instagram can go five seconds between keyframes, and everything shown in between (captions, faces) was missed.

MediaMetadataRetriever.getScaledFrameAtTime did the same one frame at a time, setting up and converting on the CPU for each: ~150 ms a frame on the edge 50, half the faces pass. Anything this can't handle throws, and MediaFrames falls back to that for the video.

Not thread-hopping: the GL context belongs to the thread that made it, so callers run this on one thread throughout (see MediaFrames).

Constructors

Link copied to clipboard
constructor(context: Context, uri: Uri)

Functions

Link copied to clipboard
fun forEach(timesMs: List<Long>, px: Int, onFrame: (atMs: Long, frame: Bitmap) -> Boolean): Int

Calls onFrame with the first frame at or after each of timesMs (ascending), at most px on its long side, the right way up. Stops early if it returns false. Returns how many frames it delivered.