Compress Videos using android MediaCodec api
You can try Intel INDE on https://software.intel.com/en-us/intel-inde and Media Pack for Android which is a part of INDE, tutorials on https://software.intel.com/en-us/articles/intel-inde-media-pack-for-android-tutorials. It has a sample that shows how to use media pack to transcode=recompress video files. You can set smaller resolution and\or bitrate to output to get smaller file
in ComposerTranscodeCoreActivity.java
protected void setTranscodeParameters(MediaComposer mediaComposer) throws IOException {
mediaComposer.addSourceFile(mediaUri1);
mediaComposer.setTargetFile(dstMediaPath);
configureVideoEncoder(mediaComposer, videoWidthOut, videoHeightOut);
configureAudioEncoder(mediaComposer);
}
protected void transcode() throws Exception {
factory = new AndroidMediaObjectFactory(getApplicationContext());
mediaComposer = new MediaComposer(factory, progressListener);
setTranscodeParameters(mediaComposer);
mediaComposer.start();
}
Comments
-
Mr.G about 2 years
I want to compress locally saved video file to a smaller size in order to upload to a server.
Since i used MediaCodec , i have found some tips to compress video . Here are the steps that i followed
1) . Extracted the media file using MediaExrtactor and Decoded it. 2) . Creates the Encoder with required file format 3) . Create muxer to save file in local storage. (not complete)
Question : But i dont know how to encode the already decoded stream and save the stream in to the local storage using MediaMuxer.
public class CompressMedia { private static final String SAMPLE = Environment .getExternalStorageDirectory() + "/DCIM/Camera/20140506_174959.mp4"; private static final String OUTPUT_PATH = Environment .getExternalStorageDirectory() + "/DCIM/Camera/20140506_174959_REC.mp4"; private MediaExtractor extractor; private MediaCodec decoder; private MediaCodec encoder; String mime; private static final String MIME_TYPE = "video/avc"; public void extractMediaFile() { // work plan // locate media file // extract media file using Media Extractor // retrieve decoded frames extractor = new MediaExtractor(); try { extractor.setDataSource(SAMPLE); } catch (IOException e) { // TODO Auto-generated catch block // file not found e.printStackTrace(); } // add decoded frames for (int i = 0; i < extractor.getTrackCount(); i++) { MediaFormat format = extractor.getTrackFormat(i); mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { extractor.selectTrack(i); decoder = MediaCodec.createDecoderByType(mime); decoder.configure(format, null, null, 0); break; } } if (decoder == null) { Log.e("DecodeActivity", "Can't find video info!"); return; } // - start decoder - decoder.start(); extractor.selectTrack(0); // - decoded frames can obtain in here - } private void createsEncoder() { // creates media encoder to set formats encoder = MediaCodec.createDecoderByType(MIME_TYPE); // init media format MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, /* 640 */ 320, /* 480 */240); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 400000); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 25); mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar); mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5); encoder.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); encoder.start(); // - encoded data format is avaiable in here } private void createMuxer() { // creates media muxer - media muxer will be used to write the final // strem in to a desired file :) try { MediaMuxer muxer = new MediaMuxer(OUTPUT_PATH, OutputFormat.MUXER_OUTPUT_MPEG_4); int videoTrackIndex = muxer.addTrack(encoder.getOutputFormat()); //muxer.writeSampleData(videoTrackIndex, inputBuffers, bufferInfo); muxer.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Here are the links that i follwed Android MediaCodec: Reduce mp4 video size and