# Mp4Muxer.Builder

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/Mp4Muxer.java+class:androidx.media3.muxer.Mp4Muxer.Builder)

*** ** * ** ***


```
class Mp4Muxer.Builder
```

<br />

*** ** * ** ***

A builder for `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer` instances.

## Summary

| ### Public constructors |
|---|
| `[Builder](https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#Builder(java.io.FileOutputStream))(outputStream: https://developer.android.com/reference/java/io/FileOutputStream.html!)` **This function is deprecated.** Use `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#Builder(androidx.media3.muxer.SeekableMuxerOutput)` instead. <br /> |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#Builder(androidx.media3.muxer.SeekableMuxerOutput)(seekableMuxerOutput: https://developer.android.com/reference/kotlin/androidx/media3/muxer/SeekableMuxerOutput!)` Creates an instance. |

| ### Public functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer!` | `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#build()()` Builds an `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer` instance. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/kotlin/androidx/media3/common/util/ExperimentalApi https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#experimentalSetFreeSpaceAfterFileTypeBox(int)(bytes: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Sets the amount of free space (in bytes) to be reserved immediately after the `ftyp` box (File Type box) in the MP4 file. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setAnnexBToAvccConverter(androidx.media3.muxer.AnnexBToAvccConverter)(annexBToAvccConverter: https://developer.android.com/reference/kotlin/androidx/media3/muxer/AnnexBToAvccConverter!)` Sets the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/AnnexBToAvccConverter` to be used by the muxer to convert H.264 and H.265 NAL units from the Annex-B format (using start codes to delineate NAL units) to the AVCC format (which uses length prefixes). |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setAttemptStreamableOutputEnabled(boolean)( attemptStreamableOutputEnabled: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Sets whether to attempt to write a file where the metadata is stored at the start, which can make the file more efficient to read sequentially. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setCacheFileSupplier(com.google.common.base.Supplier<java.lang.String>)(cacheFileSupplier: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!)` Sets a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html` that provides an absolute path of a cache file. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setLastSampleDurationBehavior(int)( @https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.LastSampleDurationBehavior lastSampleDurationBehavior: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Sets the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.LastSampleDurationBehavior`. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setMp4AtFileParameters(androidx.media3.muxer.Mp4Muxer.Mp4AtFileParameters)( mp4AtFileParameters: https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters! )` Sets the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters`. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setOutputFileFormat(int)(@https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.FileFormat fileFormat: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Sets the specific MP4 file format. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setSampleBatchingEnabled(boolean)(enabled: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Sets whether to enable sample batching. |
| `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setSampleCopyingEnabled(boolean)(enabled: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Sets whether to enable the sample copy. |

## Public constructors

### Builder

```
[Builder](https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#Builder(java.io.FileOutputStream))(outputStream: FileOutputStream!)
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Use `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#Builder(androidx.media3.muxer.SeekableMuxerOutput)` instead.

### Builder

```
Builder(seekableMuxerOutput: SeekableMuxerOutput!)
```

Creates an instance.

| Parameters |
|---|---|
| `seekableMuxerOutput: https://developer.android.com/reference/kotlin/androidx/media3/muxer/SeekableMuxerOutput!` | A `https://developer.android.com/reference/kotlin/androidx/media3/muxer/SeekableMuxerOutput` to write output to. It will be automatically `https://developer.android.com/reference/java/nio/channels/Channel.html#close()` by the muxer when `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Muxer#close()` is called. |

## Public functions

### build

```
fun build(): Mp4Muxer!
```

Builds an `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer` instance.

### experimentalSetFreeSpaceAfterFileTypeBox

```
@CanIgnoreReturnValue
@ExperimentalApi
fun experimentalSetFreeSpaceAfterFileTypeBox(bytes: Int): Mp4Muxer.Builder!
```

Sets the amount of free space (in bytes) to be reserved immediately after the `ftyp` box (File Type box) in the MP4 file.

A `free` box of the requested size is always written. If `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setAttemptStreamableOutputEnabled(boolean)` is set to `true`, an additional space is reserved for the `moov` box (Movie Box) which is written if it fits.

This method is experimental and will be renamed or removed in a future release.

### setAnnexBToAvccConverter

```
@CanIgnoreReturnValue
fun setAnnexBToAvccConverter(annexBToAvccConverter: AnnexBToAvccConverter!): Mp4Muxer.Builder!
```

Sets the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/AnnexBToAvccConverter` to be used by the muxer to convert H.264 and H.265 NAL units from the Annex-B format (using start codes to delineate NAL units) to the AVCC format (which uses length prefixes).

The default value is `https://developer.android.com/reference/kotlin/androidx/media3/muxer/AnnexBToAvccConverter#DEFAULT()`.

### setAttemptStreamableOutputEnabled

```
@CanIgnoreReturnValue
fun setAttemptStreamableOutputEnabled(
    attemptStreamableOutputEnabled: Boolean
): Mp4Muxer.Builder!
```

Sets whether to attempt to write a file where the metadata is stored at the start, which can make the file more efficient to read sequentially.

Setting to `true` does not guarantee a streamable MP4 output.

The default value is `true`.

### setCacheFileSupplier

```
@CanIgnoreReturnValue
fun setCacheFileSupplier(cacheFileSupplier: Supplier<String!>!): Mp4Muxer.Builder!
```

Sets a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html` that provides an absolute path of a cache file.

Every call to `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html#get()` must return a new cache file path.

This must be set when `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters#shouldInterleaveSamples()` is set to `
false`.

The app is responsible for deleting the cache file after `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Muxer#close()` the muxer.

### setLastSampleDurationBehavior

```
@CanIgnoreReturnValue
fun setLastSampleDurationBehavior(
    @Mp4Muxer.LastSampleDurationBehavior lastSampleDurationBehavior: Int
): Mp4Muxer.Builder!
```

Sets the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.LastSampleDurationBehavior`.

The default value is `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#LAST_SAMPLE_DURATION_BEHAVIOR_SET_FROM_END_OF_STREAM_BUFFER_OR_DUPLICATE_PREVIOUS()`.

### setMp4AtFileParameters

```
@CanIgnoreReturnValue
fun setMp4AtFileParameters(
    mp4AtFileParameters: Mp4Muxer.Mp4AtFileParameters!
): Mp4Muxer.Builder!
```

Sets the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters`.

### setOutputFileFormat

```
@CanIgnoreReturnValue
fun setOutputFileFormat(@Mp4Muxer.FileFormat fileFormat: Int): Mp4Muxer.Builder!
```

Sets the specific MP4 file format.

The default value is `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#FILE_FORMAT_DEFAULT()`.

For `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION()`, `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters` must also be `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setMp4AtFileParameters(androidx.media3.muxer.Mp4Muxer.Mp4AtFileParameters)` set}.

