资料
android-30注释
MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
// More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
// or MediaExtractor.getTrackFormat()
MediaFormat audioFormat = new MediaFormat(...);
MediaFormat videoForamt = new MediaFormat(...);
int audioTrackIndex = muxer.addTrack(audioFormat);
int videoTrackIndex = muxer.addTrack(videoForamt);
ByteBuffer inputBuffer = ByteBuffer.allcate(bufferSize);
boolean finished = false;
BufferInfo bufferInfo = new BufferInfo();
muxer.start();
while(!finished) {
// getInputBuffer() will fill the inputBuffer with one frame of encoded
// sample from either MediaCodec or MediaExtractor, set isAudioSample to
// true when the sample is audio data. set up all the fields of bufferInfo,
// and return true if there are no more samples.
finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
if(!finished) {
int currentTrackIndex = isAudioSample ? audioTrackIndex : videoForamt;
muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
}
};
muxer.stop();
muxer.release
每帧元数据可用于携带与视频或音频相关的额外信息以促进离线处理,例如 在进行离线处理时,来自传感器的陀螺仪信号可以帮助视频稳定。
元数据轨道仅在 MP4 容器中受支持。
添加新的元数据轨道时,轨道的 mime 格式必须以前缀“application/”开头,例如 “应用程序/陀螺仪”。
元数据的格式/布局将由应用程序定义
写入元数据与写入视频/音频数据几乎相同,只是数据不会来自 mediacodec.
应用程序只需要将包含元数据的字节缓冲区以及相关的时间戳传递给 WriteSampleData api。
时间戳必须与视频和音频在同一时基
生成的 MP4 文件使用 ISOBMFF 第 12.3.3.2 节中定义的 TextMetaDataSampleEntry 来表示元数据的 mime 格式.
当使用 MediaExtractor 提取带有元数据轨道的文件时,元数据的 mime 格式将被提取到 MediaFormat
MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
// setup video/audio tracks.
MediaFormat audioFormat = new MediaFormat(...);
MediaFormat videoFormat = new MediaFormat(...);
int audioTrackIndex = muxer.addTrack(audioFormat);
int vidoeTrackIndex = muxer.addTrack(videoFormat);
// setup Metadata Track;
MediaFormat metadataFormat = new MediaFormat(...);
metadataFormat.setString(KEY_MIME, "application/gyro");
int metadataTrackIndex = muxer.addTrack(metadataFormat);
muxer.start();
while(...) {
// Allocate bytebuffer and write gyro data(x,y,z) into it.
ByteBuffer metaData = ByteBuffer.allocate(bufferSize);
metaData.putFloat(x);
metaData.putFloat(y);
metaData.putFloat(z);
BufferInfo metaInfo = new BufferInfo();
// Associate this metadata with the video frame by setString
// the same timestamp as the video frame.
metaInfo.presentationTimeUs = currentVideoTrackTimeUs;
metaInfo.offset = 0;
metaInfo.flags = 0;
metaInfo.size = bufferSize;
mux.writeSampleData(metadataTrackIndex, metaData, metaData);
}
muxer.stop();
muxer.release();