This topic describes the methods that you can use to control the length of a produced video in regular editing scenarios.
1. Set the main track
1.1 Background
Pain point: You recorded an audio file and want to produce a video by combining the audio file with an unknown number of videos or images that have unknown lengths. You want the display length of the videos or images to align with the length of the audio. You do not want to spend too much effort in configuring the time parameters of each video or image.
Solution: In the preceding scenario, you can set the track where the audio material is located as the main track. The lengths of other tracks are adjusted based on the length of the main track. If the length of a track exceeds the length of the main track, the track is truncated based on the length of the main track.
Benefits:
This way, you can complete editing without the need to know the lengths of the materials.
The workload to configure timeline parameters such as In, Out, TimelineIn, and TimelineOut is reduced.
1.2 Introduction
You can specify a video or audio track as the main track by setting the MainTrack
field to true
in the timeline.
Take note of the following information about the main track:
The length of the final video is the same as the length of the specified main track.
If the length of a track exceeds the length of the main track, the track is truncated based on the length of the main track.
Only 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.
1.3 Example
In the following timeline example:
Two tracks exist, including one video track and one audio track.
Four videos are added to the video track, with a total length of 20 seconds. No time-related parameters are set for the audio track, but the length of the "your_audio.mp3" audio file is 15 seconds.
The audio track is specified as 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: The length of the video output is 15 seconds, which is the same as the length of the audio track specified as the main track.
You can use a fixed audio file and replace only videos and images without the need to configure the time parameters of the materials. This allows you to generate videos that meet different business requirements.
2. Specify MaxDuration
2.1 Background
In an age where short videos are popular, you may want to produce videos that have a fixed length, such as 15 seconds or 20 seconds.
Pain point: You have a large amount of video and audio materials, and you want to use these materials to generate different videos with a fixed length, but you do not want to spend too much effort in configuring the time-related parameters of each material.
Solution: In the preceding scenario, you can configure the MaxDuration parameter to specify the maximum length of a video. In this video, the video track, audio track, subtitle track, and effect track are automatically adjusted based on the specified MaxDuration parameter.
If the length of a material exceeds the value of MaxDuration, the material is automatically truncated.
If you do not specify the display length of a global image or banner text, the display lengths of these materials are also automatically set to the value of MaxDuration.
Benefits:
You can generate videos with a fixed length in an efficient manner.
The workload to configure timeline parameters such as In, Out, TimelineIn, and TimelineOut is reduced.
You can edit a large number of videos in an efficient manner.
2.2 Introduction
When you call the SubmitMediaProducingJob operation to submit an editing job, you can set the MaxDuration
field in the OutputMediaConfig parameter to specify the maximum length of the final video.
Take note of the following limits about MaxDuraiton
:
If the length of a track exceeds the length specified by
MaxDuration
, the track is truncated based on the value ofMaxDuration
. The length of the final video does not exceed the length specified byMaxDuration
.If the lengths of all tracks are shorter than the length specified by
MaxDuration
, the length of the final video is the same as whenMaxDuration
is not specified.
If
MaxDuration
and the main track are specified,MaxDuration
takes effect and the main track becomes invalid.
2.3 Example
The following section provides sample configurations of OutputMediaConfig and Timeline.
In the OutputMediaConfig example:
The
MaxDuration
parameter is set to 15 seconds.
{
"MediaURL": "https://your-bucket.oss-cn-shanghai.aliyuncs.com/your_output.mp4",
"MaxDuration": 15
}
In the Timeline example:
A total of four tracks exist, including two video tracks, an audio track and a subtitle track.
In the first video track, an image in the JPG format is specified as the global image.
Two videos are continuously added to the second video track, and no time-related parameters are specified.
An audio material is added to the audio track, and no time-related parameters are specified.
Banner text is added to the subtitle track, and no time-related parameters are specified.
{
"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: The length of the video output is 15 seconds, which is the same as the length specified by MaxDuration
.
The display length of the global image is automatically set to 15 seconds.
In most scenarios, video and audio materials exceed 15 seconds. In this case, the video track that contains two videos and the audio track that contains one audio material are automatically truncated to 15 seconds.
The display length of the banner text is automatically set to 15 seconds.
3. Automatic speed adjustment of tracks
3.1 Background
Pain point: After the main track is set, other tracks are truncated based on the length of the main track by default. However, when the track is truncated, the integrity of the content is damaged, which makes the final video unusable. For example, if the video track is set as the main track and the audio track is truncated, the last few words of a complete sentence may be lost. In a different scenario, you set the audio track as the main track. If the video is longer than the audio track, the video is truncated. However, if the video content is shorter than the audio track, no video content exists when the audio has not finished playing, which affects the quality of the final video.
Solution: In the preceding scenario, you can specify a main track and configure automatic speed adjustment for other tracks. If the length of a video track exceeds the length of the main track, the video speed is automatically increased to shorten the length of the video track. If the length of the video track is shorter than the length of the main track, the video speed is automatically decreased to increase the length of the video track.
Benefits:
You do not need to ensure that the length of the video is the same as the length of the audio. If the length of the video is different from the length of the video, the integrity of the video content can be guaranteed.
When the content of some tracks is insufficient, you can slow down the tracks to prevent black screens.
3.2 Introduction
In the Timeline parameter, you can configure TrackShortenMode
when the current track is longer than the main track and configure TrackExpandMode
when the current track is shorter than the main track. These two modes support only AutoSpeed
, which indicates automatic speed adjustment.
Notes:
You cannot configure automatic speed adjustment and inter-track material alignment at the same time, because automatic speed adjustment affects the accuracy of material alignment.
Automatic speed adjustment does not take effect when the audio is played in a loop, because loop playback automatically complements the entire track.
3.3 Example
Example 1: The video track is the main track. If the audio is longer than the video, the audio speed is automatically increased to ensure that the audio ends when the video ends.
{
"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. If the video is longer than the audio, the video speed is automatically increased. If the video is shorter than the audio, the video speed is automatically decreased. This ensures that the video ends when the audio ends.
{
"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"
}
]
}]
}]
}