Benjamin Cassidy
Benjamin Cassidy

Reputation: 837

MediaCodec sub-second video length

I'm using MediaCodec to encode an image into a video. It is a requirement that videos can have sub-second length (e.g. 3.5 seconds long).

My thinking in order to achieve this is is to determine the video frame rate like so.

int lengthInMillis = 3500;
float seconds = lengthInMillis / 1000f;
int ordinal = (int) seconds; // ordinal == 3
float point = seconds - ordinal;
float numFrames = seconds / point; // numFrames == 7
float fps = seconds / numFrames; // fps = 0.7
this.numFrames = (int) numFrames;

Unfortunately when attempting to configure the MediaCodec with a KEY_FRAME_RATE of less than 1 an IllegalStateException. So this method doesn't work. Is it possible to use MediaCodec to create a video with a running time that ends at a fraction of a second?

Upvotes: 1

Views: 275

Answers (1)

fadden
fadden

Reputation: 52303

The length of the video, and the frame rate of the video, are not related.

A 3.5-second video with 7 frames is running at 2 fps, not 0.7 fps. You should be computing "frames per second" as "frames / seconds", not seconds / numFrames.

In any event, the frame-rate value is actually deprecated in API 21:

Note: On LOLLIPOP, the format to MediaCodecList.findDecoder/EncoderForFormat must not contain a frame rate. Use format.setString(MediaFormat.KEY_FRAME_RATE, null) to clear any existing frame rate setting in the format.

Upvotes: 4

Related Questions