Android中使用 MediaExtractor 和 MediaMuxer解析、封装 mp4 文件

2024-04-16 22:18

本文主要是介绍Android中使用 MediaExtractor 和 MediaMuxer解析、封装 mp4 文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近开始学习Android下的封装和解封装技术,熟悉MediaExtractor和MediaMuxer的使用。

1、MainActivity.java文件:

package com.example.tongjiangsong.mediaextractmuxer;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button)findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new ExtractorMuxerThread().start();}});}
}

2、ExtractorMuxerThread.java文件:

package com.example.tongjiangsong.mediaextractmuxer;import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Environment;
import android.widget.Toast;import java.io.IOException;
import java.nio.ByteBuffer;
import android.view.View;public class ExtractorMuxerThread extends Thread {@Overridepublic void run() {super.run();mixer();}public void mixer() {MediaExtractor videoExtractor = null;MediaExtractor audioExtractor = null;MediaMuxer mixMediaMuxer = null;String outputVideoFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/people.mp4";String outputAudioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/qishi.aac";String outputFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mixer.mp4";try {videoExtractor = new MediaExtractor();videoExtractor.setDataSource(outputVideoFilePath);int videoIndex = -1;MediaFormat videoTrackFormat = null;int trackCount = videoExtractor.getTrackCount();for (int i = 0; i < trackCount; i++) {videoTrackFormat = videoExtractor.getTrackFormat(i);if (videoTrackFormat.getString(MediaFormat.KEY_MIME).startsWith("video/")) {videoIndex = i;break;}}audioExtractor = new MediaExtractor();audioExtractor.setDataSource(outputAudioFilePath);int audioIndex = -1;MediaFormat audioTrackFormat = null;trackCount = audioExtractor.getTrackCount();for (int i = 0; i < trackCount; i++) {audioTrackFormat = audioExtractor.getTrackFormat(i);if (audioTrackFormat.getString(MediaFormat.KEY_MIME).startsWith("audio/")) {audioIndex = i;break;}}videoExtractor.selectTrack(videoIndex);audioExtractor.selectTrack(audioIndex);MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();mixMediaMuxer = new MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);int videoTrackIndex = mixMediaMuxer.addTrack(videoTrackFormat);int audioTrackIndex = mixMediaMuxer.addTrack(audioTrackFormat);mixMediaMuxer.start();ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);long videotime;long audiotime;{videoExtractor.readSampleData(byteBuffer, 0);if (videoExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {videoExtractor.advance();}videoExtractor.readSampleData(byteBuffer, 0);long sampleTime = videoExtractor.getSampleTime();videoExtractor.advance();videoExtractor.readSampleData(byteBuffer, 0);long sampleTime1 = videoExtractor.getSampleTime();videoExtractor.advance();videotime = Math.abs(sampleTime - sampleTime1);}{audioExtractor.readSampleData(byteBuffer, 0);if (audioExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {audioExtractor.advance();}audioExtractor.readSampleData(byteBuffer, 0);long sampleTime = audioExtractor.getSampleTime();audioExtractor.advance();audioExtractor.readSampleData(byteBuffer, 0);long sampleTime1 = audioExtractor.getSampleTime();audioExtractor.advance();audiotime = Math.abs(sampleTime - sampleTime1);}videoExtractor.unselectTrack(videoIndex);videoExtractor.selectTrack(videoIndex);while (true) {int data = videoExtractor.readSampleData(byteBuffer, 0);if (data < 0) {break;}videoBufferInfo.size = data;videoBufferInfo.presentationTimeUs += videotime;videoBufferInfo.offset = 0;videoBufferInfo.flags = videoExtractor.getSampleFlags();mixMediaMuxer.writeSampleData(videoTrackIndex, byteBuffer, videoBufferInfo);videoExtractor.advance();}while (true) {int data = audioExtractor.readSampleData(byteBuffer, 0);if (data < 0) {break;}audioBufferInfo.size = data;audioBufferInfo.presentationTimeUs += audiotime;audioBufferInfo.offset = 0;audioBufferInfo.flags = audioExtractor.getSampleFlags();mixMediaMuxer.writeSampleData(audioTrackIndex, byteBuffer, audioBufferInfo);audioExtractor.advance();}} catch (IOException e) {e.printStackTrace();} finally {if (mixMediaMuxer != null) {mixMediaMuxer.stop();mixMediaMuxer.release();}if (videoExtractor != null){videoExtractor.release();}if (audioExtractor != null){audioExtractor.release();}}}
}

3、activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="解封装和封装!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

4、权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

5、运行结果:

这篇关于Android中使用 MediaExtractor 和 MediaMuxer解析、封装 mp4 文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910046

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class