FragmentedMp4Muxer


@UnstableApi
class FragmentedMp4Muxer : Muxer


A muxer for creating a fragmented MP4 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 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

Constants

const Long

The default fragment duration.

Public constructors

Creates an instance with DEFAULT_FRAGMENT_DURATION_MS.

FragmentedMp4Muxer(
    fileOutputStream: FileOutputStream!,
    fragmentDurationMs: Long
)

Creates an instance.

Public functions

Unit

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

FragmentedMp4Muxer

FragmentedMp4Muxer(fileOutputStream: FileOutputStream!)

Creates an instance with DEFAULT_FRAGMENT_DURATION_MS.

FragmentedMp4Muxer

FragmentedMp4Muxer(
    fileOutputStream: FileOutputStream!,
    fragmentDurationMs: Long
)

Creates an instance.

Parameters
fileOutputStream: FileOutputStream!

The FileOutputStream to write the media data to.

fragmentDurationMs: Long

The fragment duration (in milliseconds). The muxer will attempt to create fragments of the given duration but the actual duration might be greater depending upon the frequency of sync samples.

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.

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 MediaCodec.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 MediaCodec.BufferInfo related to this sample.

Throws
java.io.IOException

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