title: Compress Videos using android MediaCodec api
body: <p>I want to compress locally saved video file to a smaller size in order to upload to a server.  </p>
  
  <p>Since i used  MediaCodec , i have found some tips to compress video . Here are the steps that i followed</p>
  
  <p>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)</p>
  
  <p>Question : But i dont know how to encode the already decoded stream and save the stream in to the local storage using MediaMuxer. </p>
  
  <pre><code>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 &lt; 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();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Here are the links that i follwed 
  <a href="https://stackoverflow.com/questions/22076742/android-mediacodec-reduce-mp4-video-size">Android MediaCodec: Reduce mp4 video size</a> and</p>
  
  <p><a href="https://stackoverflow.com/questions/15950610/video-compression-on-android-using-new-mediacodec-library">Video compression on android using new MediaCodec Library</a></p>