MediaMuxerCompat


public final class MediaMuxerCompat


A drop-in replacement for MediaMuxer that provides similar functionality, based on the media3.muxer logic.

Currently only MP4 file format is supported.

Supported codecs are:

  • Video Codecs:
    • AV1
    • MPEG-4
    • H.263
    • H.264 (AVC)
    • H.265 (HEVC)
    • VP9
    • APV
    • Dolby Vision
  • Audio Codecs:
    • AAC
    • AMR-NB (Narrowband AMR)
    • AMR-WB (Wideband AMR)
    • Opus
    • Vorbis
    • Raw Audio

All the methods should be called from the same thread.

All the operations are performed on the caller thread.

Summary

Nested types

The output file format.

Constants

static final int

The MP4 file format.

Public constructors

MediaMuxerCompat(
    FileDescriptor fileDescriptor,
    @MediaMuxerCompat.OutputFormat int outputFormat
)

Creates an instance.

MediaMuxerCompat(
    String filePath,
    @MediaMuxerCompat.OutputFormat int outputFormat
)

Creates an instance.

Public methods

int

Adds a track of the given media format.

void

Releases the underlying resources.

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

Sets the location of the media file.

void
setOrientationHint(int degrees)

Sets the orientation hint for the media file.

void

Starts the muxer.

void

Stops the muxer.

void
writeSampleData(
    int trackIndex,
    ByteBuffer byteBuffer,
    MediaCodec.BufferInfo bufferInfo
)

Writes encoded sample data.

Constants

OUTPUT_FORMAT_MP4

public static final int OUTPUT_FORMAT_MP4 = 0

The MP4 file format.

Public constructors

MediaMuxerCompat

public MediaMuxerCompat(
    FileDescriptor fileDescriptor,
    @MediaMuxerCompat.OutputFormat int outputFormat
)

Creates an instance.

It is the caller's responsibility to close the fileDescriptor, which is safe to do so as soon as this call returns.

Parameters
FileDescriptor fileDescriptor

A FileDescriptor for the output media file. It must represent a local file that is open in read-write mode.

@MediaMuxerCompat.OutputFormat int outputFormat

The OutputFormat.

Throws
java.io.IOException

If an error occurs while performing an I/O operation.

MediaMuxerCompat

public MediaMuxerCompat(
    String filePath,
    @MediaMuxerCompat.OutputFormat int outputFormat
)

Creates an instance.

Parameters
String filePath

The path of the output media file.

@MediaMuxerCompat.OutputFormat int outputFormat

The OutputFormat.

Throws
java.io.IOException

If an error occurs while performing an I/O operation.

Public methods

addTrack

public int addTrack(MediaFormat format)

Adds a track of the given media format.

All tracks must be added before any samples are written to any track.

KEY_CAPTURE_RATE is used to write KEY_ANDROID_CAPTURE_FPS metadata in the MP4 file.

Parameters
MediaFormat format

The MediaFormat of the track.

Returns
int

A track index for this track, which should be passed to writeSampleData.

See also
addTrack

release

public void release()

Releases the underlying resources.

It should be called after stop.

See also
release

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 of the media file.

It must be called before start.

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

See also
setLocation

setOrientationHint

public void setOrientationHint(int degrees)

Sets the orientation hint for the media file.

It must be called before start.

Parameters
int degrees

The orientation, in degrees. The supported values are 0, 90, 180 and 270 (degrees).

start

public void start()

Starts the muxer.

This must be called after addTrack and before writeSampleData.

See also
start

stop

public void stop()

Stops the muxer.

Once the muxer is stopped, it can not be restarted.

See also
stop

writeSampleData

public void writeSampleData(
    int trackIndex,
    ByteBuffer byteBuffer,
    MediaCodec.BufferInfo bufferInfo
)

Writes encoded sample data.

To set the duration of the last sample in a track, an additional empty buffer ( bufferInfo.size = 0) with BUFFER_FLAG_END_OF_STREAM flag and a suitable presentation timestamp must be passed as the last sample of that track. This timestamp should be the sum of the desired duration and the presentation timestamp of the original last sample. If no explicit END_OF_STREAM sample is provided, the last sample's duration will be equal to the previous sample's duration.

Parameters
int trackIndex

The track index, previously returned by addTrack.

ByteBuffer byteBuffer

A buffer containing the sample data to write.

MediaCodec.BufferInfo bufferInfo

The MediaCodec.BufferInfo of the sample.

See also
writeSampleData