Skip to content

Create From Video

In this example, we will show you how to initialize the CreativeEditor SDK with an initial video.

Starting from an existing video allows you to use the editor for customizing individual assets. This is done by using suspend fun createFromVideo(videoUri: URI): DesignBlock and passing a URI as argument.

Create an instance of Uri using the remote url. Use the object videoRemoteUri as a source for the initial video.

val videoRemoteUri = Uri.parse("https://img.ly/static/ubq_video_samples/bbb.mp4")
val scene = engine.scene.createFromVideo(videoRemoteUri)

We can retrieve the graphic block id of this initial video using fun findByType(blockType: DesignBlockType): List<DesignBlock>. Note that that function returns an array. Since there’s only a single graphic block in the scene, the block is at index 0.

// Find the automatically added graphic block in the scene that contains the video fill.
val block = engine.block.findByType(DesignBlockType.Graphic).first()

We can then manipulate and modify this block. Here we modify its opacity with fun setOpacity(block: DesignBlock, @FloatRange(from = 0.0, to = 1.0) value: Float). See Modifying Scenes for more details.

// Change its opacity.
engine.block.setOpacity(block, value = 0.5F)

When starting with an initial video, the scene’s page dimensions match the given resource and the scene is configured to be in pixel design units.

To later save your scene, see Saving Scenes.

Full Code

Here’s the full code:

import android.net.Uri
import kotlinx.coroutines.*
import ly.img.engine.*
fun createSceneFromVideoURL(
license: String,
userId: String,
) = CoroutineScope(
Dispatchers.Main,
).launch {
val engine = Engine.getInstance(id = "ly.img.engine.example")
engine.start(license = license, userId = userId)
engine.bindOffscreen(width = 100, height = 100)
val videoRemoteUri = Uri.parse("https://img.ly/static/ubq_video_samples/bbb.mp4")
val scene = engine.scene.createFromVideo(videoRemoteUri)
// Find the automatically added graphic block in the scene that contains the video fill.
val block = engine.block.findByType(DesignBlockType.Graphic).first()
// Change its opacity.
engine.block.setOpacity(block, value = 0.5F)
engine.stop()
}