使用 android MediaCodec api 压缩视频 [英] Compress Videos using android MediaCodec api

查看:118
本文介绍了使用 android MediaCodec api 压缩视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将本地保存的视频文件压缩到较小的大小,以便上传到服务器.

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

自从我使用 MediaCodec 以来,我发现了一些压缩视频的技巧.这是我遵循的步骤

Since i used MediaCodec , i have found some tips to compress video . Here are the steps that i followed

1).使用 MediaExrtactor 提取媒体文件并对其进行解码.2).创建具有所需文件格式的编码器3).创建多路复用器以将文件保存在本地存储中.(不完整)

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)

问题:但我不知道如何使用 MediaMuxer 对已解码的流进行编码并将流保存到本地存储中.

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

    }

}

这是我关注的链接Android MediaCodec:减小 mp4 视频大小

使用新的 MediaCodec 库在 android 上压缩视频

推荐答案

您可以在 https://software.intel.com/en-us/intel-inde 和作为 INDE 一部分的 Android 媒体包,https://software.intel.com/en-us/articles/intel-inde-media-打包安卓教程.它有一个示例,展示了如何使用媒体包来转码=重新压缩视频文件.您可以设置较小的分辨率和/或比特率来输出以获得较小的文件

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

在 ComposerTranscodeCoreActivity.java 中

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

这篇关于使用 android MediaCodec api 压缩视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