FragmentedMp4Muxer


@UnstableApi
class FragmentedMp4Muxer : 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
  • 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 a fragmented MP4 file, the caller must:

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 writeSampleData in an order that interleaves samples from different tracks.

Summary

Nested types

A builder for FragmentedMp4Muxer instances.

Constants

const Long

The default fragment duration.

Public functions

Unit

Adds metadata about the output file.

Muxer.TrackToken!
addTrack(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

DEFAULT_FRAGMENT_DURATION_MS

const val DEFAULT_FRAGMENT_DURATION_MS = 2000: Long

The default fragment duration.

Public functions

addMetadataEntry

fun addMetadataEntry(metadataEntry: Metadata.Entry!): Unit

Adds metadata about the output file.

List of supported metadata entries:

Parameters
metadataEntry: Metadata.Entry!

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

addTrack

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

Adds a track of the given media format.

Parameters
format: Format!

The Format of the track.

Returns
Muxer.TrackToken!

The TrackToken for this track, which should be passed to writeSampleData.

Throws
androidx.media3.muxer.Muxer.MuxerException

If the muxer encounters a problem while adding the track.

close

fun close(): Unit

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

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

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

bufferInfo: MediaCodec.BufferInfo!

The BufferInfo related to this sample.

Throws
androidx.media3.muxer.Muxer.MuxerException

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