@UnstableApi
public final class Mp4Muxer implements Muxer


A muxer for creating an MP4 container file.

The muxer supports writing H264, H265 and AV1 video, AAC audio and 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.

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

Behavior for the last sample duration.

Constants

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

Insert a zero-length last sample.

Public methods

void

Adds metadata for 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

LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION

public static final int LAST_FRAME_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_FRAME_DURATION_BEHAVIOR_INSERT_SHORT_FRAME

public static final int LAST_FRAME_DURATION_BEHAVIOR_INSERT_SHORT_FRAME = 0

Insert a zero-length last sample.

Public methods

addMetadata

public void addMetadata(Metadata.Entry metadata)

Adds metadata for the output file.

List of supported metadata entries:

Parameters
Metadata.Entry metadata

The metadata. An IllegalArgumentException 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.

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.

close

public void close()

Closes the file.

writeSampleData

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

Writes encoded sample data.

The samples are cached and are written in batches so the caller must not change the and the BufferInfo after calling this method.

Note: Out of order B-frames are currently not supported.

Parameters
Muxer.TrackToken trackToken

The TrackToken for which this sample is being written.

ByteBuffer byteBuffer

The encoded sample.

MediaCodec.BufferInfo bufferInfo

The BufferInfo related to this sample.

Throws
java.io.IOException

If there is any error while writing data to the disk.