17
votes

In my Android app, I want to compress mp4 video by changing its resolution, bitrate. I don't want to use FFmpeg (because I don't want to use NDK), so I decided to use MediaCodec API.

Here are my logical steps:

  1. Extract video file with MediaExtractor, then decode data.
  2. Create new encoder with my new resolution, bitrate and encode data.
  3. Using MediaMuxer to create a new mp4 file.

My problem is: I don't know how to setup the connection between output of decoder and input of encoder. I can decode the video to a Surface or encode a new video from a surface. But I don't understand how to connect them.

I read these links: Android MediaCodec: Reduce mp4 video size, Video compression on android using new MediaCodec Library and the example from Bigflake: https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java

Can anybody give me a clear explanation what I have to do?

1
To do a straight copy, you would just get the input surface from the encoder (via getInputSurface()) and pass that to the decoder as the output surface (via configure()). - fadden
The MediaCodec API is quite obscure and undocumented. I've started a bounty on this question. Hopelly someone will publish a class they already have. Big mp4 file on disk -> Small mp4 file on disk. All using MediaCodec. That would save countless lives ! (well, maybe not, but sure would save mine ^^) - Taiko
@TOP i am using the below answer's to compressor library to compress the video file..But i am not getting the audio from it..Could u please help me to short out from this problem - Ravindra Kushwaha
Can you please share a sample of how you finally did that? Thank you - james04

1 Answers

15
votes

I have used this to see how to manipulate video in android: https://github.com/hoolrory/AndroidVideoSamples , check the VideoResampler.java in the CommonVideoLibrary .