### setSampleBatchingEnabled

```
@CanIgnoreReturnValue
fun setSampleBatchingEnabled(enabled: Boolean): Mp4Muxer.Builder!
```

Sets whether to enable sample batching.

If sample batching is enabled, samples are written in batches for each track, otherwise samples are written as they `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)`.

When sample batching is enabled, and `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setSampleCopyingEnabled(boolean)` is disabled the `https://developer.android.com/reference/java/nio/ByteBuffer.html` and `https://developer.android.com/reference/kotlin/androidx/media3/muxer/BufferInfo` provided to `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)` must not be modified. Otherwise, if sample batching is disabled or sample copying is enabled, the `https://developer.android.com/reference/java/nio/ByteBuffer.html` and `https://developer.android.com/reference/kotlin/androidx/media3/muxer/BufferInfo` can be modified after calling `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)`.

The default value is `false`.

### setSampleCopyingEnabled

```
@CanIgnoreReturnValue
fun setSampleCopyingEnabled(enabled: Boolean): Mp4Muxer.Builder!
```

Sets whether to enable the sample copy.

If the sample copy is enabled, `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)` copies the input `https://developer.android.com/reference/java/nio/ByteBuffer.html` and `https://developer.android.com/reference/kotlin/androidx/media3/muxer/BufferInfo` before it returns, so it is safe to reuse them immediately. Otherwise, the muxer takes ownership of the `https://developer.android.com/reference/java/nio/ByteBuffer.html` and the `https://developer.android.com/reference/kotlin/androidx/media3/muxer/BufferInfo` and the caller must not modify them.

Note: Sample copying is only effective when `https://developer.android.com/reference/kotlin/androidx/media3/muxer/Mp4Muxer.Builder#setSampleBatchingEnabled(boolean)` is also enabled. If sample batching is disabled, samples are written immediately upon arrival, and copying is not performed, regardless of this setting.

The default value is `false`.