FragmentedMp4Muxer


@UnstableApi
public final class FragmentedMp4Muxer implements 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

static final long

The default fragment duration.

Public constructors

Creates an instance with DEFAULT_FRAGMENT_DURATION_MS.

FragmentedMp4Muxer(
    FileOutputStream fileOutputStream,
    long fragmentDurationMs
)

Creates an instance.

Public methods

void

Adds metadata for the output file.

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

DEFAULT_FRAGMENT_DURATION_MS

public static final long DEFAULT_FRAGMENT_DURATION_MS = 2000

The default fragment duration.

Public constructors

FragmentedMp4Muxer

public FragmentedMp4Muxer(FileOutputStream fileOutputStream)

Creates an instance with DEFAULT_FRAGMENT_DURATION_MS.

FragmentedMp4Muxer

public FragmentedMp4Muxer(
    FileOutputStream fileOutputStream,
    long fragmentDurationMs
)

Creates an instance.

Parameters
FileOutputStream fileOutputStream

The FileOutputStream to write the media data to.

long fragmentDurationMs

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 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.

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

Throws
java.io.IOException

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