Transformer.Builder


public final class Transformer.Builder


A builder for Transformer instances.

Summary

Public constructors

Builder(Context context)

Creates a builder with default values.

Public methods

Transformer.Builder

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

Transformer

Builds a Transformer instance.

Transformer.Builder

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

Transformer.Builder

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.

Transformer.Builder

Removes all listeners.

Transformer.Builder

Removes a Transformer.Listener.

Transformer.Builder

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

Transformer.Builder

Sets the audio MIME type of the output.

Transformer.Builder

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

Transformer.Builder

This method is deprecated.

Set the audio processors in an setEffects, and pass it to start instead.

Transformer.Builder

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

Transformer.Builder

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

Transformer.Builder

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

Transformer.Builder
@CanIgnoreReturnValue
setFlattenForSlowMotion(boolean flattenForSlowMotion)

This method is deprecated.

Use setFlattenForSlowMotion to flatten the EditedMediaItem passed to start instead.

Transformer.Builder

This method is deprecated.

Use addListener, removeListener or removeAllListeners instead.

Transformer.Builder

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

Transformer.Builder
@CanIgnoreReturnValue
setMaxDelayBetweenMuxerSamplesMs(long maxDelayBetweenMuxerSamplesMs)

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

Transformer.Builder

Sets the Muxer.Factory for muxers that write the media container.

Transformer.Builder

Sets whether to encode portrait videos in portrait orientation.

Transformer.Builder
@CanIgnoreReturnValue
setRemoveAudio(boolean removeAudio)

This method is deprecated.

Use setRemoveAudio to remove the audio from the EditedMediaItem passed to start instead.

Transformer.Builder
@CanIgnoreReturnValue
setRemoveVideo(boolean removeVideo)

This method is deprecated.

Use setRemoveVideo to remove the video from the EditedMediaItem passed to start instead.

Transformer.Builder

This method is deprecated.

Use setAudioMimeType, setVideoMimeType and setHdrMode instead.

Transformer.Builder

This method is deprecated.

Set the video effects in an setEffects, and pass it to start instead.

Transformer.Builder

Sets the VideoFrameProcessor.Factory to be used to create VideoFrameProcessor instances.

Transformer.Builder

Sets the video MIME type of the output.

Public constructors

Builder

public Builder(Context context)

Creates a builder with default values.

Parameters
Context context

The Context.

Public methods

addListener

@CanIgnoreReturnValue
public Transformer.Builder addListener(Transformer.Listener listener)

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

This is equivalent to addListener.

Parameters
Transformer.Listener listener

A Transformer.Listener.

Returns
Transformer.Builder

This builder.

build

public Transformer build()

Builds a Transformer instance.

Throws
java.lang.IllegalStateException

If both audio and video have been removed (otherwise the output would not contain any samples).

java.lang.IllegalStateException

If the muxer doesn't support the requested audio/video MIME type.

experimentalSetMaxFramesInEncoder

@CanIgnoreReturnValue
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 INDEX_UNSET, which means no limit is enforced.

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

Parameters
int maxFramesInEncoder

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

Returns
Transformer.Builder

This builder.

Throws
java.lang.IllegalArgumentException

If maxFramesInEncoder is not equal to INDEX_UNSET and is non-positive.

experimentalSetTrimOptimizationEnabled

@CanIgnoreReturnValue
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 optimizationResult will indicate whether the optimization was applied.

Parameters
boolean enabled

Whether to enable trim optimization.

Returns
Transformer.Builder

This builder.

removeAllListeners

@CanIgnoreReturnValue
public Transformer.Builder removeAllListeners()

Removes all listeners.

This is equivalent to removeAllListeners.

Returns
Transformer.Builder

This builder.

removeListener

@CanIgnoreReturnValue
public Transformer.Builder removeListener(Transformer.Listener listener)

Removes a Transformer.Listener.

This is equivalent to removeListener.

Parameters
Transformer.Listener listener

A Transformer.Listener.

Returns
Transformer.Builder

This builder.

setAssetLoaderFactory

@CanIgnoreReturnValue
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 and a DefaultDecoderFactory.

Parameters
AssetLoader.Factory assetLoaderFactory

An AssetLoader.Factory.

Returns
Transformer.Builder

This builder.

setAudioMimeType

