Mp4Muxer
@UnstableApi
public final class Mp4Muxer implements Muxer
A muxer for creating an MP4 container file.
Muxer supports muxing of:
- Video Codecs:
- AV1
- MPEG-4
- H.263
- H.264 (AVC)
- H.265 (HEVC)
- VP9
- APV
- Dolby Vision
- Audio Codecs:
- AAC
- AMR-NB (Narrowband AMR)
- AMR-WB (Wideband AMR)
- Opus
- Vorbis
- Raw Audio
- Metadata
All the operations are performed on the caller thread.
To create an MP4 container file, the caller must:
- Add tracks using
addTrackwhich will return a track id. - Use the associated track id when
writing samplesfor that track. - close the muxer when all data has been written.
Some key points:
- Tracks can be added at any point, even after writing some samples to other tracks.
- The caller is responsible for ensuring that samples of different track types are well interleaved by calling
writeSampleDatain an order that interleaves samples from different tracks. - When writing a file, if an error occurs and the muxer is not closed, then the output MP4 file may still have some partial data.
Summary
Nested types |
|---|
public final class Mp4Muxer.BuilderA builder for |
@DocumentedThe specific MP4 file format. |
@DocumentedBehavior for the duration of the last sample. |
public final class Mp4Muxer.Mp4AtFileParametersParameters for |
Constants |
|
|---|---|
static final int |
The default MP4 format. |
static final int |
The MP4 With Auxiliary Tracks Extension (MP4-AT) file format. |
static final int |
Use the |
static final int |
The duration of the last sample is set to 0. |
static final ImmutableList<String> |
A list of supported audio |
static final ImmutableList<String> |
A list of supported video |
Public methods |
|
|---|---|
void |
addMetadataEntry(Metadata.Entry metadataEntry)Adds metadata about the output file. |
int |
Adds a track of the given media format. |
int |
Adds a track of the given media format. |
void |
close()Closes the file. |
void |
writeSampleData(Writes encoded sample data. |
Inherited methods |
||
|---|---|---|
|
Constants
FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION
public static final int FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION = 1
The MP4 With Auxiliary Tracks Extension (MP4-AT) file format. In this file format all the tracks with auxiliaryTrackType set to AUXILIARY_TRACK_TYPE_ORIGINAL, AUXILIARY_TRACK_TYPE_DEPTH_LINEAR, AUXILIARY_TRACK_TYPE_DEPTH_INVERSE, or AUXILIARY_TRACK_TYPE_DEPTH_METADATA are written in the Auxiliary Tracks MP4 (axte box). The rest of the tracks are written as usual.
See the file format at https://developer.android.com/media/platform/mp4-at-file-format.
LAST_SAMPLE_DURATION_BEHAVIOR_SET_FROM_END_OF_STREAM_BUFFER_OR_DUPLICATE_PREVIOUS
public static final int LAST_SAMPLE_DURATION_BEHAVIOR_SET_FROM_END_OF_STREAM_BUFFER_OR_DUPLICATE_PREVIOUS = 1
Use the end of stream sample to set the duration of the last sample.
After writing all the samples for a track, the app must write an empty sample with flag BUFFER_FLAG_END_OF_STREAM. The timestamp of this sample should be equal to the desired track duration.
Once a sample with flag BUFFER_FLAG_END_OF_STREAM is written, no more samples can be written for that track.
If no explicit BUFFER_FLAG_END_OF_STREAM sample is passed, then the duration of the last sample will be same as that of the sample before that.
LAST_SAMPLE_DURATION_BEHAVIOR_SET_TO_ZERO
public static final int LAST_SAMPLE_DURATION_BEHAVIOR_SET_TO_ZERO = 0
The duration of the last sample is set to 0.
SUPPORTED_AUDIO_SAMPLE_MIME_TYPES
public static final ImmutableList<String> SUPPORTED_AUDIO_SAMPLE_MIME_TYPES
A list of supported audio sample MIME types.
SUPPORTED_VIDEO_SAMPLE_MIME_TYPES
public static final ImmutableList<String> SUPPORTED_VIDEO_SAMPLE_MIME_TYPES
A list of supported video sample MIME types.
Public methods
addMetadataEntry
public void addMetadataEntry(Metadata.Entry metadataEntry)
Adds metadata about the output file.
List of supported metadata entries:
Mp4OrientationDataMp4LocationDataMp4TimestampDataMdtaMetadataEntry: Onlystring typeorfloat typevalue is supported.XmpData
| Parameters | |
|---|---|
Metadata.Entry metadataEntry |
The |
addTrack
public int addTrack(Format format)
Adds a track of the given media format.
All tracks must be added before any samples are written to any track.
Tracks can be added at any point before the muxer is closed, even after writing samples to other tracks.
The order of tracks remains same in which they are added.
| Returns | |
|---|---|
int |
A unique track id. The track id is non-negative. It should be used in |
| Throws | |
|---|---|
androidx.media3.muxer.MuxerException |
If an error occurs while adding track. |
addTrack
public int addTrack(int sortKey, Format format)
Adds a track of the given media format.
Tracks can be added at any point before the muxer is closed, even after writing samples to other tracks.
The final order of tracks is determined by the provided sort key. Tracks with a lower sort key will be written before tracks with a higher sort key. Ordering between tracks with the same sort key is not specified.
| Parameters | |
|---|---|
int sortKey |
The key used for sorting the track list. |
Format format |
The |
| Returns | |
|---|---|
int |
A unique track id. The track id is non-negative. It should be used in |
| Throws | |
|---|---|
androidx.media3.muxer.MuxerException |
If an error occurs while adding track. |
close
public void close()
Closes the file.
The muxer cannot be used anymore once this method returns.
| Throws | |
|---|---|
androidx.media3.muxer.MuxerException |
If the muxer fails to finish writing the output. |
writeSampleData
public void writeSampleData(
int trackId,
ByteBuffer byteBuffer,
BufferInfo bufferInfo
)
Writes encoded sample data.
The muxer's handling of sample ByteBuffer and BufferInfo depends on the sample batching and sample copying settings:
- If
sample batchingis disabled: Samples are written immediately upon arrival. The caller can safely modify or reuse these objects immediately after this method returns. - If
sample batchingis enabled:- If
sample copyingis enabled: The muxer makes internal copies of the providedByteBufferandBufferInfo. The caller can safely modify or reuse these objects immediately after this method returns. - If
sample copyingis disabled: The muxer takes ownership of theByteBufferandBufferInfo. The caller must not modify these objects after this method returns.
- If
| Parameters | |
|---|---|
int trackId |
The track id for which this sample is being written. |
ByteBuffer byteBuffer |
The encoded sample. |
BufferInfo bufferInfo |
The |
| Throws | |
|---|---|
androidx.media3.muxer.MuxerException |
If an error occurs while writing data to the output file. |