# FragmentedMp4Muxer

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

*** ** * ** ***


```
@UnstableApi
public final class FragmentedMp4Muxer implements Muxer
```

<br />

*** ** * ** ***

A muxer for creating a fragmented MP4 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 a fragmented MP4 file, the caller must:

- Add tracks using `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#addTrack(androidx.media3.common.Format)` which will return a track id.
- Use the associated track id when `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)` for that track.
- `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#close()` the muxer when all data has been written.

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 `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)` in an order that interleaves samples from different tracks.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer.Builder` A builder for `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer` instances. |

| ### Constants |
|---|---|
| `static final long` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#DEFAULT_FRAGMENT_DURATION_MS() = 2000` The default fragment duration. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#SUPPORTED_AUDIO_SAMPLE_MIME_TYPES()` A list of supported audio `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#SUPPORTED_VIDEO_SAMPLE_MIME_TYPES()` A list of supported video `https://developer.android.com/reference/androidx/media3/common/MimeTypes`. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#addMetadataEntry(androidx.media3.common.Metadata.Entry)(https://developer.android.com/reference/androidx/media3/common/Metadata.Entry metadataEntry)` Adds metadata about the output file. |
| `int` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#addTrack(androidx.media3.common.Format)(https://developer.android.com/reference/androidx/media3/common/Format format)` Adds a track of the given media format. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#close()()` Closes the file. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)( int trackId, https://developer.android.com/reference/java/nio/ByteBuffer.html byteBuffer, https://developer.android.com/reference/androidx/media3/muxer/BufferInfo bufferInfo )` Writes encoded sample data. |

### Inherited methods

From [java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable.html) `abstract void` `https://developer.android.com/reference/java/lang/AutoCloseable.html#close()()`

## Constants

### DEFAULT_FRAGMENT_DURATION_MS

```
public static final long DEFAULT_FRAGMENT_DURATION_MS = 2000
```

The default fragment duration.

### SUPPORTED_AUDIO_SAMPLE_MIME_TYPES

```
public static final ImmutableList<String> SUPPORTED_AUDIO_SAMPLE_MIME_TYPES
```

A list of supported audio `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

### SUPPORTED_VIDEO_SAMPLE_MIME_TYPES

```
public static final ImmutableList<String> SUPPORTED_VIDEO_SAMPLE_MIME_TYPES
```

A list of supported video `https://developer.android.com/reference/androidx/media3/common/MimeTypes`.

## Public methods

### addMetadataEntry

```
public void addMetadataEntry(Metadata.Entry metadataEntry)
```

Adds metadata about the output file.

List of supported `https://developer.android.com/reference/androidx/media3/common/Metadata.Entry`:

- `https://developer.android.com/reference/androidx/media3/container/Mp4OrientationData`
- `https://developer.android.com/reference/androidx/media3/container/Mp4LocationData`
- `https://developer.android.com/reference/androidx/media3/container/Mp4TimestampData`
- `https://developer.android.com/reference/androidx/media3/container/MdtaMetadataEntry`: Only `https://developer.android.com/reference/androidx/media3/container/MdtaMetadataEntry#TYPE_INDICATOR_STRING()` or `https://developer.android.com/reference/androidx/media3/container/MdtaMetadataEntry#TYPE_INDICATOR_FLOAT32()` value is supported.
- `https://developer.android.com/reference/androidx/media3/container/XmpData`

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Metadata.Entry metadataEntry` | The `https://developer.android.com/reference/androidx/media3/common/Metadata.Entry`. An `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` is thrown if the `https://developer.android.com/reference/androidx/media3/common/Metadata.Entry` is not supported. |

### addTrack

```
public int addTrack(Format format)
```

Adds a track of the given media format.

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

A metadata track (`https://developer.android.com/reference/androidx/media3/common/C#TRACK_TYPE_METADATA()`) or a track with an unknown type (`https://developer.android.com/reference/androidx/media3/common/C#TRACK_TYPE_UNKNOWN()`) is written as a text metadata track, unless the sample MIME type is `https://developer.android.com/reference/androidx/media3/common/MimeTypes#APPLICATION_ITUT_T35()`, in which case it is written as a T35 metadata track.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format format` | The `https://developer.android.com/reference/androidx/media3/common/Format` for the track. |

| Returns |
|---|---|
| `int` | A unique track id. The track id is non-negative. It should be used in `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/muxer/MuxerException` | If an error occurs while adding track. |

### close

```
public void close()
```

Closes the file.

The muxer cannot be used anymore once this method returns.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/muxer/MuxerException` | If the muxer fails to finish writing the output. |

### writeSampleData

```
public void writeSampleData(
    int trackId,
    ByteBuffer byteBuffer,
    BufferInfo bufferInfo
)
```

Writes encoded sample data.

Samples are written to the disk in batches. If `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer.Builder#setSampleCopyingEnabled(boolean)` is disabled, the `byteBuffer` and the `bufferInfo` must not be modified after calling this method. Otherwise, they are copied and it is safe to modify them after this method returns.

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

| Parameters |
|---|---|
| `int trackId` | The track id for which this sample is being written. |
| `https://developer.android.com/reference/java/nio/ByteBuffer.html byteBuffer` | The encoded sample. The muxer takes ownership of the buffer if `https://developer.android.com/reference/androidx/media3/muxer/FragmentedMp4Muxer.Builder#setSampleCopyingEnabled(boolean)` is disabled. Otherwise, the position of the buffer is updated but the caller retains ownership. |
| `https://developer.android.com/reference/androidx/media3/muxer/BufferInfo bufferInfo` | The `https://developer.android.com/reference/androidx/media3/muxer/BufferInfo` related to this sample. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/muxer/MuxerException` | If there is any error while writing data to the disk. |