public final class

Transformer.Builder

extends java.lang.Object

 java.lang.Object

↳androidx.media3.transformer.Transformer.Builder

Overview

A builder for Transformer instances.

Summary

Constructors
publicBuilder(Context context)

Creates a builder with default values.

Methods
public Transformer.BuilderaddListener(Transformer.Listener listener)

Adds a Transformer.Listener to listen to the export events.

public Transformerbuild()

Builds a Transformer instance.

public Transformer.BuilderexperimentalSetMaxFramesInEncoder(int maxFramesInEncoder)

Limits how many video frames can be processed at any time by the encoder.

public Transformer.BuilderexperimentalSetTrimOptimizationEnabled(boolean enabled)

Sets whether to attempt to optimize trims from the start of the EditedMediaItem by transcoding as little of the file as possible and transmuxing the rest.

public Transformer.BuilderremoveAllListeners()

Removes all listeners.

public Transformer.BuilderremoveListener(Transformer.Listener listener)

Removes a Transformer.Listener.

public Transformer.BuildersetAssetLoaderFactory(AssetLoader.Factory assetLoaderFactory)

Sets the AssetLoader.Factory to be used to retrieve the samples to export.

public Transformer.BuildersetAudioMimeType(java.lang.String audioMimeType)

Sets the audio MIME type of the output.

public Transformer.BuildersetAudioMixerFactory(AudioMixer.Factory audioMixerFactory)

Sets the AudioMixer.Factory to be used when audio mixing is needed.

public Transformer.BuildersetAudioProcessors(java.util.List<AudioProcessor> audioProcessors)

public Transformer.BuildersetDebugViewProvider(DebugViewProvider debugViewProvider)

Sets a provider for views to show diagnostic information (if available) during export.

public Transformer.BuildersetEncoderFactory(Codec.EncoderFactory encoderFactory)

Sets the Codec.EncoderFactory that will be used by the transformer.

public Transformer.BuildersetEnsureFileStartsOnVideoFrameEnabled(boolean enabled)

Sets whether to ensure that the output file starts on a video frame.

public Transformer.BuildersetFlattenForSlowMotion(boolean flattenForSlowMotion)

public Transformer.BuildersetListener(Transformer.Listener listener)

public Transformer.BuildersetLooper(Looper looper)

Sets the that must be used for all calls to the transformer and that is used to call listeners on.

public Transformer.BuildersetMaxDelayBetweenMuxerSamplesMs(long maxDelayBetweenMuxerSamplesMs)

Sets the maximum delay allowed between output samples regardless of the track type, or C.TIME_UNSET if there is no maximum.

public Transformer.BuildersetMuxerFactory(Muxer.Factory muxerFactory)

Sets the androidx.media3.transformer.Muxer.Factory for muxers that write the media container.

public Transformer.BuildersetRemoveAudio(boolean removeAudio)

public Transformer.BuildersetRemoveVideo(boolean removeVideo)

public Transformer.BuildersetTransformationRequest(TransformationRequest transformationRequest)

public Transformer.BuildersetVideoEffects(java.util.List<Effect> effects)

public Transformer.BuildersetVideoFrameProcessorFactory(VideoFrameProcessor.Factory videoFrameProcessorFactory)

Sets the to be used to create VideoFrameProcessor instances.

public Transformer.BuildersetVideoMimeType(java.lang.String videoMimeType)

Sets the video MIME type of the output.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Builder(Context context)

Creates a builder with default values.

Parameters:

context: The .

Methods

public Transformer.Builder setAudioMimeType(java.lang.String audioMimeType)

Sets the audio MIME type of the output.

If no audio MIME type is passed, the output audio MIME type is the same as the first MediaItem in the Composition.

Supported MIME types are:

If the MIME type is not supported, Transformer will fallback to a supported MIME type and Transformer.Listener.onFallbackApplied(Composition, TransformationRequest, TransformationRequest) will be invoked with the fallback value.

Parameters:

