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

public final class Mp4Muxer.Builder

A builder for Mp4Muxer instances.

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

Behavior for the last sample duration.

public interface Mp4Muxer.TrackToken

A token representing an added track.

Constants

static final int

The default fragment duration for the fragmented MP4.

static final int

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

static final int

Insert a zero-length last sample.

static final ImmutableList<String>

A list of supported audio sample mime types.

static final ImmutableList<String>

A list of supported video sample mime types.

Public methods

void
addMetadata(String key, Object value)

Adds custom metadata.

Mp4Muxer.TrackToken
addTrack(int sortKey, Format format)

Adds a track of the given media format.

void

Adds xmp data.

void

Closes the MP4 file.

void
setCaptureFps(float captureFps)

Sets the capture frame rate.

void
setLocation(
    @FloatRange(from = "-90.0", to = 90.0) float latitude,
    @FloatRange(from = "-180.0", to = 180.0) float longitude
)

Sets the location.

void
setModificationTime(long timestampMs)

Sets the file modification time.

void
setOrientation(int orientation)

Sets the orientation hint for the video playback.

void
writeSampleData(
    Mp4Muxer.TrackToken trackToken,
    ByteBuffer byteBuffer,
    MediaCodec.BufferInfo bufferInfo
)

Writes encoded sample data.

Constants

DEFAULT_FRAGMENT_DURATION_US

public static final int DEFAULT_FRAGMENT_DURATION_US = 2000000

The default fragment duration for the fragmented MP4.

LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION

public static final int LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION = 1

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

public static final int LAST_FRAME_DURATION_BEHAVIOR_INSERT_SHORT_FRAME = 0

Insert a zero-length last sample.

SUPPORTED_AUDIO_SAMPLE_MIME_TYPES

public static final ImmutableList<StringSUPPORTED_AUDIO_SAMPLE_MIME_TYPES

A list of supported audio sample mime types.

SUPPORTED_VIDEO_SAMPLE_MIME_TYPES

public static final ImmutableList<StringSUPPORTED_VIDEO_SAMPLE_MIME_TYPES

A list of supported video sample mime types.

Public methods

addMetadata

public void addMetadata(String key, Object value)

Adds custom metadata.

Parameters
String key

The metadata key in String format.

Object value

The metadata value in String or Float format.

addTrack

public Mp4Muxer.TrackToken addTrack(int sortKey, Format format)

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
int sortKey

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

public void addXmp(ByteBuffer xmp)

Adds xmp data.

Parameters
ByteBuffer xmp

The xmp ByteBuffer.

close

public void close()

Closes the MP4 file.

setCaptureFps

public void setCaptureFps(float captureFps)

Sets the capture frame rate.

Parameters
float captureFps

The frame rate.

setLocation

public void setLocation(
    @FloatRange(from = "-90.0", to = 90.0) float latitude,
    @FloatRange(from = "-180.0", to = 180.0) float longitude
)

Sets the location.

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

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

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

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

setModificationTime

public void setModificationTime(long timestampMs)

Sets the file modification time.

Parameters
long timestampMs

The modification time UTC in milliseconds since the Unix epoch.

setOrientation

public void setOrientation(int orientation)

Sets the orientation hint for the video playback.

Parameters
int orientation

The orientation, in degrees.

writeSampleData

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

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

Parameters
Mp4Muxer.TrackToken trackToken

The TrackToken for which this sample is being written.

ByteBuffer byteBuffer

The encoded sample.

MediaCodec.BufferInfo bufferInfo

The BufferInfo related to this sample.

Throws
java.io.IOException

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