当前位置: 首页 > ops >正文

Android 使用MediaMuxer+MediaCodec编码MP4视频

以下是一个基于 MediaMuxer + MediaCodec 的 YUV420 转 MP4 工具类实现,支持开始录制、送帧、结束录制接口:

import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.util.Log;
import java.io.IOException;
import java.nio.ByteBuffer;public class YuvToMp4Encoder {private static final String TAG = "YuvToMp4Encoder";private static final String MIME_TYPE = "video/avc"; // H.264编码private static final int TIMEOUT_USEC = 10000; // 10ms超时// 编码器状态public enum EncoderState {IDLE, PREPARED, ENCODING, STOPPED}private MediaCodec mMediaCodec;private MediaMuxer mMediaMuxer;private int mTrackIndex;private boolean mMuxerStarted;private EncoderState mState = EncoderState.IDLE;private int mWidth;private int mHeight;private int mFrameRate;private int mBitRate;private long mPresentationTimeUs;private long mFirstFrameTime = -1;/*** 初始化编码器* @param width 视频宽度* @param height 视频高度* @param frameRate 帧率* @param bitRate 比特率 (bps)*/public YuvToMp4Encoder(int width, int height, int frameRate, int bitRate) {this.mWidth = width;this.mHeight = height;this.mFrameRate = frameRate;this.mBitRate = bitRate;}/*** 准备编码器* @param outputPath 输出文件路径* @throws IOException 初始化异常*/public void prepare(String outputPath) throws IOException {if (mState != EncoderState.IDLE) {throw new IllegalStateException("Encoder must be in IDLE state");}try {// 1. 创建并配置MediaCodecmMediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);format.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);format.setInteger(MediaFormat.KEY_FRAME_RATE, mFrameRate);format.setInteger(MediaFormat.KEY_COLOR_FORMAT,MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedPlanar);format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); // 关键帧间隔(秒)mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);mMediaCodec.start();// 2. 创建MediaMuxermMediaMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);mTrackIndex = -1;mMuxerStarted = false;mState = EncoderState.PREPARED;mPresentationTimeUs = 0;Log.i(TAG, "Encoder prepared successfully");} catch (Exception e) {e.printStackTrace();release();}}/*** 开始编码*/public void start() {if (mState != EncoderState.PREPARED) {throw new IllegalStateException("Encoder must be prepared before starting");}mState = EncoderState.ENCODING;mFirstFrameTime = -1;Log.i(TAG, "Encoder started");}/*** 输入一帧YUV420数据* @param yuvData YUV420字节数组* @param timestamp 时间戳(ns)*/public void feedFrame(byte[] yuvData, long timestamp) {if (mState != EncoderState.ENCODING) {Log.w(TAG, "Ignoring frame - encoder not in encoding state");return;}try {// 1. 获取输入缓冲区int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_USEC);if (inputBufferIndex < 0) {Log.w(TAG, "No available input buffer, skipping frame");return;}// 2. 填充YUV数据ByteBuffer inputBuffer = mMediaCodec.getInputBuffer(inputBufferIndex);if (inputBuffer != null) {inputBuffer.clear();inputBuffer.put(yuvData);// 3. 计算PTS (呈现时间戳)long presentationTimeUs = timestamp / 1000; // ns -> μsif (mFirstFrameTime < 0) {mFirstFrameTime = presentationTimeUs;}presentationTimeUs -= mFirstFrameTime;// 4. 提交到编码器mMediaCodec.queueInputBuffer(inputBufferIndex,0,yuvData.length,presentationTimeUs,0);}// 5. 处理编码输出drainEncoder(false);} catch (Exception e) {e.printStackTrace();}}/*** 停止编码并释放资源*/public void stop() {if (mState == EncoderState.STOPPED || mState == EncoderState.IDLE) {return;}try {if (mState == EncoderState.ENCODING) {// 发送结束信号drainEncoder(true);// 停止编码器if (mMediaCodec != null) {mMediaCodec.stop();}}// 停止混合器if (mMediaMuxer != null && mMuxerStarted) {mMediaMuxer.stop();}Log.i(TAG, "Encoder stopped successfully");} catch (Exception e) {e.printStackTrace();} finally {release();mState = EncoderState.STOPPED;}}// 释放所有资源private void release() {try {if (mMediaCodec != null) {mMediaCodec.release();mMediaCodec = null;}if (mMediaMuxer != null) {mMediaMuxer.release();mMediaMuxer = null;}} catch (Exception e) {e.printStackTrace();}mMuxerStarted = false;mTrackIndex = -1;}// 处理编码器输出private void drainEncoder(boolean endOfStream) {if (endOfStream) {// 发送结束信号int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_USEC);if (inputBufferIndex >= 0) {mMediaCodec.queueInputBuffer(inputBufferIndex, 0, 0, 0,MediaCodec.BUFFER_FLAG_END_OF_STREAM);}}// 处理所有可用输出while (true) {MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_USEC);if (outputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {if (!endOfStream) break;} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {// 格式改变时设置混合器if (mMuxerStarted) {throw new RuntimeException("Format changed after muxer started");}MediaFormat newFormat = mMediaCodec.getOutputFormat();mTrackIndex = mMediaMuxer.addTrack(newFormat);mMediaMuxer.start();mMuxerStarted = true;} else if (outputBufferIndex < 0) {
//                Log.w(TAG, "Unexpected result from dequeueOutputBuffer: " + outputBufferIndex);} else {ByteBuffer outputBuffer = mMediaCodec.getOutputBuffer(outputBufferIndex);if (outputBuffer == null) {Log.e(TAG, "Output buffer was null");} else {// 写入混合器if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0 && bufferInfo.size > 0) {if (mMuxerStarted) {outputBuffer.position(bufferInfo.offset);outputBuffer.limit(bufferInfo.offset + bufferInfo.size);mMediaMuxer.writeSampleData(mTrackIndex, outputBuffer, bufferInfo);}}// 检查结束标志if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {break;}}mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);}}}/*** 获取当前编码器状态*/public EncoderState getState() {return mState;}
}
http://www.xdnf.cn/news/18888.html