audioMimeType: The MIME type of the audio samples in the output.

Returns:

This builder.

public Transformer.Builder setVideoMimeType(java.lang.String videoMimeType)

Sets the video MIME type of the output.

If no video MIME type is passed, the output video MIME type is the same as the first MediaItem in the Composition.

Supported MIME types are:

If the MIME type is not supported, Transformer will fallback to a supported MIME type and Transformer.Listener.onFallbackApplied(Composition, TransformationRequest, TransformationRequest) will be invoked with the fallback value.

Parameters:

videoMimeType: The MIME type of the video samples in the output.

Returns:

This builder.

public Transformer.Builder setTransformationRequest(TransformationRequest transformationRequest)

Deprecated: Use Transformer.Builder.setAudioMimeType(String), Transformer.Builder.setVideoMimeType(String) and Composition.Builder.setHdrMode(int) instead.

public Transformer.Builder setAudioProcessors(java.util.List<AudioProcessor> audioProcessors)

Deprecated: Set the audio processors in an EditedMediaItem.Builder.setEffects(Effects), and pass it to Transformer.start(EditedMediaItem, String) instead.

public Transformer.Builder setVideoEffects(java.util.List<Effect> effects)

Deprecated: Set the video effects in an EditedMediaItem.Builder.setEffects(Effects), and pass it to Transformer.start(EditedMediaItem, String) instead.

public Transformer.Builder setRemoveAudio(boolean removeAudio)

Deprecated: Use EditedMediaItem.Builder.setRemoveAudio(boolean) to remove the audio from the EditedMediaItem passed to Transformer.start(EditedMediaItem, String) instead.

public Transformer.Builder setRemoveVideo(boolean removeVideo)

Deprecated: Use EditedMediaItem.Builder.setRemoveVideo(boolean) to remove the video from the EditedMediaItem passed to Transformer.start(EditedMediaItem, String) instead.

public Transformer.Builder setFlattenForSlowMotion(boolean flattenForSlowMotion)

Deprecated: Use EditedMediaItem.Builder.setFlattenForSlowMotion(boolean) to flatten the EditedMediaItem passed to Transformer.start(EditedMediaItem, String) instead.

public Transformer.Builder experimentalSetTrimOptimizationEnabled(boolean enabled)

Sets whether to attempt to optimize trims from the start of the EditedMediaItem by transcoding as little of the file as possible and transmuxing the rest.

This optimization has the following limitations:

  • Only supported for single-asset (i.e. only one EditedMediaItem in the whole Composition) exports of mp4 files.
  • Not guaranteed to work with any effects.

This process relies on the given EncoderFactory providing the right encoder level and profiles when transcoding, so that the transcoded and transmuxed segments of the file can be stitched together. If the file segments can't be stitched together, Transformer throw away any progress and proceed with unoptimized export instead.

The ExportResult.optimizationResult will indicate whether the optimization was applied.

Parameters:

enabled: Whether to enable trim optimization.

Returns:

This builder.

public Transformer.Builder experimentalSetMaxFramesInEncoder(int maxFramesInEncoder)

Limits how many video frames can be processed at any time by the encoder.

A video frame starts encoding when it enters the encoder input surface, and finishes encoding when the corresponding encoder output buffer is released.

The default value is C.INDEX_UNSET, which means no limit is enforced.

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

Parameters:

maxFramesInEncoder: The maximum number of frames that the video encoder is allowed to process at a time, or C.INDEX_UNSET if no limit is enforced.

Returns:

This builder.

public Transformer.Builder setEnsureFileStartsOnVideoFrameEnabled(boolean enabled)

Sets whether to ensure that the output file starts on a video frame.

Any audio samples that are earlier than the first video frame will be dropped. This can make the output of trimming operations more compatible with player implementations that don't show the first video frame until its presentation timestamp.

Ignored when trim optimization is set.

Parameters:

enabled: Whether to ensure that the file starts on a video frame.

Returns:

This builder.

