Search Docs
Loading...
Skip to content

Force Trim

Force trim lets you enforce minimum and maximum video durations in the timeline UI. The editor clamps export to the maximum duration and shows labels to communicate the limits.

2 mins
estimated time
GitHub

Configure duration constraints#

We apply constraints in EngineConfiguration.onLoaded after the scene has loaded. Keep minimumVideoDuration and maximumVideoDuration in seconds and ensure the max is not smaller than the min.

val event = EditorEvent.ApplyVideoDurationConstraints(
minDuration = 1.seconds,
maxDuration = 5.seconds,
)
editorContext.eventHandler.send(event)

Launch the video editor#

Use the default video scene and the standard video UI. You can call the setter again later to switch presets at runtime.

Editor(
license = license, // pass null or empty for evaluation mode with watermark
configuration = {
EditorConfiguration.remember(::VideoConfigurationBuilder) {
onLoaded = {
val event = EditorEvent.ApplyVideoDurationConstraints(
minDuration = 1.seconds,
maxDuration = 5.seconds,
)
editorContext.eventHandler.send(event)
onLoaded()
}
}
},
onClose = onClose,
)

Force Trim constraints in the timeline

Timeline and export behavior#

When the scene duration is below the minimum, the min label stays visible and the editor blocks export with a dialog. When the duration exceeds the maximum, the playhead sticks to the max position and export is clamped to that duration.

Full Code#

This full sample uses VideoConfigurationBuilder from the Video Editor starter kit, applies duration constraints by dispatching EditorEvent.ApplyVideoDurationConstraints from onLoaded, and then calls onLoaded() from the video preset callback to keep the default video setup behavior.

import androidx.compose.runtime.Composable
import ly.img.editor.Editor
import ly.img.editor.configuration.video.VideoConfigurationBuilder
import ly.img.editor.configuration.video.callback.onLoaded
import ly.img.editor.core.configuration.EditorConfiguration
import ly.img.editor.core.configuration.remember
import ly.img.editor.core.event.EditorEvent
import kotlin.time.Duration.Companion.seconds
// Add this composable to your NavHost
@Composable
fun ForceTrimVideoSolution(
license: String,
onClose: (Throwable?) -> Unit,
) {
Editor(
license = license, // pass null or empty for evaluation mode with watermark
configuration = {
EditorConfiguration.remember(::VideoConfigurationBuilder) {
onLoaded = {
val event = EditorEvent.ApplyVideoDurationConstraints(
minDuration = 1.seconds,
maxDuration = 5.seconds,
)
editorContext.eventHandler.send(event)
onLoaded()
}
}
},
onClose = onClose,
)
}