Android音视频处理之MediaExtractor

Android提供了一个MediaExtractor类,可以用来分离容器中的视频track和音频track,下面的例子展示了使用MediaExtractor和MediaMuxer来实现视频的换音:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
private void muxingAudioAndVideo(String videoPath, String audioPath, String outputPath) throws IOException {
MediaMuxer mediaMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

//视频的MediaExtractor
MediaExtractor videoExtractor = new MediaExtractor();
videoExtractor.setDataSource(videoPath);
int videoTrackIndex = -1;
for (int i = 0; i < videoExtractor.getTrackCount(); i++) {
MediaFormat format = videoExtractor.getTrackFormat(i);
if (format.getString(MediaFormat.KEY_MIME).startsWith("video")) {
videoExtractor.selectTrack(i);
videoTrackIndex = mediaMuxer.addTrack(format);
break;
}
}

//音频的MediaExtractor
MediaExtractor audioExtractor = new MediaExtractor();
audioExtractor.setDataSource(audioPath);
int audioTrackIndex = -1;
for (int i = 0; i < audioExtractor.getTrackCount(); i++) {
MediaFormat format = audioExtractor.getTrackFormat(i);
if (format.getString(MediaFormat.KEY_MIME).startsWith("audio")) {
audioExtractor.selectTrack(i);
audioTrackIndex = mediaMuxer.addTrack(format);
}
}
// 添加完所有轨道后start
mediaMuxer.start();

// 封装视频track
if (-1 != videoTrackIndex) {
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
info.presentationTimeUs = 0;
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
while (true) {
int sampleSize = videoExtractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
break;
}
info.offset = 0;
info.size = sampleSize;
info.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
info.presentationTimeUs = videoExtractor.getSampleTime();
mediaMuxer.writeSampleData(videoTrackIndex, buffer, info);

videoExtractor.advance();
}
}

// 封装音频track
if (-1 != audioTrackIndex) {
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
info.presentationTimeUs = 0;
ByteBuffer buffer = ByteBuffer.allocate(100 * 1024);
while (true) {
int sampleSize = audioExtractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
break;
}
info.offset = 0;
info.size = sampleSize;
info.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
info.presentationTimeUs = audioExtractor.getSampleTime();
mediaMuxer.writeSampleData(audioTrackIndex, buffer, info);

audioExtractor.advance();
}
}

// 释放MediaExtractor
videoExtractor.release();
audioExtractor.release();

// 释放MediaMuxer
mediaMuxer.stop();
mediaMuxer.release();
}

MediaExtractor的接口比较简单,首先通过 setDataSource()设置数据源,数据源可以是本地文件地址,也可以是网络地址:

1
2
MediaExtractor mVideoExtractor = new MediaExtractor();
mVideoExtractor.setDataSource(mVideoPath);

然后可以通过getTrackFormat(int index)来获取各个track的 MediaFormat,通过MediaFormat来获取track的详细信息,如:MimeType、分辨率、采样频率、帧率 等等,其内部就是一个map对象,通过key获取对应的信息:

1
2
3
for (int i = 0; i < mVideoExtractor.getTrackCount(); i++) {
MediaFormat format = mVideoExtractor.getTrackFormat(i);
}

获取到track的详细信息后,通过selectTrack(int index)选择指定的通道:

1
2
3
4
if (format.getString(MediaFormat.KEY_MIME).startsWith("video/")) {
mVideoExtractor.selectTrack(i);
break;
}

指定通道之后就可以从MediaExtractor中读取数据了:

1
2
3
4
5
6
7
8
while (true) {
int sampleSize = mVideoExtractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
break;
}
// do something
mVideoExtractor.advance(); // 移动到下一帧
}

在读取结束之后,记得释放资源:

1
mVideoExtractor.release();

问题:

  1. Android MediaExtractor readSampleData IllegalArgumentException
    视频采样的时候报的错误,主要视频的采用buffer设置的可能小了,开始设置的是100K,后来提到1M就没有问题;
    参考地址:https://stackoverflow.com/questions/33148629/android-mediaextractor-readsampledata-illegalargumentexception
  2. MPEG4Writer: Unsupported mime ‘audio/mpeg’
    一开始使用的是mp3音频,显示不支持,后来换成m4a就没有问题,后期在看下MPEG4Writer支持什么音频格式!!!
    参考地址: http://www.cnblogs.com/rockylearnstodevelop/p/5353717.html