@CanIgnoreReturnValue
public Transformer.Builder setAudioMimeType(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 onFallbackApplied will be invoked with the fallback value.
Parameters
String audioMimeType

The MIME type of the audio samples in the output.

Returns
Transformer.Builder

This builder.

Throws
java.lang.IllegalArgumentException

If the audio MIME type passed is not an audio MIME type.

setAudioMixerFactory

@CanIgnoreReturnValue
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
AudioMixer.Factory audioMixerFactory

A AudioMixer.Factory.

Returns
Transformer.Builder

This builder.

setAudioProcessors

@CanIgnoreReturnValue
public Transformer.Builder setAudioProcessors(List<AudioProcessor> audioProcessors)

setDebugViewProvider

@CanIgnoreReturnValue
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 NONE, which doesn't show any debug info.

Not all exports will result in debug views being populated.

Parameters
DebugViewProvider debugViewProvider

Provider for debug views.

Returns
Transformer.Builder

This builder.

setEncoderFactory

@CanIgnoreReturnValue
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
Codec.EncoderFactory encoderFactory

The Codec.EncoderFactory instance.

Returns
Transformer.Builder

This builder.

setEnsureFileStartsOnVideoFrameEnabled

@CanIgnoreReturnValue
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
boolean enabled

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

Returns
Transformer.Builder

This builder.

setFlattenForSlowMotion

@CanIgnoreReturnValue
public Transformer.Builder setFlattenForSlowMotion(boolean flattenForSlowMotion)

setListener

@CanIgnoreReturnValue
public Transformer.Builder setListener(Transformer.Listener listener)

setLooper

@CanIgnoreReturnValue
public Transformer.Builder setLooper(Looper looper)

Sets the Looper 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 looper

A Looper.

Returns
Transformer.Builder

This builder.

setMaxDelayBetweenMuxerSamplesMs

@CanIgnoreReturnValue
public Transformer.Builder setMaxDelayBetweenMuxerSamplesMs(long maxDelayBetweenMuxerSamplesMs)

Sets the maximum delay allowed between output samples regardless of the track type, or TIME_UNSET if there is no maximum. The default value is 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
long maxDelayBetweenMuxerSamplesMs

The maximum delay allowed (in microseconds).

Returns
Transformer.Builder

This builder.

setMuxerFactory

@CanIgnoreReturnValue
public Transformer.Builder setMuxerFactory(Muxer.Factory muxerFactory)

Sets the Muxer.Factory for muxers that write the media container.

The default value is a DefaultMuxer.Factory.

Parameters
Muxer.Factory muxerFactory

A Muxer.Factory.

Returns
Transformer.Builder

This builder.

setPortraitEncodingEnabled

@CanIgnoreReturnValue
public Transformer.Builder setPortraitEncodingEnabled(boolean enabled)

Sets whether to encode portrait videos in portrait orientation.

The default value is false. In this case, portrait videos will be rotated by 90 degrees before being encoded, and metadata will be added to the output file to indicate that the video should be rotated back when played. This is done because encoding landscape videos is more largely supported. Enabling portrait encoding should therefore be done carefully because it is likely to result in more failures.

Parameters
boolean enabled

Whether to encode portrait videos in portrait orientation.

Returns
Transformer.Builder

This builder.

setRemoveAudio

@CanIgnoreReturnValue
public Transformer.Builder setRemoveAudio(boolean removeAudio)

setRemoveVideo

@CanIgnoreReturnValue
public Transformer.Builder setRemoveVideo(boolean removeVideo)

setTransformationRequest

@CanIgnoreReturnValue
public Transformer.Builder setTransformationRequest(TransformationRequest transformationRequest)

setVideoEffects

@CanIgnoreReturnValue
public Transformer.Builder setVideoEffects(List<Effect> effects)

setVideoFrameProcessorFactory

@CanIgnoreReturnValue
public Transformer.Builder setVideoFrameProcessorFactory(
    VideoFrameProcessor.Factory videoFrameProcessorFactory
)

Sets the VideoFrameProcessor.Factory to be used to create VideoFrameProcessor instances.

The default value is a DefaultVideoFrameProcessor.Factory built with default values.

If passing in a DefaultVideoFrameProcessor.Factory, the caller must not set the texture output.

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

Parameters
VideoFrameProcessor.Factory videoFrameProcessorFactory

A VideoFrameProcessor.Factory.

Returns
Transformer.Builder

This builder.

setVideoMimeType

@CanIgnoreReturnValue
public Transformer.Builder setVideoMimeType(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 onFallbackApplied will be invoked with the fallback value.
Parameters
String videoMimeType

The MIME type of the video samples in the output.

Returns
Transformer.Builder

This builder.

Throws
java.lang.IllegalArgumentException

If the video MIME type passed is not a video MIME type.