@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)
  • Audio Codecs:
    • AAC
    • AMR-NB (Narrowband AMR)
    • AMR-WB (Wideband AMR)
    • Opus
    • Vorbis
  • Metadata

All the operations are performed on the caller thread.

To create an MP4 container file, the caller must:

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 writeSampleData in 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.Builder

A builder for Mp4Muxer instances.

Parameters for FILE_FORMAT_EDITABLE_VIDEO.

Provides temporary cache files to be used by the muxer.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
public annotation Mp4Muxer.FileFormat

The specific MP4 file format.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
public annotation Mp4Muxer.LastSampleDurationBehavior

Behavior for the duration of the last sample.

Constants

static final int

The default MP4 format.

static final int

The editable video file format.

static final int

Use the difference between the last timestamp and the one before that as the duration of the last sample.

static final int

The duration of the last sample is set to 0.

static final int

Use the end of stream sample to set the duration of the last sample.

Public methods

void

Adds metadata about the output file.

Muxer.TrackToken
addTrack(Format format)

Adds a track of the given media format.

Muxer.TrackToken
addTrack(int sortKey, Format format)

Adds a track of the given media format.

void

Closes the file.

void
writeSampleData(
    Muxer.TrackToken trackToken,
    ByteBuffer byteBuffer,
    MediaCodec.BufferInfo bufferInfo
)

Writes encoded sample data.

Constants

FILE_FORMAT_DEFAULT

public static final int FILE_FORMAT_DEFAULT = 0

The default MP4 format.

FILE_FORMAT_EDITABLE_VIDEO

public static final int FILE_FORMAT_EDITABLE_VIDEO = 1

The editable video 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 MP4 edit data (edvd box). The rest of the tracks are written as usual.

LAST_SAMPLE_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION

public static final int LAST_SAMPLE_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION = 1

Use the difference between the last timestamp and the one before that as the duration of the last sample.

LAST_SAMPLE_DURATION_BEHAVIOR_INSERT_SHORT_SAMPLE

public static final int LAST_SAMPLE_DURATION_BEHAVIOR_INSERT_SHORT_SAMPLE = 0

The duration of the last sample is set to 0.

LAST_SAMPLE_DURATION_BEHAVIOR_USING_END_OF_STREAM_FLAG

public static final int LAST_SAMPLE_DURATION_BEHAVIOR_USING_END_OF_STREAM_FLAG = 2

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 set to 0.

Public methods

addMetadataEntry

public void addMetadataEntry(Metadata.Entry metadataEntry)

Adds metadata about the output file.

List of supported metadata entries:

Parameters
Metadata.Entry metadataEntry

The metadata. An is thrown if the metadata is not supported.

addTrack

public Muxer.TrackToken addTrack(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 order of tracks remains same in which they are added.

Parameters
Format format

The Format for the track.

Returns
Muxer.TrackToken

A unique TrackToken. It should be used in writeSampleData.

Throws
androidx.media3.muxer.Muxer.MuxerException

If an error occurs while adding track.

addTrack

public Muxer.TrackToken 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 always have a lower track id than 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 Format for the track.

Returns
Muxer.TrackToken

A unique TrackToken. It should be used in writeSampleData.

Throws
androidx.media3.muxer.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.Muxer.MuxerException

If the muxer fails to finish writing the output.

writeSampleData

public void writeSampleData(
    Muxer.TrackToken trackToken,
    ByteBuffer byteBuffer,
    MediaCodec.BufferInfo bufferInfo
)

Writes encoded sample data.

Samples are written to the file 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.

Parameters
Muxer.TrackToken trackToken

The TrackToken for which this sample is being written.

ByteBuffer byteBuffer

The encoded sample. The muxer takes ownership of the buffer if sample copying is disabled. Otherwise, the position of the buffer is updated but the caller retains ownership.

MediaCodec.BufferInfo bufferInfo

The BufferInfo related to this sample.

Throws
androidx.media3.muxer.Muxer.MuxerException

If an error occurs while writing data to the output file.