# 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)

*** ** * ** ***


```
public final class Mp4Muxer.Builder
```

<br />

*** ** * ** ***

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

## Summary

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

| ### Public methods |
|---|---|
| `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer` | `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#build()()` Builds an `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer` instance. |
| `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/androidx/media3/common/util/ExperimentalApi https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#experimentalSetFreeSpaceAfterFileTypeBox(int)(int bytes)` 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/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setAnnexBToAvccConverter(androidx.media3.muxer.AnnexBToAvccConverter)(https://developer.android.com/reference/androidx/media3/muxer/AnnexBToAvccConverter annexBToAvccConverter)` Sets the `https://developer.android.com/reference/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/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setAttemptStreamableOutputEnabled(boolean)( boolean attemptStreamableOutputEnabled )` 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/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setCacheFileSupplier(com.google.common.base.Supplier<java.lang.String>)(https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/String.html> cacheFileSupplier)` 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/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setLastSampleDurationBehavior(int)( @https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.LastSampleDurationBehavior int lastSampleDurationBehavior )` Sets the `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.LastSampleDurationBehavior`. |
| `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setMp4AtFileParameters(androidx.media3.muxer.Mp4Muxer.Mp4AtFileParameters)( https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters mp4AtFileParameters )` Sets the `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Mp4AtFileParameters`. |
| `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setOutputFileFormat(int)(@https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.FileFormat int fileFormat)` Sets the specific MP4 file format. |
| `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setSampleBatchingEnabled(boolean)(boolean enabled)` Sets whether to enable sample batching. |
| `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer.Builder#setSampleCopyingEnabled(boolean)(boolean enabled)` Sets whether to enable the sample copy. |

## Public constructors

### Builder

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

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

### Builder

```
public Builder(SeekableMuxerOutput seekableMuxerOutput)
```

Creates an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/muxer/SeekableMuxerOutput seekableMuxerOutput` | A `https://developer.android.com/reference/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/androidx/media3/muxer/Muxer#close()` is called. |

## Public methods

### build

```
public Mp4Muxer build()
```

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

### experimentalSetFreeSpaceAfterFileTypeBox

```
@CanIgnoreReturnValue
@ExperimentalApi
public Mp4Muxer.Builder experimentalSetFreeSpaceAfterFileTypeBox(int bytes)
```

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/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
public Mp4Muxer.Builder setAnnexBToAvccConverter(AnnexBToAvccConverter annexBToAvccConverter)
```

Sets the `https://developer.android.com/reference/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/androidx/media3/muxer/AnnexBToAvccConverter#DEFAULT()`.

### setAttemptStreamableOutputEnabled

```
@CanIgnoreReturnValue
public Mp4Muxer.Builder setAttemptStreamableOutputEnabled(
    boolean attemptStreamableOutputEnabled
)
```

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
public Mp4Muxer.Builder setCacheFileSupplier(Supplier<String> cacheFileSupplier)
```

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/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/androidx/media3/muxer/Muxer#close()` the muxer.

### setLastSampleDurationBehavior

```
@CanIgnoreReturnValue
public Mp4Muxer.Builder setLastSampleDurationBehavior(
    @Mp4Muxer.LastSampleDurationBehavior int lastSampleDurationBehavior
)
```

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

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

### setMp4AtFileParameters

```
@CanIgnoreReturnValue
public Mp4Muxer.Builder setMp4AtFileParameters(
    Mp4Muxer.Mp4AtFileParameters mp4AtFileParameters
)
```

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

### setOutputFileFormat

```
@CanIgnoreReturnValue
public Mp4Muxer.Builder setOutputFileFormat(@Mp4Muxer.FileFormat int fileFormat)
```

Sets the specific MP4 file format.

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

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

### setSampleBatchingEnabled

```
@CanIgnoreReturnValue
public Mp4Muxer.Builder setSampleBatchingEnabled(boolean enabled)
```

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/androidx/media3/muxer/Mp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)`.

When sample batching is enabled, and `https://developer.android.com/reference/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/androidx/media3/muxer/BufferInfo` provided to `https://developer.android.com/reference/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/androidx/media3/muxer/BufferInfo` can be modified after calling `https://developer.android.com/reference/androidx/media3/muxer/Mp4Muxer#writeSampleData(int,java.nio.ByteBuffer,androidx.media3.muxer.BufferInfo)`.

The default value is `false`.

### setSampleCopyingEnabled

```
@CanIgnoreReturnValue
public Mp4Muxer.Builder setSampleCopyingEnabled(boolean enabled)
```

Sets whether to enable the sample copy.

If the sample copy is enabled, `https://developer.android.com/reference/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/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/androidx/media3/muxer/BufferInfo` and the caller must not modify them.

Note: Sample copying is only effective when `https://developer.android.com/reference/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`.