> Documentation index: https://developer.android.com/reference/llms.txt

# Mp4Muxer

Artifact: [androidx.media3:media3-muxer](https://developer.android.com/jetpack/androidx/releases/media3.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/media3/muxer/Mp4Muxer.java+class:androidx.media3.muxer.Mp4Muxer)

*** ** * ** ***


```
@UnstableApi
class Mp4Muxer : Muxer
```

<br />

*** ** * ** ***

A muxer for creating an MP4 container file.

Muxer supports muxing of:

- Video Codecs:
  - AV1
  - MPEG-4
  - H.263
  - H.264 (AVC)
  - H.265 (HEVC)
  - VP9
  - APV
  - Dolby Vision (profiles 5, 8 and 9)
- Audio Codecs:
  - AAC
  - AMR-NB (Narrowband AMR)
  - AMR-WB (Wideband AMR)
  - Opus
  - Vorbis
  - Raw Audio
- Metadata

All the operations are performed on the caller thread.

To create an MP4 container file, the caller must:

- Add tracks using `addTrack` which will return a track id.
- Use the associated track id when `writing samples` for that track.
- `close` the muxer when all data has been written.

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 |
|---|
| `class Mp4Muxer.Builder` A builder for `Mp4Muxer` instances. |
| `@Documented @Retention(value = RetentionPolicy.SOURCE) @Target(value = ElementType.TYPE_USE) @IntDef(value = ) annotation Mp4Muxer.FileFormat` The specific MP4 file format. |
| `@Documented @Retention(value = RetentionPolicy.SOURCE) @Target(value = ElementType.TYPE_USE) @IntDef(value = ) annotation Mp4Muxer.LastSampleDurationBehavior` Behavior for the duration of the last sample. |
| `class Mp4Muxer.Mp4AtFileParameters` Parameters for `FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION`. |
| `@Documented @Retention(value = RetentionPolicy.SOURCE) @Target(value = ElementType.TYPE_USE) @IntDef(value = ) annotation Mp4Muxer.TrackReferenceType` A track reference type. |

| ### Constants |
|---|---|
| `const Int` | `FILE_FORMAT_DEFAULT = 0` The default MP4 format. |
| `const Int` | `FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION = 1` The MP4 With Auxiliary Tracks Extension (MP4-AT) file format. |
| `const Int` | `LAST_SAMPLE_DURATION_BEHAVIOR_SET_FROM_END_OF_STREAM_BUFFER_OR_DUPLICATE_PREVIOUS = 1` Use the `end of stream sample` to set the duration of the last sample. |
| `const Int` | `LAST_SAMPLE_DURATION_BEHAVIOR_SET_TO_ZERO = 0` The duration of the last sample is set to 0. |
| `const ImmutableList<String!>!` | `SUPPORTED_AUDIO_SAMPLE_MIME_TYPES` A list of supported audio `sample MIME types`. |
| `const ImmutableList<String!>!` | `SUPPORTED_VIDEO_SAMPLE_MIME_TYPES` A list of supported video `sample MIME types`. |
| `const Int` | `TRACK_REFERENCE_TYPE_CDSC = 1667527523` Links a descriptive or metadata track to the content which it describes. |

| ### Public functions |
|---|---|
| `Unit` | `addMetadataEntry(metadataEntry: Metadata.Entry!)` Adds metadata about the output file. |
| `Int` | `addTrack(format: Format!)` Adds a track of the given media format. |
| `Int` | `addTrack(sortKey: Int, format: Format!)` Adds a track of the given media format. |
| `Unit` | `addTrackReference( trackId: Int, @Mp4Muxer.TrackReferenceType referenceType: Int, referencedTrackIds: (Mutable)List<Int!>! )` Adds a track reference to establish a relationship between tracks in the MP4 file. |
| `Unit` | `close()` Closes the file. |
| `Unit` | `writeSampleData( trackId: Int, byteBuffer: ByteBuffer!, bufferInfo: BufferInfo! )` Writes encoded sample data. |

### Inherited functions

From [java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable.md.txt) `abstract Unit` `close()`

## Constants

### FILE_FORMAT_DEFAULT

```
const val FILE_FORMAT_DEFAULT = 0: Int
```

The default MP4 format.

### FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION

```
const val FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION = 1: Int
```

The MP4 With Auxiliary Tracks Extension (MP4-AT) file format. In this file format all the tracks with `auxiliaryTrackType` set to `AUXILIARY_TRACK_TYPE_ORIGINAL`, `AUXILIARY_TRACK_TYPE_DEPTH_LINEAR`, `AUXILIARY_TRACK_TYPE_DEPTH_INVERSE`, or `AUXILIARY_TRACK_TYPE_DEPTH_METADATA` are written in the Auxiliary Tracks MP4 (axte box). The rest of the tracks are written as usual.

See the file format at https://developer.android.com/media/platform/mp4-at-file-format.

### LAST_SAMPLE_DURATION_BEHAVIOR_SET_FROM_END_OF_STREAM_BUFFER_OR_DUPLICATE_PREVIOUS

```
const val LAST_SAMPLE_DURATION_BEHAVIOR_SET_FROM_END_OF_STREAM_BUFFER_OR_DUPLICATE_PREVIOUS = 1: Int
```

Use the `end of stream sample` to set the duration of the last sample.

After `writing` all the samples for a track, the app must `write` an empty sample with flag `BUFFER_FLAG_END_OF_STREAM`. The timestamp of this sample should be equal to the desired track duration.

Once a sample with flag `BUFFER_FLAG_END_OF_STREAM` is `written`, no more samples can be written for that track.

If no explicit `BUFFER_FLAG_END_OF_STREAM` sample is passed, then the duration of the last sample will be same as that of the sample before that.

### LAST_SAMPLE_DURATION_BEHAVIOR_SET_TO_ZERO

```
const val LAST_SAMPLE_DURATION_BEHAVIOR_SET_TO_ZERO = 0: Int
```

The duration of the last sample is set to 0.

### SUPPORTED_AUDIO_SAMPLE_MIME_TYPES

```
const val SUPPORTED_AUDIO_SAMPLE_MIME_TYPES: ImmutableList<String!>!
```

A list of supported audio `sample MIME types`.

### SUPPORTED_VIDEO_SAMPLE_MIME_TYPES

```
const val SUPPORTED_VIDEO_SAMPLE_MIME_TYPES: ImmutableList<String!>!
```

A list of supported video `sample MIME types`.

### TRACK_REFERENCE_TYPE_CDSC

```
const val TRACK_REFERENCE_TYPE_CDSC = 1667527523: Int
```

Links a descriptive or metadata track to the content which it describes.

## Public functions

### addMetadataEntry

```
fun addMetadataEntry(metadataEntry: Metadata.Entry!): Unit
```

Adds metadata about the output file.

List of supported `metadata entries`:

- `Mp4OrientationData`
- `Mp4LocationData`
- `Mp4TimestampData`
- `MdtaMetadataEntry`: Only `string type` or `float type` value is supported.
- `XmpData`

| Parameters |
|---|---|
| `metadataEntry: Metadata.Entry!` | The `metadata`. An `IllegalArgumentException` is thrown if the `metadata` is not supported. |

### addTrack

```
fun addTrack(format: Format!): Int
```

Adds a track of the given media format.

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

Tracks can be added at any point before the muxer is closed, even after writing samples to other tracks. Note that a track requiring an additional `ftyp` compatible brand (for example a `VIDEO_DOLBY_VISION` track, which requires the `dby1` brand) that is added after samples have already been written to another track will not retroactively update the `ftyp` box.

The order of tracks remains same in which they are added.

A metadata track (`TRACK_TYPE_METADATA`) or a track with an unknown type (`TRACK_TYPE_UNKNOWN`) is written as a text metadata track, unless the sample MIME type is `APPLICATION_ITUT_T35`, in which case it is written as a T35 metadata track.

For a metadata track, it is recommended to `add track reference`.

| Parameters |
|---|---|
| `format: Format!` | The `Format` for the track. |

| Returns |
|---|---|
| `Int` | A unique track id. The track id is non-negative. It should be used in `writeSampleData`. |

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If the `format` is not supported or an error occurs while adding the track. |

### addTrack

```
fun addTrack(sortKey: Int, format: Format!): Int
```

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. Note that a track requiring an additional `ftyp` compatible brand (for example a `VIDEO_DOLBY_VISION` track, which requires the `dby1` brand) that is added after samples have already been written to another track will not retroactively update the `ftyp` box.

The final order of tracks is determined by the provided sort key. Tracks with a lower sort key will be written before tracks with a higher sort key. Ordering between tracks with the same sort key is not specified.

| Parameters |
|---|---|
| `sortKey: Int` | The key used for sorting the track list. |
| `format: Format!` | The `Format` for the track. |

| Returns |
|---|---|
| `Int` | A unique track id. The track id is non-negative. It should be used in `writeSampleData`. |

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If the `format` is not supported or an error occurs while adding the track. |

### addTrackReference

```
fun addTrackReference(
    trackId: Int,
    @Mp4Muxer.TrackReferenceType referenceType: Int,
    referencedTrackIds: (Mutable)List<Int!>!
): Unit
```

Adds a track reference to establish a relationship between tracks in the MP4 file.

Track references are used to link one track to another. For example, a metadata track might reference a video track to indicate that the metadata describes the content of that video.

| Parameters |
|---|---|
| `trackId: Int` | The track id of the track to which the reference is being added. This must be a valid track id returned by `addTrack`. |
| `@Mp4Muxer.TrackReferenceType referenceType: Int` | The type of the reference, as defined by `TrackReferenceType`. |
| `referencedTrackIds: (Mutable)List<Int!>!` | The track ids of the tracks that are being referenced. These must also be valid track ids returned by `addTrack`. |

### close

```
fun close(): Unit
```

Closes the file.

The muxer cannot be used anymore once this method is called, even if it throws. Implementations must make further calls to this method a no-op and, once it has been called, must throw an `IllegalStateException` from all other methods.

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If the muxer fails to finish writing the output. |

### writeSampleData

```
fun writeSampleData(
    trackId: Int,
    byteBuffer: ByteBuffer!,
    bufferInfo: BufferInfo!
): Unit
```

Writes encoded sample data.

The muxer's handling of sample `ByteBuffer` and `BufferInfo` depends on the `sample batching` and ` sample copying` settings:

- If `sample batching` is disabled: Samples are written immediately upon arrival. The caller can safely modify or reuse these objects immediately after this method returns.
- If `sample batching` is enabled:
  - If `sample copying` is enabled: The muxer makes internal copies of the provided `ByteBuffer` and `BufferInfo`. The caller can safely modify or reuse these objects immediately after this method returns.
  - If `sample copying` is disabled: The muxer takes ownership of the `ByteBuffer` and `BufferInfo`. The caller must not modify these objects after this method returns.

| Parameters |
|---|---|
| `trackId: Int` | The track id for which this sample is being written. |
| `byteBuffer: ByteBuffer!` | The encoded sample. |
| `bufferInfo: BufferInfo!` | The `BufferInfo` related to this sample. |

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If an error occurs while writing data to the output file. |