MediaCodec理解和使用(包含demo)

概念:

1.MediaCodec是一个Codec,通过硬件加速解码和编码。它为芯片厂商和应用开发者搭建了一个统一接口。MediaCodec几乎是所有安卓播放器硬解的标配.
2.MediaCodec是Android提供的用于对音视频进行编解码的类,它通过访问底层的codec来实现编解码的功能。是Android media基础框架的一部分,通常和 MediaExtractor,MediaSync, MediaMuxer, MediaCrypto, MediaDrm, Image, Surface和AudioTrack 一起使用。

与MediaRecorder区别:

MediaCodec更偏向原生,而MediaRecorder偏向的上层封装,MediaCodec效率更高一点.
优点: MediaRecorder这个类相对于MediaCodec简单,因为他封装的很好,直接就是几个接口来完成视频录制
缺点: MediaRecorder有一个问题就是不能接触到视频流数据了,他完成不了视频的叠加技术的.

使用流程:
1.解封视频

//           MediaExtractor媒体萃取器 在播放视频前,需先解封装,MediaExtractor可以负责解封装。
            extractor = new MediaExtractor();
            try {
                extractor.setDataSource(mVideoPath);
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(CodecVideoActivity.this, "视频文件不存在!", Toast.LENGTH_SHORT).show();
            

2.创建MediaCodec

 // 创建MediaCodec
            for (int i = 0; i < extractor.getTrackCount(); i++) {
                MediaFormat format = extractor.getTrackFormat(i);
                String mime = format.getString(MediaFormat.KEY_MIME);
                LogUtils.d("dddddd mime==",mime);
                // 选择视频轨道
                if (mime.startsWith("video/")) {
                    extractor.selectTrack(i);
                    try {
                        //createDecoderByType 为特定MIME类型创建首选的 编解码器 但是,这不能用于注入特性,并且可能会
                        //创建一个不能处理特定媒体格式的编解码器。这里采用createByCodecName 方式创建.
                    // decoder = MediaCodec.createDecoderByType(mime);
                     MediaCodecList codecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
                     decoder = MediaCodec.createByCodecName(codecList.findDecoderForFormat(format));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    decoder.configure(format, surface, null, 0);
                    break;
                }
            }

            if (decoder == null) {
                Log.e("DecodeActivity", "Can't find video info!");
                return;
            }
            decoder.start();

MediaCodec处理数据的一般流程在一个循环内不断调用 dequeueInputBuffer -> queueInputBuffer填充数据 -> dequeueOutputBuffer -> releaseOutputBuffer显示画面
3.给MediaCodec喂数据

    if (!isEOS) {
                    int inIndex = decoder.dequeueInputBuffer(10000);
                    if (inIndex >= 0) {
                        ByteBuffer buffer = inputBuffers[inIndex];
//                        通过MediaExtractor的readSampleData和advance我们可以从视频文件中不断取到所选轨道的未解码数据
                        int sampleSize = extractor.readSampleData(buffer, 0);
                        //如果返回值小于0,说明已经到的文件末尾
                        if (sampleSize < 0) {
                            Log.d("DecodeActivity", "InputBuffer BUFFER_FLAG_END_OF_STREAM");
                            decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                            isEOS = true;
                        } else {
                            decoder.queueInputBuffer(inIndex, 0, sampleSize, extractor.getSampleTime(), 0);
                            //提前到下一个样品。如果没有更多的示例数据,则返回false 去掉后只能渲染第一帧.
                            boolean advance = extractor.advance();
                            Log.e("eeeeeeee advance==",advance+"");
                        }
                    }
                }

4.从MediaCodec取解码后的数据流程.

//                 从MediaCodec取解码后的数据(实际拿到的是一个Index),一般流程是这样的
                int outIndex = decoder.dequeueOutputBuffer(info, 10000);
                Log.e("eeeeeeee outIndex==",outIndex+"");
                switch (outIndex) {
                    case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
                        Log.d("DecodeActivityssss", "INFO_OUTPUT_BUFFERS_CHANGED");
                        outputBuffers = decoder.getOutputBuffers();
                        break;
                    case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
                        Log.d("DecodeActivityssss", "New format " + decoder.getOutputFormat());
                        break;
                    case MediaCodec.INFO_TRY_AGAIN_LATER:
                        Log.d("DecodeActivityssss", "dequeueOutputBuffer timed out!");
                        break;
                    default:
                        ByteBuffer buffer = outputBuffers[outIndex];
                        Log.v("DecodeActivity", "We can't use this buffer but render it due to the API limit, " + buffer);

                        // We use a very simple clock to keep the video FPS, or the video
                        // playback will be too fast
                        while (info.presentationTimeUs / 1000 > System.currentTimeMillis() - startMs) {
                            try {
                                sleep(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                break;
                            }
                        }
//                        通过 releaseOutputBuffer (outIndex, true)就可以让MediaCodec将这一帧输出到Surface上。
                        decoder.releaseOutputBuffer(outIndex, true);
                        break;
                }

1.MediaCodec的官方文档 https://developer.android.google.cn/reference/android/media/MediaCodec

MediaCodec+SurfaceView实现视频播放demo

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,演示如何使用MediaCodec和MediaMuxer API剪切视频。请注意,这只是一个基本示例,实际上需要更多的代码来完成这个任务,例如处理不同的视频格式和编解码器参数等。 ```java import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.media.MediaExtractor; import android.media.MediaFormat; import android.media.MediaMuxer; import android.os.Environment; import android.util.Log; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; public class VideoClipper { private static final String TAG = "VideoClipper"; private static final String SAMPLE_PREFIX = "video_clip_"; private static final String SAMPLE_EXTENSION = ".mp4"; private static final int TIMEOUT_US = 10000; public static void clipVideo(String inputVideoPath, long startMs, long endMs) throws IOException { File inputFile = new File(inputVideoPath); MediaExtractor extractor = new MediaExtractor(); extractor.setDataSource(inputFile.toString()); int trackCount = extractor.getTrackCount(); int videoTrackIndex = -1; MediaFormat videoFormat = null; // Find the first video track index and its format for (int i = 0; i < trackCount; i++) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { videoTrackIndex = i; videoFormat = format; break; } } if (videoTrackIndex == -1) { throw new RuntimeException("No video track found in " + inputVideoPath); } // Configure the video codec MediaCodec videoDecoder = MediaCodec.createDecoderByType(videoFormat.getString(MediaFormat.KEY_MIME)); videoDecoder.configure(videoFormat, null, null, 0); videoDecoder.start(); // Configure the video muxer String outputVideoPath = new File(Environment.getExternalStorageDirectory(), SAMPLE_PREFIX + System.currentTimeMillis() + SAMPLE_EXTENSION).getAbsolutePath(); MediaMuxer muxer = new MediaMuxer(outputVideoPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); // Copy the video header ByteBuffer header = ByteBuffer.allocate(1024); videoDecoder.getOutputFormat().getByteBuffer("csd-0").rewind(); header.put(videoDecoder.getOutputFormat().getByteBuffer("csd-0")); header.put(videoDecoder.getOutputFormat().getByteBuffer("csd-1")); header.flip(); int videoTrackIndexOut = muxer.addTrack(videoDecoder.getOutputFormat()); muxer.start(); // Extract video frames and write to muxer extractor.selectTrack(videoTrackIndex); ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); boolean inputDone = false; boolean outputDone = false; boolean videoDone = false; long videoStartTimeMs = -1; long videoEndTimeMs = -1; while (!outputDone) { if (!inputDone) { int inputIndex = videoDecoder.dequeueInputBuffer(TIMEOUT_US); if (inputIndex >= 0) { ByteBuffer inputBuffer = videoDecoder.getInputBuffer(inputIndex); int sampleSize = extractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { videoDecoder.queueInputBuffer(inputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM); inputDone = true; } else { long presentationTimeUs = extractor.getSampleTime(); if (videoStartTimeMs == -1) { videoStartTimeMs = presentationTimeUs / 1000; } if (videoEndTimeMs == -1 || presentationTimeUs / 1000 < videoEndTimeMs) { videoDecoder.queueInputBuffer(inputIndex, 0, sampleSize, presentationTimeUs, 0); extractor.advance(); } else { inputDone = true; } } } } if (!videoDone) { MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); int outputIndex = videoDecoder.dequeueOutputBuffer(bufferInfo, TIMEOUT_US); if (outputIndex >= 0) { if (bufferInfo.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) { outputDone = true; } else { ByteBuffer outputBuffer = videoDecoder.getOutputBuffer(outputIndex); buffer.clear(); buffer.put(header); buffer.put(outputBuffer); buffer.flip(); muxer.writeSampleData(videoTrackIndexOut, buffer, bufferInfo); videoDecoder.releaseOutputBuffer(outputIndex, false); if (videoEndTimeMs == -1 && bufferInfo.presentationTimeUs / 1000 >= endMs) { videoEndTimeMs = bufferInfo.presentationTimeUs / 1000; videoDone = true; } } } } if (inputDone && videoDone) { outputDone = true; } } // Release resources extractor.release(); if (videoDecoder != null) { videoDecoder.stop(); videoDecoder.release(); } if (muxer != null) { muxer.stop(); muxer.release(); } Log.i(TAG, "Video clipped and saved to " + outputVideoPath); } } ``` 要使用这个示例代码,只需调用`clipVideo`方法并传入要剪切的视频文件路径、开始时间和结束时间。例如: ```java try { VideoClipper.clipVideo("/sdcard/input.mp4", 10000, 20000); } catch (IOException e) { e.printStackTrace(); } ``` 这将在SD卡根目录下创建一个名为“video_clip_*.mp4”的新视频文件,其中*是当前时间的毫秒级时间戳。这个新文件将是原始视频文件从10秒到20秒的剪辑版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值