# Transformer

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

*** ** * ** ***


```
@UnstableApi
public final class Transformer
```

<br />

*** ** * ** ***

A transformer to export media inputs.

The same Transformer instance can be used to export multiple inputs (sequentially, not concurrently).

Transformer instances must be accessed from a single application thread. For the vast majority of cases this should be the application's main thread. The thread on which a Transformer instance must be accessed can be explicitly specified by passing a `https://developer.android.com/reference/android/os/Looper.html` when creating the transformer. If no Looper is specified, then the Looper of the thread that the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder` is created on is used, or if that thread does not have a Looper, the Looper of the application's main thread is used. In all cases the Looper of the thread from which the transformer must be accessed can be queried using `https://developer.android.com/reference/androidx/media3/transformer/Transformer#getApplicationLooper()`.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder` A builder for `https://developer.android.com/reference/androidx/media3/transformer/Transformer` instances. |
| `public interface https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener` A listener for the export events. |
| `@https://developer.android.com/reference/java/lang/annotation/Documented.html @https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#SOURCE) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE_USE) @https://developer.android.com/reference/androidx/annotation/IntDef(value = ) public annotation https://developer.android.com/reference/androidx/media3/transformer/Transformer.ProgressState` Progress state. |

| ### Constants |
|---|---|
| `static final long` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#DEFAULT_MAX_DELAY_BETWEEN_MUXER_SAMPLES_MS()` The default value for the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setMaxDelayBetweenMuxerSamplesMs(long)`. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_AVAILABLE() = 2` Indicates that the progress is available. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_NOT_STARTED() = 0` Indicates that the corresponding operation hasn't been started. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_UNAVAILABLE() = 3` Indicates that the progress is permanently unavailable. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_WAITING_FOR_AVAILABILITY() = 1` Indicates that the progress is currently unavailable, but might become available. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#addListener(androidx.media3.transformer.Transformer.Listener)(https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener listener)` Adds a `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener` to listen to the export events. |
| `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#buildUpon()()` Returns a `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder` initialized with the values of this instance. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#cancel()()` Cancels the export that is currently in progress, if any. |
| `https://developer.android.com/reference/android/os/Looper.html` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#getApplicationLooper()()` Returns the `https://developer.android.com/reference/android/os/Looper.html` associated with the application thread that's used to access the transformer and on which transformer events are received. |
| `int` | `@https://developer.android.com/reference/androidx/media3/transformer/Transformer.ProgressState https://developer.android.com/reference/androidx/media3/transformer/Transformer#getProgress(androidx.media3.transformer.ProgressHolder)(https://developer.android.com/reference/androidx/media3/transformer/ProgressHolder progressHolder)` Returns the current `https://developer.android.com/reference/androidx/media3/transformer/Transformer.ProgressState` and updates `progressHolder` with the current progress if it is `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_AVAILABLE()`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#removeAllListeners()()` Removes all `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#removeListener(androidx.media3.transformer.Transformer.Listener)(https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener listener)` Removes a `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#resume(androidx.media3.transformer.Composition,java.lang.String,java.lang.String)( https://developer.android.com/reference/androidx/media3/transformer/Composition composition, https://developer.android.com/reference/java/lang/String.html outputFilePath, https://developer.android.com/reference/java/lang/String.html oldFilePath )` Resumes a previously `https://developer.android.com/reference/androidx/media3/transformer/Transformer#cancel()` export. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#start(androidx.media3.transformer.Composition,androidx.media3.transformer.TransformerOutput)(https://developer.android.com/reference/androidx/media3/transformer/Composition composition, https://developer.android.com/reference/androidx/media3/transformer/TransformerOutput output)` Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/transformer/Composition` to a `https://developer.android.com/reference/androidx/media3/transformer/TransformerOutput`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#start(androidx.media3.transformer.Composition,java.lang.String)(https://developer.android.com/reference/androidx/media3/transformer/Composition composition, https://developer.android.com/reference/java/lang/String.html path)` Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/transformer/Composition`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#start(androidx.media3.transformer.EditedMediaItem,java.lang.String)(https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem editedMediaItem, https://developer.android.com/reference/java/lang/String.html path)` Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/Transformer#start(androidx.media3.common.MediaItem,java.lang.String)(https://developer.android.com/reference/androidx/media3/common/MediaItem mediaItem, https://developer.android.com/reference/java/lang/String.html path)` Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/common/MediaItem`. |

## Constants

### DEFAULT_MAX_DELAY_BETWEEN_MUXER_SAMPLES_MS

```
public static final long DEFAULT_MAX_DELAY_BETWEEN_MUXER_SAMPLES_MS
```

