实现把一个音频文件的视频抽取出来

本文介绍了如何使用MediaExtractor和MediaMuxer从音视频文件中抽取音频或视频。MediaExtractor用于数据分离,通过setDataSource设置源文件,getTrackCount获取通道数,readSampleData读取样本数据。MediaMuxer则用于生成单独的音频或视频文件,将数据写入新的文件。提供了伪代码和示例代码以帮助理解操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

一个音视频文件是由音频和视频组成的;我们可以通过MediaExtractor、MediaMuxer把音频或视频给单独抽取出来;抽取出来的音频和视频能单独播放;

知识结构

MediaExtractor :作用是把音频和视频的数据进行分离;
它常用的方法有:

1、setDataSource(String path):
即可以设置本地文件又可以设置网络文件
Sets the data source (file-path or http URL) to use

2、getTrackCount():
得到源文件通道数
Count the number of tracks found in the data source.

3、getTrackFormat(int index):获取指定(index)的通道格式
Get the track format at the specified index.

4、getSampleTime():返回当前的时间戳
Returns the current sample’s presentation time in microseconds.

5、readSampleData(ByteBuffer byteBuf, int offset):把指定通道中的数据按偏移量读取到ByteBuffer中;
Retrieve the current encoded sample and store it in the byte buffer starting at the given offset.

6、advance():读取下一帧数据
Advance to the next sample.

7、release():读取结束后,释放资源

MediaMuxer:生成音频或视频文件;还可以把音频与视频混合成一个音视频文件;
它常用的方法:

1、MediaMuxer(String path, int format):
path:输出文件的名称
format:输出文件的格式;当前只支持MP4格式;

2、addTrack(MediaFormat format):添加通道;我们更多的是使用MediaCodec.getOutpurForma()或Extractor.getTrackFormat(int index)来获取MediaFormat;也可以自己创建;
Adds a track with the specified format.

3、start():开始合成文件
Starts the muxer.

4、writeSampleData(int trackIndex, ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo):把ByteBuffer中的数据写入到在构造器设置的文件中;
Writes an encoded sample into the muxer.

5、stop():停止合成文件
Stops the muxer.

6、rel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值