The API adds a few variables to the template execution context, which can be used to create smarter templates.
Dynamic variables
For each element in the scenes or in the movie, the API adds a few local variables:
ELEMENTID_start
: the start timecode of the element within the container (scene or movie)ELEMENTID_end
: the end timecode of the element within the container (scene or movie)ELEMENTID_duration
: the real duration of the element (it can be different from the asset duration)ELEMENTID_extratime
: the extra time value of the element
Where ELEMENTID
is the ID of the element.
Example 1
Let's see an example:
{
"comment": "Dynamically generated variables example",
"resolution": "full-hd",
"scenes": [
{
"elements": [
{
"id": "my_video",
"type": "video",
"src": "https://cdn.json2video.com/assets/videos/beach-01.mp4"
},
{
"type": "image",
"src": "https://cdn.json2video.com/assets/images/london-01.jpg",
"duration": 5,
"start": "{{my_video_end}}"
}
]
}
]
}
In this example, the image will start when the video ends.
The my_video_end
variable is the end timecode of the video element and it's calculated dynamically based on the video duration.
Example 2
Let's see another example:
{
"comment": "Dynamically generated variables example",
"resolution": "instagram-story",
"scenes": [
{
"elements": [
{
"id": "my_voice_over",
"type": "voice",
"text": "We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win, and the others, too.",
"voice": "en-US-AdamMultilingualNeural"
},
{
"type": "image",
"src": "https://cdn.json2video.com/assets/images/apollo11-01.jpg",
"duration": "{{my_voice_over_duration/3}}",
"start": 0,
"position": "center-center",
"height": 1920
},
{
"type": "image",
"src": "https://cdn.json2video.com/assets/images/apollo11-02.jpg",
"duration": "{{my_voice_over_duration/3}}",
"start": "{{my_voice_over_duration/3}}",
"position": "center-center",
"height": 1920
},
{
"type": "image",
"src": "https://cdn.json2video.com/assets/images/apollo11-03.jpg",
"duration": "{{my_voice_over_duration/3}}",
"start": "{{my_voice_over_duration/3*2}}",
"position": "center-center",
"height": 1920
}
]
}
]
}
In this example, the three images will play one after the other, with the duration of each image being one third of the voice over duration.
The my_voice_over_duration
variable is the duration of the voice over element and it's calculated dynamically based on the voice over audio duration.