Compress video using android MediaCodec api

I want to compress a locally saved video file to a smaller size in order to upload it to the server.

Since I used MediaCodec, I found some tips for compressing the video. Here are the steps I followed

1). Extracted media using MediaExrtactor and decoded. 2). Creates an encoder with the desired file format 3). Create a multiplexer to save the file to local storage. (not complete)

Question: But I do not know how to encode an already decoded stream and save the stream to 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 I wrote Android MediaCodec: reduce the size of the mp4 video and

Compress video on Android using the new MediaCodec library

+6
source share
1 answer

You can try Intel INDE at https://software.intel.com/en-us/intel-inde and the Media Pack for Android, which is part of INDE, tutorials at https://software.intel.com/en-us/ articles / intel-inde-media-pack-for-android-tutorials . It has a sample that shows how to use a media package for transcoding = recompress video files. You can set lower resolution and / or bit rate for output to get a 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(); } 
+6
source

Source: https://habr.com/ru/post/969861/


All Articles