FragmentedMp4Muxer
@UnstableApi
public final class FragmentedMp4Muxer implements Muxer
A muxer for creating a fragmented MP4 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 a fragmented MP4 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:
- All tracks must be added before writing any samples.
- 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.
Summary
Nested types |
|---|
public final class FragmentedMp4Muxer.BuilderA builder for |
Constants |
|
|---|---|
static final long |
DEFAULT_FRAGMENT_DURATION_MS = 2000The default fragment duration. |
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. |
void |
close()Closes the file. |
void |
writeSampleData(Writes encoded sample data. |
Inherited methods |
||
|---|---|---|
|
Constants
DEFAULT_FRAGMENT_DURATION_MS
public static final long DEFAULT_FRAGMENT_DURATION_MS = 2000
The default fragment duration.
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.
| Returns | |
|---|---|
int |
A track id for this track, which should be passed to |
| Throws | |
|---|---|
androidx.media3.muxer.MuxerException |
If the muxer encounters a problem while adding the 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.
Samples are written to the disk in batches. If sample copying is disabled, the byteBuffer and the bufferInfo must not be modified after calling this method. Otherwise, they are copied and it is safe to modify them after this method returns.
Note: Out of order B-frames are currently not supported.
| Parameters | |
|---|---|
int trackId |
The track id for which this sample is being written. |
ByteBuffer byteBuffer |
The encoded sample. The muxer takes ownership of the buffer if |
BufferInfo bufferInfo |
The |
| Throws | |
|---|---|
androidx.media3.muxer.MuxerException |
If there is any error while writing data to the disk. |