Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

compression - Compress Videos using android MediaCodec api

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

Video compression on android using new MediaCodec Library

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 andor 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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...