The default value for the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setMaxDelayBetweenMuxerSamplesMs(long)`.

### PROGRESS_STATE_AVAILABLE

```
public static final int PROGRESS_STATE_AVAILABLE = 2
```

Indicates that the progress is available.

### PROGRESS_STATE_NOT_STARTED

```
public static final int PROGRESS_STATE_NOT_STARTED = 0
```

Indicates that the corresponding operation hasn't been started.

### PROGRESS_STATE_UNAVAILABLE

```
public static final int PROGRESS_STATE_UNAVAILABLE = 3
```

Indicates that the progress is permanently unavailable.

### PROGRESS_STATE_WAITING_FOR_AVAILABILITY

```
public static final int PROGRESS_STATE_WAITING_FOR_AVAILABILITY = 1
```

Indicates that the progress is currently unavailable, but might become available.

## Public methods

### addListener

```
public void addListener(Transformer.Listener listener)
```

Adds a `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener` to listen to the export events.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener listener` | A `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |

### buildUpon

```
public Transformer.Builder buildUpon()
```

Returns a `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder` initialized with the values of this instance.

### cancel

```
public void cancel()
```

Cancels the export that is currently in progress, if any.

The export output file (if any) is not deleted.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |

### getApplicationLooper

```
public Looper getApplicationLooper()
```

Returns the `https://developer.android.com/reference/android/os/Looper.html` associated with the application thread that's used to access the transformer and on which transformer events are received.

### getProgress

```
@Transformer.ProgressState
public int getProgress(ProgressHolder progressHolder)
```

Returns the current `https://developer.android.com/reference/androidx/media3/transformer/Transformer.ProgressState` and updates `progressHolder` with the current progress if it is `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_AVAILABLE()`.

If the export is `https://developer.android.com/reference/androidx/media3/transformer/Transformer#resume(androidx.media3.transformer.Composition,java.lang.String,java.lang.String)`, this method returns `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_UNAVAILABLE()`.

After an export `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener#onCompleted(androidx.media3.transformer.Composition,androidx.media3.transformer.ExportResult)`, this method returns `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_NOT_STARTED()`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/ProgressHolder progressHolder` | A `https://developer.android.com/reference/androidx/media3/transformer/ProgressHolder`, updated to hold the percentage progress if `https://developer.android.com/reference/androidx/media3/transformer/Transformer#PROGRESS_STATE_AVAILABLE()`. |

| Returns |
|---|---|
| `int` | The `https://developer.android.com/reference/androidx/media3/transformer/Transformer.ProgressState`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |

### removeAllListeners

```
public void removeAllListeners()
```

Removes all `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |

### removeListener

```
public void removeListener(Transformer.Listener listener)
```

Removes a `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener listener` | A `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Listener`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |

### resume

```
public void resume(
    Composition composition,
    String outputFilePath,
    String oldFilePath
)
```

Resumes a previously `https://developer.android.com/reference/androidx/media3/transformer/Transformer#cancel()` export.

An export can be resumed only when:

- The `https://developer.android.com/reference/androidx/media3/transformer/Composition` contains a single `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItemSequence` having continuous audio and video tracks.
- The output is an MP4 file.
- The `https://developer.android.com/reference/androidx/media3/transformer/Composition` and `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem` instances do not contain speed changing effects.

Note that export optimizations (such as `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#experimentalSetTrimOptimizationEnabled(boolean)`) will not be applied upon resumption.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/Composition composition` | The `https://developer.android.com/reference/androidx/media3/transformer/Composition` to resume export. |
| `https://developer.android.com/reference/java/lang/String.html outputFilePath` | The path to the output file. This must be different from the output path of the cancelled export. |
| `https://developer.android.com/reference/java/lang/String.html oldFilePath` | The output path of the the cancelled export. |

### start

```
public void start(Composition composition, TransformerOutput output)
```

Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/transformer/Composition` to a `https://developer.android.com/reference/androidx/media3/transformer/TransformerOutput`.

The export state is notified through the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#addListener(androidx.media3.transformer.Transformer.Listener)`.

Concurrent exports on the same Transformer object are not allowed.

If no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setMuxerFactory(androidx.media3.muxer.Muxer.Factory)` is specified, the output is an MP4 file.

The output can contain at most one video track and one audio track. Other track types are ignored. For adaptive bitrate inputs, if no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setAssetLoaderFactory(androidx.media3.transformer.AssetLoader.Factory)` is specified, the highest bitrate video and audio streams are selected.

If exporting the video track entails transcoding, the output frames' dimensions will be swapped if the output video's height is larger than the width. This is to improve compatibility among different device encoders.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/Composition composition` | The `https://developer.android.com/reference/androidx/media3/transformer/Composition` to export. |
| `https://developer.android.com/reference/androidx/media3/transformer/TransformerOutput output` | The `https://developer.android.com/reference/androidx/media3/transformer/TransformerOutput` destination. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If an export is already in progress. |

