Android 视频的分离和合成这方面的资源网上是少之又少,学习的过程也是各种demo的看,各种demo的实验,各种坑的踩,为了让大家能够android的视频的分离和合成又个了解,写了下面的例子,完整例子再最下面。例子写的比较草,中间有较多的容错处理和代码的重构,也就懒得做了。另外导入的项目的时候,需要将工程input.mp4导入sd卡中,放在根目录里,由代码可以知道。
理论上来,我们可以利用的视频的分离和合成可以做很多事,比如裁剪视频大小,拼凑视频,对一段视频加入背景等。
MediaExtractor facilitates extraction of demuxed, typically encoded, media data from a data source.
其实就是将视频多信道的分离出来,比如声音和图像分隔出来,可以做到二次合成。
1.实例对象
2.setDateSource();//本地或者远程
3.获取想追踪的TrackIndex;
4.对TrackIndex的信道ByteBuffer数据进行处理
接下来讲一个mp4文件进行处理,分隔出audio信道和video信道
private void exactorMedia() {
FileOutputStream videoOutputStream = null;
FileOutputStream audioOutputStream = null;
try {
//分离的视频文件
File videoFile = new File(SDCARD_PATH, "output_video.mp4");
//分离的音频文件
File audioFile = new File(SDCARD_PATH, "output_audio");
videoOutputStream = new FileOutputStream(videoFile);
audioOutputStream = new FileOutputStream(audioFile);
//源文件
mediaExtractor.setDataSource(SDCARD_PATH + "/input.mp4");
//信道总数
int trackCount = mediaExtractor.getTrackCount();
int audioTrackIndex = -1;
int videoTrackIndex = -1;
for (int i = 0; i < trackCount; i++) {
MediaFormat trackFormat = mediaExtractor.getTrackFormat(i);
String mineType = trackFormat.getString(MediaFormat.KEY_MIME);
//视频信道
if (mineType.startsWith("video/")) {
videoTrackIndex = i;
}
//音频信道
if (mineType.startsWith("audio/")) {
audioTrackIndex = i;
}
}
ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
//切换到视频信道
mediaExtractor.selectTrack(videoTrackIndex);
while (true) {
int readSampleCount = mediaExtractor.readSampleData(byteBuffer, 0);
if (readSampleCount < 0) {
break;
}
//保存视频信道信息
byte[] buffer = new byte[readSampleCount];
byteBuffer.get(buffer);
videoOutputStream.write(buffer);
byteBuffer.clear();
mediaExtractor.advance();
}
//切换到音频信道
mediaExtractor.selectTrack(audioTrackIndex);
while (true) {
int readSampleCount = mediaExtractor.readSampleData(byteBuffer, 0);
if (readSampleCount < 0) {
break;
}
//保存音频信息
byte[] buffer = new byte[readSampleCount];
byteBuffer.get(buffer);
audioOutputStream.write(buffer);
byteBuffer.clear();
mediaExtractor.advance();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
mediaExtractor.release();
try {
videoOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但是我们注意分离出的两个文件大小其实是不相等的。
inputSize:243104
audioSize:40720
videoSize:199555
上面是分离视频的,下面是合成视频。将所有的信道的信息合成一个视频
下面的例子基本涵盖所有的使用情况
输入视频是input.mp4 ,分离保存的视频output_video;
private void muxerMedia() {
mediaExtractor = new MediaExtractor();
int videoIndex = -1;
try {
mediaExtractor.setDataSource(SDCARD_PATH + "/input.mp4");
int trackCount = mediaExtractor.getTrackCount();
for (int i = 0; i < trackCount; i++) {
MediaFormat trackFormat = mediaExtractor.getTrackFormat(i);
String mimeType = trackFormat.getString(MediaFormat.KEY_MIME);
// 取出视频的信号
if (mimeType.startsWith("video/")) {
videoIndex = i;
}
}
//切换道视频信号的信道
mediaExtractor.selectTrack(videoIndex);
MediaFormat trackFormat = mediaExtractor.getTrackFormat(videoIndex);
mediaMuxer = new MediaMuxer(SDCARD_PATH + "/output_video.mp4", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
//追踪此信道
int trackIndex = mediaMuxer.addTrack(trackFormat);
ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 500);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
mediaMuxer.start();
long videoSampleTime;
//获取每帧的之间的时间
{
mediaExtractor.readSampleData(byteBuffer, 0);
//skip first I frame
if (mediaExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC)
mediaExtractor.advance();
mediaExtractor.readSampleData(byteBuffer, 0);
long firstVideoPTS = mediaExtractor.getSampleTime();
mediaExtractor.advance();
mediaExtractor.readSampleData(byteBuffer, 0);
long SecondVideoPTS = mediaExtractor.getSampleTime();
videoSampleTime = Math.abs(SecondVideoPTS - firstVideoPTS);
Log.d("fuck", "videoSampleTime is " + videoSampleTime);
}
//重新切换此信道,不然上面跳过了3帧,造成前面的帧数模糊
mediaExtractor.unselectTrack(videoIndex);
mediaExtractor.selectTrack(videoIndex);
while (true) {
//读取帧之间的数据
int readSampleSize = mediaExtractor.readSampleData(byteBuffer, 0);
if (readSampleSize < 0) {
break;
}
mediaExtractor.advance();
bufferInfo.size = readSampleSize;
bufferInfo.offset = 0;
bufferInfo.flags = mediaExtractor.getSampleFlags();
bufferInfo.presentationTimeUs += videoSampleTime;
//写入帧的数据
mediaMuxer.writeSampleData(trackIndex, byteBuffer, bufferInfo);
}
//release
mediaMuxer.stop();
mediaExtractor.release();
mediaMuxer.release();
Log.e("TAG", "finish");
} catch (IOException e) {
e.printStackTrace();
}
}
这里比上面文件的要大,因为保存了一些信道信息等,而且是可以播放。
输入视频是input.mp4,输出的音频为output_audio;
private void muxerAudio() {
mediaExtractor = new MediaExtractor();
int audioIndex = -1;
try {
mediaExtractor.setDataSource(SDCARD_PATH + "/input.mp4");
int trackCount = mediaExtractor.getTrackCount();
for (int i = 0; i < trackCount; i++) {
MediaFormat trackFormat = mediaExtractor.getTrackFormat(i);
if (trackFormat.getString(MediaFormat.KEY_MIME).startsWith("audio/")) {
audioIndex = i;
}
}
mediaExtractor.selectTrack(audioIndex);
MediaFormat trackFormat = mediaExtractor.getTrackFormat(audioIndex);
mediaMuxer = new MediaMuxer(SDCARD_PATH + "/output_audios", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
int writeAudioIndex = mediaMuxer.addTrack(trackFormat);
mediaMuxer.start();
ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
long stampTime = 0;
//获取帧之间的间隔时间
{
mediaExtractor.readSampleData(byteBuffer, 0);
if (mediaExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {
mediaExtractor.advance();
}
mediaExtractor.readSampleData(byteBuffer, 0);
long secondTime = mediaExtractor.getSampleTime();
mediaExtractor.advance();
mediaExtractor.readSampleData(byteBuffer, 0);
long thirdTime = mediaExtractor.getSampleTime();
stampTime = Math.abs(thirdTime - secondTime);
Log.e("fuck", stampTime + "");
}
mediaExtractor.unselectTrack(audioIndex);
mediaExtractor.selectTrack(audioIndex);
while (true) {
int readSampleSize = mediaExtractor.readSampleData(byteBuffer, 0);
if (readSampleSize < 0) {
break;
}
mediaExtractor.advance();
bufferInfo.size = readSampleSize;
bufferInfo.flags = mediaExtractor.getSampleFlags();
bufferInfo.offset = 0;
bufferInfo.presentationTimeUs += stampTime;
mediaMuxer.writeSampleData(writeAudioIndex, byteBuffer, bufferInfo);
}
mediaMuxer.stop();
mediaMuxer.release();
mediaExtractor.release();
Log.e("fuck", "finish");
} catch (IOException e) {
e.printStackTrace();
}
}
基本原理和上面差不多,只是我们这次选中音频的信道
将上面分离出的output_video和分离出output_audio合成原来完整的视频
输入视频output_video,输入音频是output_audio,合成视频output
private void combineVideo() {
try {
MediaExtractor videoExtractor = new MediaExtractor();
videoExtractor.setDataSource(SDCARD_PATH + "/output_video");
MediaFormat videoFormat = null;
int videoTrackIndex = -1;
int videoTrackCount = videoExtractor.getTrackCount();
for (int i = 0; i < videoTrackCount; i++) {
videoFormat = videoExtractor.getTrackFormat(i);
String mimeType = videoFormat.getString(MediaFormat.KEY_MIME);
if (mimeType.startsWith("video/")) {
videoTrackIndex = i;
break;
}
}
MediaExtractor audioExtractor = new MediaExtractor();
audioExtractor.setDataSource(SDCARD_PATH + "/output_audio");
MediaFormat audioFormat = null;
int audioTrackIndex = -1;
int audioTrackCount = audioExtractor.getTrackCount();
for (int i = 0; i < audioTrackCount; i++) {
audioFormat = audioExtractor.getTrackFormat(i);
String mimeType = audioFormat.getString(MediaFormat.KEY_MIME);
if (mimeType.startsWith("audio/")) {
audioTrackIndex = i;
break;
}
}
videoExtractor.selectTrack(videoTrackIndex);
audioExtractor.selectTrack(audioTrackIndex);
MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();
MediaMuxer mediaMuxer = new MediaMuxer(SDCARD_PATH + "/output", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
int writeVideoTrackIndex = mediaMuxer.addTrack(videoFormat);
int writeAudioTrackIndex = mediaMuxer.addTrack(audioFormat);
mediaMuxer.start();
ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
long sampleTime = 0;
{
videoExtractor.readSampleData(byteBuffer, 0);
if (videoExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {
videoExtractor.advance();
}
videoExtractor.readSampleData(byteBuffer, 0);
long secondTime = videoExtractor.getSampleTime();
videoExtractor.advance();
long thirdTime = videoExtractor.getSampleTime();
sampleTime = Math.abs(thirdTime - secondTime);
}
videoExtractor.unselectTrack(videoTrackIndex);
videoExtractor.selectTrack(videoTrackIndex);
while (true) {
int readVideoSampleSize = videoExtractor.readSampleData(byteBuffer, 0);
if (readVideoSampleSize < 0) {
break;
}
videoBufferInfo.size = readVideoSampleSize;
videoBufferInfo.presentationTimeUs += sampleTime;
videoBufferInfo.offset = 0;
videoBufferInfo.flags = videoExtractor.getSampleFlags();
mediaMuxer.writeSampleData(writeVideoTrackIndex, byteBuffer, videoBufferInfo);
videoExtractor.advance();
}
while (true) {
int readAudioSampleSize = audioExtractor.readSampleData(byteBuffer, 0);
if (readAudioSampleSize < 0) {
break;
}
audioBufferInfo.size = readAudioSampleSize;
audioBufferInfo.presentationTimeUs += sampleTime;
audioBufferInfo.offset = 0;
audioBufferInfo.flags = videoExtractor.getSampleFlags();
mediaMuxer.writeSampleData(writeAudioTrackIndex, byteBuffer, audioBufferInfo);
audioExtractor.advance();
}
mediaMuxer.stop();
mediaMuxer.release();
videoExtractor.release();
audioExtractor.release();
} catch (IOException e) {
e.printStackTrace();
}
}
完整例子见MediaExtractor和MediaMuxer的Demo