Control the length of a produced video in regular editing scenarios using the main track, MaxDuration, or automatic speed adjustment.
Set the main track
When to use the main track
When you combine audio with videos or images of unknown lengths, setting time parameters on every clip is tedious and error-prone. Designate one track as the main track instead. The service aligns all other tracks to the main track's duration, truncating any track that runs longer. You can finish editing without knowing the exact length of each clip, and without setting In, Out, TimelineIn, or TimelineOut on every asset.
How to configure the main track
Set MainTrack to true on a video or audio track in the Timeline object. The final video length matches that track's duration.
Any track longer than the main track is truncated to the main track's duration.
Only one main track is allowed. If multiple tracks have
MainTrack: true, the first one takes effect.-
Only video and audio tracks can be the main track.
NoteOnly one track can be specified as the main track in a video. If multiple tracks are specified as main tracks, the first track takes precedence.
You can set only a video or audio track as the main track.
If
TimelineOutis set in theTimelineobject, it takes precedence and the service ignores the main track logic. UseTimelineInandInOutto position assets on the timeline instead.
Example
The timeline below has two tracks: one video track with four clips totaling 20 seconds, and one audio track with a single 15-second file. The audio track is the main track.
{
"VideoTracks": [{
"VideoTrackClips": [{
"MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video1.mp4",
"Out": 5
}, {
"MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video2.mp4",
"Out": 5
}, {
"MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video3.mp4",
"Out": 5
}, {
"MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video4.mp4",
"Out": 5
}]
}],
"AudioTracks": [{
"MainTrack": true,
"AudioTrackClips": [{
"MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_audio.mp3"
}]
}]
}
Final video length: 15 seconds. The video track is truncated from 20 seconds to match the audio main track.
Because only the audio file determines the output length, you can swap in different videos and images without touching any time parameters. This makes the main track approach well-suited for batch production where the audio stays fixed but the visual content changes.
Specify MaxDuration
When to use MaxDuration
When you have a large pool of video, audio, and subtitle materials and want to produce many fixed-length clips, setting time parameters on every asset is impractical. Set MaxDuration in OutputMediaConfig to enforce a hard cap on the output. Every track is automatically truncated to that limit, and materials without explicit display lengths (such as global images and banner text) are automatically set to MaxDuration.
How to configure MaxDuration
When calling the SubmitMediaProducingJob API, set MaxDuration in OutputMediaConfig to limit the output video length.
How MaxDuration works:
If any track's duration exceeds
MaxDuration, that track is truncated toMaxDuration. The final video never exceedsMaxDuration.If all tracks are shorter than
MaxDuration, the output length is the same as withoutMaxDuration.
If both
MaxDurationand the main track are set,MaxDurationtakes effect and the main track setting is ignored.
Example
The following examples show sample OutputMediaConfig and Timeline configurations.
OutputMediaConfig: Set MaxDuration to 15 seconds.
{
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_output.mp4",
"MaxDuration": 15
}
Timeline: Four tracks — two video tracks, one audio track, and one subtitle track. No time-related parameters are set on any clip.
Video track 1: a JPG image configured as a global image.
Video track 2: two videos added back to back.
Audio track: one audio file.
-
Subtitle track: banner text.
{ "VideoTracks": [{ "VideoTrackClips": [{ "Type": "GlobalImage", "MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_image1.jpg" }] }, { "VideoTrackClips": [{ "MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video1.mp4" }, { "MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video2.mp4" }] }], "AudioTracks": [{ "AudioTrackClips": [{ "MediaUrl": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_audio.mp3" }] }], "SubtitleTracks": [{ "SubtitleTrackClips": [{ "Type": "Text", "Content": "Banner text", "Alignment": "CenterCenter" }] }] }
Final video length: 15 seconds, matching MaxDuration. Specifically:
The global image display length is automatically set to 15 seconds.
The video track (two clips) and the audio track are truncated to 15 seconds if their combined or individual durations exceed that limit.
The banner text display length is automatically set to 15 seconds.
Automatic speed adjustment
When to use speed adjustment
When the main track is set, tracks that run longer are truncated by default. Truncation can break content integrity — for example, a narration may be cut off mid-sentence. The reverse problem also occurs: if the video is shorter than the audio main track, the video ends early and the remaining audio plays over a black screen.
Speed adjustment solves both problems. Instead of truncating, the service automatically speeds up or slows down a track so it finishes at exactly the same time as the main track, preserving all content while keeping the output precisely aligned.
How to configure speed adjustment
Set TrackShortenMode and TrackExpandMode on a video or audio track in the timeline to control how the track behaves relative to the main track. Both parameters currently only support AutoSpeed, which adjusts playback speed automatically.
TrackShortenMode: "AutoSpeed"— speeds up the track when it is longer than the main track.-
TrackExpandMode: "AutoSpeed"— slows down the track when it is shorter than the main track.Automatic speed adjustment and inter-track material alignment cannot be used together. Speed adjustment changes the timing of clips, which breaks alignment accuracy.
Automatic speed adjustment does not apply to tracks set to loop playback. Loop mode already fills the full track duration automatically.
Examples
Example 1: The video track is the main track. The audio track has TrackShortenMode: "AutoSpeed". If the audio is longer than the video, its speed is automatically increased so it ends when the video does.
{
"VideoTracks": [{
"MainTrack": true,
"VideoTrackClips": [{
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video1.mp4",
"In": 0,
"Out": 10
}, {
"Type": "Image",
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_image1.jpg",
"Duration": 4
}, {
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video2.mp4",
"In": 0,
"Out": 5
}]
}],
"AudioTracks": [{
"TrackShortenMode": "AutoSpeed",
"AudioTrackClips": [{
"Content": "Freshippo just opened a store in Huilongguan. It is the grand opening day, and there is quite a crowd at the store. Freshippo offers some great deals on snacks and drinks. Come check it out."
"Type": "AI_TTS",
"Voice": "zhiqing",
"Effects": [{
"Type": "Volume",
"Gain": 1
}, {
"FontSize": 34,
"Y": 0.658,
"Alignment": "TopCenter",
"AdaptMode": "AutoWrap",
"Type": "AI_ASR",
"Font": "FZHei-B01S"
}]
}]
}]
}
Example 2: The audio track is the main track. The video track has both TrackShortenMode and TrackExpandMode set to "AutoSpeed". The video speed is automatically increased if the video is longer than the audio, or decreased if it is shorter — ensuring the video ends exactly when the audio does.
{
"VideoTracks": [{
"TrackExpandMode": "AutoSpeed",
"TrackShortenMode": "AutoSpeed",
"VideoTrackClips": [{
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video1.mp4",
"In": 0,
"Out": 10
},
{
"Type": "Image",
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_image1.jpg",
"Duration": 4
},
{
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_video2.mp4",
"In": 0,
"Out": 5
}
]
}],
"AudioTracks": [{
"MainTrack": true,
"AudioTrackClips": [{
"Content": "Freshippo just opened a store in Huilongguan. It is the grand opening day, and there is quite a crowd at the store. Freshippo offers some great deals on snacks and drinks. Come check it out."
"Type": "AI_TTS",
"Voice": "zhiqing",
"Effects": [{
"Type": "Volume",
"Gain": 1
},
{
"FontSize": 34,
"Y": 0.658,
"Alignment": "TopCenter",
"AdaptMode": "AutoWrap",
"Type": "AI_ASR",
"Font": "FZHei-B01S"
}
]
}]
}]
}