### start

```
public void start(Composition composition, String path)
```

Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/transformer/Composition`.

The first `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem` in the first `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItemSequence` that has a given `https://developer.android.com/reference/androidx/media3/common/C.TrackType` will determine the output format for that track, unless the format is set when `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#build()` the `Transformer`. For example, consider the following composition

```
  Composition {
    EditedMediaItemSequence {
      [ImageMediaItem, VideoMediaItem]
    },
    EditedMediaItemSequence {
      [AudioMediaItem]
    },
  }
```
The video format will be determined by the `ImageMediaItem` in the first `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItemSequence`, while the audio format will be determined by the `
AudioMediaItem` in the second `EditedMediaItemSequence`.

Some `https://developer.android.com/reference/androidx/media3/transformer/Composition` are not supported yet. More specifically, `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItemSequence` within the `https://developer.android.com/reference/androidx/media3/transformer/Composition` must meet the following conditions:

- The tracks present in the output of a sequence are determined by the `trackTypes` parameter of the `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItemSequence.Builder`. Tracks of types not present in `trackTypes` will be dropped. If an `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem` within a sequence does not contain a track of a declared type, silent audio or blank video will be generated.
- If a sequence starts with an HDR `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem`, all the following items in the sequence must be HDR.
- All `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem` containing audio data must output 16 bit PCM audio with the same number of channels. This can be done by adding a `https://developer.android.com/reference/androidx/media3/common/audio/ToInt16PcmAudioProcessor` and/or a `https://developer.android.com/reference/androidx/media3/common/audio/ChannelMixingAudioProcessor` to the `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem#effects()`.

The export state is notified through the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#addListener(androidx.media3.transformer.Transformer.Listener)`.

Concurrent exports on the same Transformer object are not allowed.

If no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setMuxerFactory(androidx.media3.muxer.Muxer.Factory)` is specified, the output is an MP4 file.

The output can contain at most one video track and one audio track. Other track types are ignored. For adaptive bitrate inputs, if no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setAssetLoaderFactory(androidx.media3.transformer.AssetLoader.Factory)` is specified, the highest bitrate video and audio streams are selected.

If exporting the video track entails transcoding, the output frames' dimensions will be swapped if the output video's height is larger than the width. This is to improve compatibility among different device encoders.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/Composition composition` | The `https://developer.android.com/reference/androidx/media3/transformer/Composition` to export. |
| `https://developer.android.com/reference/java/lang/String.html path` | The path to the output file. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If an export is already in progress. |

### start

```
public void start(EditedMediaItem editedMediaItem, String path)
```

Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem`.

The export state is notified through the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#addListener(androidx.media3.transformer.Transformer.Listener)`.

Concurrent exports on the same Transformer object are not allowed.

If no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setMuxerFactory(androidx.media3.muxer.Muxer.Factory)` is specified, the output is an MP4 file.

The output can contain at most one video track and one audio track. Other track types are ignored. For adaptive bitrate inputs, if no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setAssetLoaderFactory(androidx.media3.transformer.AssetLoader.Factory)` is specified, the highest bitrate video and audio streams are selected.

If exporting the video track entails transcoding, the output frames' dimensions will be swapped if the output video's height is larger than the width. This is to improve compatibility among different device encoders.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem editedMediaItem` | The `https://developer.android.com/reference/androidx/media3/transformer/EditedMediaItem` to export. |
| `https://developer.android.com/reference/java/lang/String.html path` | The path to the output file. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If an export is already in progress. |

### start

```
public void start(MediaItem mediaItem, String path)
```

Starts an asynchronous operation to export the given `https://developer.android.com/reference/androidx/media3/common/MediaItem`.

The export state is notified through the `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#addListener(androidx.media3.transformer.Transformer.Listener)`.

Concurrent exports on the same Transformer object are not allowed.

If no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setMuxerFactory(androidx.media3.muxer.Muxer.Factory)` is specified, the output is an MP4 file.

The output can contain at most one video track and one audio track. Other track types are ignored. For adaptive bitrate inputs, if no custom `https://developer.android.com/reference/androidx/media3/transformer/Transformer.Builder#setAssetLoaderFactory(androidx.media3.transformer.AssetLoader.Factory)` is specified, the highest bitrate video and audio streams are selected.

If exporting the video track entails transcoding, the output frames' dimensions will be swapped if the output video's height is larger than the width. This is to improve compatibility among different device encoders.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/MediaItem mediaItem` | The `https://developer.android.com/reference/androidx/media3/common/MediaItem` to export. |
| `https://developer.android.com/reference/java/lang/String.html path` | The path to the output file. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | If the `https://developer.android.com/reference/androidx/media3/common/MediaItem` is not supported. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If this method is called from the wrong thread. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If an export is already in progress. |