public int getTrack(MediaExtractor extractor,String mime)
{
for(int i = 0 ; i < extractor.getTrackCount();i++)
{
MediaFormat format = extractor.getTrackFormat(i);
String key_mime = format.getString(KEY_MIME);
if(key_mime.startsWith(mime))
{
return i;
}
}
return -1;
}
public int mergeVideo(String videoFile,String audioFile,String outMp4File){
MediaExtractor extractor = new MediaExtractor();
MediaExtractor audioExtractor = new MediaExtractor();
try {
extractor.setDataSource(videoFile);
audioExtractor.setDataSource(audioFile);
}catch (IOException ex)
{
return -1;
}
int videoTrack = getTrack(extractor,"video/");
int audioTrack = getTrack(audioExtractor,"audio/");
if(videoTrack == -1)
{
return -1;
}
if(audioTrack == -1)
{
return -1;
}
extractor.selectTrack(videoTrack);
audioExtractor.selectTrack(audioTrack);
MediaFormat videoFormat = extractor.getTrackFormat(videoTrack);
MediaFormat audioFormat = audioExtractor.getTrackFormat(audioTrack);
MediaMuxer muxer = null;
try {
muxer = new MediaMuxer(outMp4File, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
}catch(IOException ex)
{
return -1;
}
int videoIndex = muxer.addTrack(videoFormat);
int audioIndex = muxer.addTrack(audioFormat);
ByteBuffer videoBuffer = ByteBuffer.allocate(1024*1024*5);
ByteBuffer audioBuffer = ByteBuffer.allocate(1024*1024*5);
muxer.start();
boolean videoEnd = false;
boolean audioEnd = false;
while(true)
{
int vRet = extractor.readSampleData(videoBuffer,0);
if(vRet == -1)
{
videoEnd = true;
}
long vTime = extractor.getSampleTime();
int vFlags = extractor.getSampleFlags();
int aRet = audioExtractor.readSampleData(audioBuffer,0);
if(aRet == -1)
{
audioEnd = true;
}
long aTime = audioExtractor.getSampleTime();
int aFlags = audioExtractor.getSampleFlags();
if(videoEnd && audioEnd)
{
MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
bi.presentationTimeUs = 0;
bi.offset = 0;
bi.size = 0;
bi.flags = BUFFER_FLAG_END_OF_STREAM;
muxer.writeSampleData(videoIndex,videoBuffer,bi);
muxer.writeSampleData(audioIndex,audioBuffer,bi);
break;
}
if(videoEnd)
{
//Log.d(TAG,"time: videoEnd:" + aTime + " audio Size:" + aRet);
MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
bi.presentationTimeUs = aTime;
bi.offset = 0;
bi.size = aRet;
bi.flags = aFlags;
muxer.writeSampleData(audioIndex,audioBuffer,bi);
audioExtractor.advance();
}
else if(audioEnd)
{
//Log.d(TAG,"time: audioEnd:" + vTime + " video Size:" + vRet);
MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
bi.presentationTimeUs = vTime;
bi.offset = 0;
bi.size = vRet;
bi.flags = vFlags;
muxer.writeSampleData(videoIndex,videoBuffer,bi);
extractor.advance();
}
else if(vTime <= aTime)
{
//Log.d(TAG,"time:V-A" + vTime + " " + aTime + " video Size:" + vRet);
MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
bi.presentationTimeUs = vTime;
bi.offset = 0;
bi.size = vRet;
bi.flags = vFlags;
muxer.writeSampleData(videoIndex,videoBuffer,bi);
extractor.advance();
}else
{
//Log.d(TAG,"time:V-A :" + vTime + " " + aTime + " audio Size:" + aRet);
MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
bi.presentationTimeUs = aTime;
bi.offset = 0;
bi.size = aRet;
bi.flags = aFlags;
muxer.writeSampleData(audioIndex,audioBuffer,bi);
audioExtractor.advance();
}
}
while(true)
{
try {
muxer.stop();
}catch(IllegalStateException ex)
{
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
break;
}
muxer.release();
//Log.d(TAG,"over.");
return 0;
}
采用此代码可直接将音频mp4和视频mp4合成成一份带音视频mp4