17

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?

Braiam
  • 1
  • 11
  • 47
  • 78
TOP
  • 2,574
  • 5
  • 35
  • 60
  • 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 Jun 15 '15 at 18:15
  • Check this answer http://stackoverflow.com/questions/31796711/fast-video-compression-like-whatsapp/31823187#31823187 – Jorge E. Hernández Aug 05 '15 at 04:16
  • 1
    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 Oct 23 '15 at 11:09
  • @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 Jun 30 '17 at 07:02
  • Can you please share a sample of how you finally did that? Thank you – james04 Jan 03 '21 at 23:36

1 Answers1

17

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

Harry J
  • 390
  • 1
  • 3
  • 11
isma3l
  • 3,833
  • 1
  • 22
  • 28
  • Sample use : VideoResampler resampler = new VideoResampler(); resampler.addSamplerClip( new SamplerClip( inputUri ) ); resampler.setOutput( outputUri ); resampler.setOutputResolution( Resolution.RESOLUTION_720P.getWidth(), Resolution.RESOLUTION_720P.getHeight() ); try { resampler.start(); } catch ( Throwable e ) { e.printStackTrace(); } It works like magic !! – Taiko Oct 29 '15 at 09:25
  • 5
    @Taiko No audio after compressing it. Is there any code missing? – SkyWalker Nov 18 '15 at 12:03
  • @SkyWalker Sorry. In my case I only used video. Feel free to update my post with audio as well ! – Taiko Nov 24 '15 at 11:47
  • @SkyWalker a bit late, but check this answer https://github.com/hoolrory/AndroidVideoSamples/issues/1#issuecomment-256654251 `mux` function adds the audio track, I tried it and worked with me – Silvia H Oct 09 '18 at 15:05