@UnstableApi
class Mp4Muxer


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.

A token representing an added track.

Constants

const Int

The default fragment duration for the fragmented MP4.

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.

const ImmutableList<String!>!

A list of supported audio sample mime types.

const ImmutableList<String!>!

A list of supported video sample mime types.

Public functions

Unit
addMetadata(key: String!, value: Any!)

Adds custom metadata.

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

Adds a track of the given media format.

Unit

Adds xmp data.

Unit

Closes the MP4 file.

Unit
setCaptureFps(captureFps: Float)

Sets the capture frame rate.

Unit
setLocation(
    latitude: @FloatRange(from = "-90.0", to = 90.0) Float,
    longitude: @FloatRange(from = "-180.0", to = 180.0) Float
)

Sets the location.

Unit
setModificationTime(timestampMs: Long)

Sets the file modification time.

Unit
setOrientation(orientation: Int)

Sets the orientation hint for the video playback.

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

Writes encoded sample data.

Constants

DEFAULT_FRAGMENT_DURATION_US

const val DEFAULT_FRAGMENT_DURATION_US = 2000000: Int

The default fragment duration for the fragmented MP4.

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.

SUPPORTED_AUDIO_SAMPLE_MIME_TYPES

const val SUPPORTED_AUDIO_SAMPLE_MIME_TYPESImmutableList<String!>!

A list of supported audio sample mime types.

SUPPORTED_VIDEO_SAMPLE_MIME_TYPES

const val SUPPORTED_VIDEO_SAMPLE_MIME_TYPESImmutableList<String!>!

A list of supported video sample mime types.

Public functions

addMetadata

fun addMetadata(key: String!, value: Any!): Unit

Adds custom metadata.

Parameters
key: String!

The metadata key in String format.

value: Any!

The metadata value in String or Float format.

addTrack

fun addTrack(sortKey: Int, format: Format!): Mp4Muxer.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
Mp4Muxer.TrackToken!

A unique TrackToken. It should be used in writeSampleData.

addXmp

fun addXmp(xmp: ByteBuffer!): Unit

Adds xmp data.

Parameters
xmp: ByteBuffer!

The xmp ByteBuffer.

close

fun close(): Unit

Closes the MP4 file.

setCaptureFps

fun setCaptureFps(captureFps: Float): Unit

Sets the capture frame rate.

Parameters
captureFps: Float

The frame rate.

setLocation

fun setLocation(
    latitude: @FloatRange(from = "-90.0", to = 90.0) Float,
    longitude: @FloatRange(from = "-180.0", to = 180.0) Float
): Unit

Sets the location.

Parameters
latitude: @FloatRange(from = "-90.0", to = 90.0) Float

The latitude, in degrees. Its value must be in the range [-90, 90].

longitude: @FloatRange(from = "-180.0", to = 180.0) Float

The longitude, in degrees. Its value must be in the range [-180, 180].

setModificationTime

fun setModificationTime(timestampMs: Long): Unit

Sets the file modification time.

Parameters
timestampMs: Long

The modification time UTC in milliseconds since the Unix epoch.

setOrientation

fun setOrientation(orientation: Int): Unit

Sets the orientation hint for the video playback.

Parameters
orientation: Int

The orientation, in degrees.

writeSampleData

fun writeSampleData(
    trackToken: Mp4Muxer.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/release the ByteBuffer and the BufferInfo after calling this method.

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

Parameters
trackToken: Mp4Muxer.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.