相关文章:

  • 辅助驾驶出海、具身智能落地,稀缺的3D数据从哪里来?
  • 介绍智慧城管十大核心功能之一:风险预警系统
  • 架构评审:构建稳定、高效、可扩展的技术架构(下)
  • Java8-21的核心特性以及用法
  • 揭开.NET Core 中 ToList () 与 ToArray () 的面纱:从原理到抉择
  • ⸢ 贰 ⸥ ⤳ 安全架构:数字银行安全体系规划
  • 上海控安:GB 44495-2024《汽车整车信息安全技术要求》标准解读和测试方案
  • 修改win11任务栏时间字体和小图标颜色
  • vue实现表格轮播
  • 力扣18:四数之和
  • Python 实现冒泡排序:从原理到代码
  • PDFMathTranslate:让科学PDF翻译不再难——技术原理与实践指南
  • 2024中山大学研保研上机真题
  • (附源码)基于Spring Boot公务员考试信息管理系统设计与实现
  • 2025年渗透测试面试题总结-36(题目+回答)
  • 数据结构Java--8
  • Linux基础优化(Ubuntu、Kylin)
  • vue2实现背景颜色渐变
  • Java基础 8.27
  • 神经网络|(十六)概率论基础知识-伽马函数·上
  • Linux系统性能优化全攻略:从CPU到网络的全方位监控与诊断
  • 软考-系统架构设计师 业务处理系统(TPS)详细讲解
  • Python异步编程:从理论到实战的完整指南
  • 集成电路学习:什么是SSD单发多框检测器
  • 20250827的学习笔记
  • # 快递单号查询系统:一个现代化的物流跟踪解决方案
  • [后端快速搭建]基于 Django+DeepSeek API 快速搭建智能问答后端
  • PyTorch闪电入门:张量操作与自动微分实战
  • 济南大学杨波与济南青盟信息技术有限公司杨华伟
  • DMA学习