Android MediaCodec: Reduce mp4 video size Android MediaCodec: Reduce mp4 video size android android

Android MediaCodec: Reduce mp4 video size


The closest thing to what you want is probably the DecodeEditEncodeTest on bigflake. You don't need SurfaceView or TextureView, since you're not trying to display the video, but you will need to use a SurfaceTexture.

The test code generates a video, then decodes / edits / encodes, then tests the encoded output. You want the decode-edit-encode part. When creating the encoder, give it whatever size you want. The input surface it creates will be sized to match. The scaling is performed by the GPU as it renders the GL texture (from the SurfaceTexture) onto the encoder's input surface. You'll have to decide whether GLES linear filtering provides the level of quality you need.

(You can see something similar in ExtractMpegFramesTest, which scales everything to 640x480 as it's doing the extraction. In this case it's converting the frames to PNG rather than feeding them into an encoder, so it's not as applicable to your situation.)

Dropping frames is theoretically easy: just don't submit it to the encoder. In practice it's a bit harder if the input has irregularly-spaced frames. If the input is "frame1 frame2 ... pause ... frame4 frame5", and you drop frame2, you'll end up paused on frame1, which might look weird. You can use the presentation time stamps to figure out the timing.

Surface input and MediaMuxer (required for saving H.264 as .mp4) require API 18+. For older versions of Android you'll probably want to use a third-party library like ffmpeg.