public Transformer.Builder setMaxDelayBetweenMuxerSamplesMs(long maxDelayBetweenMuxerSamplesMs)

Sets the maximum delay allowed between output samples regardless of the track type, or C.TIME_UNSET if there is no maximum. The default value is Transformer.DEFAULT_MAX_DELAY_BETWEEN_MUXER_SAMPLES_MS.

The export will be aborted when no sample is written in maxDelayBetweenMuxerSamplesMs. Note that there is no guarantee that the export will be aborted exactly at that time.

Parameters:

maxDelayBetweenMuxerSamplesMs: The maximum delay allowed (in microseconds).

Returns:

This builder.

public Transformer.Builder setListener(Transformer.Listener listener)

Deprecated: Use Transformer.addListener(Transformer.Listener), Transformer.removeListener(Transformer.Listener) or Transformer.Builder.removeAllListeners() instead.

public Transformer.Builder addListener(Transformer.Listener listener)

Adds a Transformer.Listener to listen to the export events.

This is equivalent to Transformer.addListener(Transformer.Listener).

Parameters:

listener: A Transformer.Listener.

Returns:

This builder.

public Transformer.Builder removeListener(Transformer.Listener listener)

Removes a Transformer.Listener.

This is equivalent to Transformer.removeListener(Transformer.Listener).

Parameters:

listener: A Transformer.Listener.

Returns:

This builder.

public Transformer.Builder removeAllListeners()

Removes all listeners.

This is equivalent to Transformer.removeAllListeners().

Returns:

This builder.

public Transformer.Builder setAssetLoaderFactory(AssetLoader.Factory assetLoaderFactory)

Sets the AssetLoader.Factory to be used to retrieve the samples to export.

The default value is a DefaultAssetLoaderFactory built with a DefaultMediaSourceFactory and a DefaultDecoderFactory.

Parameters:

assetLoaderFactory: An AssetLoader.Factory.

Returns:

This builder.

public Transformer.Builder setAudioMixerFactory(AudioMixer.Factory audioMixerFactory)

Sets the AudioMixer.Factory to be used when audio mixing is needed.

The default value is a DefaultAudioMixer.Factory with default values.

Parameters:

audioMixerFactory: A AudioMixer.Factory.

Returns:

This builder.

public Transformer.Builder setVideoFrameProcessorFactory(VideoFrameProcessor.Factory videoFrameProcessorFactory)

Sets the to be used to create VideoFrameProcessor instances.

The default value is a built with default values.

If passing in a , the caller must not .

If exporting a Composition with multiple video sequences, the must be a .

Parameters:

videoFrameProcessorFactory: A .

Returns:

This builder.

public Transformer.Builder setEncoderFactory(Codec.EncoderFactory encoderFactory)

Sets the Codec.EncoderFactory that will be used by the transformer.

The default value is a DefaultEncoderFactory instance.

Parameters:

encoderFactory: The Codec.EncoderFactory instance.

Returns:

This builder.

public Transformer.Builder setMuxerFactory(Muxer.Factory muxerFactory)

Sets the androidx.media3.transformer.Muxer.Factory for muxers that write the media container.

The default value is a DefaultMuxer.Factory.

Parameters:

muxerFactory: A androidx.media3.transformer.Muxer.Factory.

Returns:

This builder.

public Transformer.Builder setLooper(Looper looper)

Sets the that must be used for all calls to the transformer and that is used to call listeners on.

The default value is the Looper of the thread that this builder was created on, or if that thread does not have a Looper, the Looper of the application's main thread.

Parameters:

looper: A .

Returns:

This builder.

public Transformer.Builder setDebugViewProvider(DebugViewProvider debugViewProvider)

Sets a provider for views to show diagnostic information (if available) during export.

This is intended for debugging. The default value is DebugViewProvider.NONE, which doesn't show any debug info.

Not all exports will result in debug views being populated.

Parameters:

debugViewProvider: Provider for debug views.

Returns:

This builder.

public Transformer build()

Builds a Transformer instance.