@UnstableApi
class Mp4Muxer : 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

A builder for Mp4Muxer instances.

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

Behavior for the last sample duration.

Constants

const Int

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

const Int

Insert a zero-length last sample.

Public functions

Unit

Adds metadata for the output file.

Muxer.TrackToken!
addTrack(format: Format!)

Adds a track of the given media format.

Muxer.TrackToken!
addTrack(sortKey: Int, format: Format!)

Adds a track of the given media format.

Unit

Closes the file.

Unit
writeSampleData(
    trackToken: Muxer.TrackToken!,
    byteBuffer: ByteBuffer!,
    bufferInfo: MediaCodec.BufferInfo!
)

Writes encoded sample data.

Constants

LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION

const val LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION = 1: Int

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

const val LAST_FRAME_DURATION_BEHAVIOR_INSERT_SHORT_FRAME = 0: Int

Insert a zero-length last sample.

Public functions

addMetadata

fun addMetadata(metadata: Metadata.Entry!): Unit

Adds metadata for the output file.

List of supported metadata entries:

Parameters
metadata: Metadata.Entry!

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

addTrack

fun addTrack(format: Format!): Muxer.TrackToken!

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

fun addTrack(sortKey: Int, format: Format!): Muxer.TrackToken!

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
sortKey: Int

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

fun close(): Unit

Closes the file.

writeSampleData

fun writeSampleData(
    trackToken: Muxer.TrackToken!,
    byteBuffer: ByteBuffer!,
    bufferInfo: MediaCodec.BufferInfo!
): Unit

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
trackToken: Muxer.TrackToken!

The TrackToken for which this sample is being written.

byteBuffer: ByteBuffer!

The encoded sample.

bufferInfo: MediaCodec.BufferInfo!

The BufferInfo related to this sample.

Throws
java.io.IOException

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