本文介绍了COM preSS采用了android媒体codeC API影片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想COM preSS本地保存的视频文件,以更小的尺寸,以便上传到服务器。

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

由于我使用的媒体codeC,我发现一些提示,以玉米为preSS的视频。这里是我遵循的步骤

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

1)。提取使用MediaExrtactor和德$ C $光盘,它的媒体文件。   2)。创建恩codeR所需的文件格式   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)

问:但我不知道如何连接code已经去codeD流,并保存在流中使用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();
        }

    }

}

下面是我follwed链接 Android的媒体codeC:减少MP4视频大小

Here are the links that i follwedAndroid MediaCodec: Reduce mp4 video size and

<一个href="http://stackoverflow.com/questions/15950610/video-com$p$pssion-on-android-using-new-media$c$cc-library">Video COM pression在Android上使用新媒体codeC图书馆

推荐答案

您可以尝试英特尔印出上的和媒体包为Android是印出的一部分,上的.它有一个示例,演示如何使用介质包装跨code = RECOM preSS的视频文件。您可以设置较小的分辨率和\或比特率,输出以获得更小的文件

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

在ComposerTrans codeCoreActivity.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();
}

这篇关于COM preSS采用了android媒体codeC API影片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 09:01