拓展:

  1. 关于Android读取assert下的资源文件
    Android 中资源分为两种,一种是res下可编译的资源文件, 这种资源文件系统会在R.java里面自动生成该资源文件的ID,访问也很简单,只需要调用R.XXX.id即可;第二种就是放在assets文件夹下面的原生资源文件,assets文件夹里面的文件都是保持原始的文件格式,放在这个文件夹下面的文件不会被R文件编译,所以不能像第一种那样直接使用.Android提供了一个工具类,方便我们操作获取assets文件下的文件:AssetManager,需要用AssetManager以字节流的形式读取文件。其主要步骤是:

    • 获取Context然后调用getAssets()来获取AssetManager引用。
    • 再用AssetManager的open(String fileName,int accessMode)方法并指定要读取的文件名称以及访
      问模式来获取输入流InputStream。
    • 用InputStream读取文件,读取完后需要InputStream.close();
    • 最后调用AssetManager的close()方法关闭AssetManager。
  2. AssetManager提供了如下方法用于处理assets:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    String[] list(String path);//列出该目录下的下级文件和文件夹名称

    InputStream open(String fileName);//以顺序读取模式打开文件,默认模式为ACCESS_STREAMING

    InputStream open(String fileName, int accessMode);//以指定模式打开文件。读取模式有以下几种:
    //ACCESS_UNKNOWN : 未指定具体的读取模式
    //ACCESS_RANDOM : 随机读取
    //ACCESS_STREAMING : 顺序读取
    //ACCESS_BUFFER : 缓存读取
    void close()//关闭AssetManager实例
  3. 使用
    assets目录下主要存放四种文件:文本文件、图像文件、网页文件(包括html中引用的js/ccs/jpg等资源)、音频视频文件,下面通过具体的案例分别来说下怎么获取

    • 加载assets目录下的网页
      1
      webView.loadUrl("file:///android_asset/html/index.htmll");

说明:这种方式可以加载assets目录下的网页,并且与网页有关的css,js,图片等文件也会的加载。

  • 加载assets目录下的图片资源

    1
    2
    3
    InputStream is = getAssets().open(fileName);  
    bitmap = BitmapFactory.decodeStream(is);
    ivImg.setImageBitmap(bitmap);
  • 加载assets目录下文本文件

    1
    2
    3
    4
    5
    InputStream is = getAssets().open(fileName);  
    int lenght = is.available();
    byte[] buffer = new byte[lenght];
    is.read(buffer);
    String result = = new String(buffer, "utf8");
  • 加载assets目录下音乐

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    // 打开指定音乐文件,获取assets目录下指定文件的AssetFileDescriptor对象  
    AssetFileDescriptor afd = am.openFd(music);
    mPlayer.reset();
    // 使用MediaPlayer加载指定的声音文件。
    mPlayer.setDataSource(afd.getFileDescriptor(),
    afd.getStartOffset(), afd.getLength());
    // 准备声音
    mPlayer.prepare();
    // 播放
    mPlayer.start();

补充下:Android中还有另外一个文件夹raw,和assets差不多,也不会被R文件编译,但是raw下不能在建文件夹,assets文件下是可以在建文件夹的,下面是获取raw文件夹下资源的方法:

1
InputStream is = getResources().openRawResource(R.id.filename);

将assets下的文件复制到SD卡中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**  
* 从assets目录中复制整个文件夹内容
* @param context Context 使用CopyFiles类的Activity
* @param oldPath String 原文件路径 如:/aa
* @param newPath String 复制后路径 如:xx:/bb/cc
*/
public void copyFilesFassets(Context context,String oldPath,String newPath) {
try {
String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名
if (fileNames.length > 0) {//如果是目录
File file = new File(newPath);
file.mkdirs();//如果文件夹不存在,则递归
for (String fileName : fileNames) {
copyFilesFassets(context,oldPath + "/" + fileName,newPath+"/"+fileName);
}
} else {//如果是文件
InputStream is = context.getAssets().open(oldPath);
FileOutputStream fos = new FileOutputStream(new File(newPath));
byte[] buffer = new byte[1024];
int byteCount=0;
while((byteCount=is.read(buffer))!=-1) {//循环从输入流读取 buffer字节
fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
}
fos.flush();//刷新缓冲区
is.close();
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//如果捕捉到错误则通知UI线程
MainActivity.handler.sendEmptyMessage(COPY_FALSE);
}
}

补充:Android获取assets或者raw目录的视频文件路径播放视频

1
2
3
4
String mp4FilePath = "file:///android_asset/Kotlin与ava共存.mp4";
mVideoView = (VideoView) findViewById(R.id.mVideoView);
mVideoView.setVideoPath(mp4FilePath);
mVideoView.start();

会提示播放失败。因为通过assets目录构造URI,不能用来播放视频,也不能播放音频。在raw目录下的文件构造URI可以播放音频,也能播放视频。 .
正确的是:

1
2
3
4
5
6
String uri = "android.resource://" + getPackageName() + "/" + R.raw.video;
mVideoView = (VideoView) findViewById(R.id.mVideoView);
mVideoView.setVideoPath(uri);
//mVideoView.setVideoPath(uri);
mVideoView.setVideoURI(Uri.parse(uri));
mVideoView.start();

参考:
[1] Android中读取assets目录下的文件详细介绍 https://blog.csdn.net/greathfs/article/details/52123984
[2] assets文件读取 https://www.cnblogs.com/wgscd/p/6738818.html
[3] asset视频播放 https://blog.csdn.net/e_inch_photo/article/details/78306785?utm_source=debugrun&utm_medium=referral