MediaMuxerCompat


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

const Int

The MP4 file format.

Public constructors

MediaMuxerCompat(
    fileDescriptor: FileDescriptor!,
    @MediaMuxerCompat.OutputFormat outputFormat: Int
)

Creates an instance.

MediaMuxerCompat(
    filePath: String!,
    @MediaMuxerCompat.OutputFormat outputFormat: Int
)

Creates an instance.

Public functions

Int

Adds a track of the given media format.

Unit

Releases the underlying resources.

Unit
setLocation(
    latitude: @FloatRange(from = "-90.0", to = 90.0) Float,
    longitude: @FloatRange(from = "-180.0", to = 180.0) Float
)

Sets the location of the media file.

Unit

Sets the orientation hint for the media file.

Unit

Starts the muxer.

Unit

Stops the muxer.

Unit
writeSampleData(
    trackIndex: Int,
    byteBuffer: ByteBuffer!,
    bufferInfo: MediaCodec.BufferInfo!
)

Writes encoded sample data.

Constants

OUTPUT_FORMAT_MP4

const val OUTPUT_FORMAT_MP4 = 0: Int

The MP4 file format.

Public constructors

MediaMuxerCompat

MediaMuxerCompat(
    fileDescriptor: FileDescriptor!,
    @MediaMuxerCompat.OutputFormat outputFormat: Int
)

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 outputFormat: Int

The OutputFormat.

Throws
java.io.IOException

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

MediaMuxerCompat

MediaMuxerCompat(
    filePath: String!,
    @MediaMuxerCompat.OutputFormat outputFormat: Int
)

Creates an instance.

Parameters
filePath: String!

The path of the output media file.

@MediaMuxerCompat.OutputFormat outputFormat: Int

The OutputFormat.

Throws
java.io.IOException

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

Public functions

addTrack

fun addTrack(format: MediaFormat!): Int

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
format: MediaFormat!

The MediaFormat of the track.

Returns
Int

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

See also
addTrack

release

fun release(): Unit

Releases the underlying resources.

It should be called after stop.

See also
release

setLocation

fun setLocation(
    latitude: @FloatRange(from = "-90.0", to = 90.0) Float,
    longitude: @FloatRange(from = "-180.0", to = 180.0) Float
): Unit

Sets the location of the media file.

It must be called before start.

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

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

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

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

See also
setLocation

setOrientationHint

fun setOrientationHint(degrees: Int): Unit

Sets the orientation hint for the media file.

It must be called before start.

Parameters
degrees: Int

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

start

fun start(): Unit

Starts the muxer.

This must be called after addTrack and before writeSampleData.

See also
start

stop

fun stop(): Unit

Stops the muxer.

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

See also
stop

writeSampleData

fun writeSampleData(
    trackIndex: Int,
    byteBuffer: ByteBuffer!,
    bufferInfo: MediaCodec.BufferInfo!
): Unit

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
trackIndex: Int

The track index, previously returned by addTrack.

byteBuffer: ByteBuffer!

A buffer containing the sample data to write.

bufferInfo: MediaCodec.BufferInfo!

The MediaCodec.BufferInfo of the sample.

See also
writeSampleData