Video merging

Updated at:

The short video SDK provides the `AliyunIMixComposer` class for merging multiple videos offline into a single video. You can create effects such as Picture-in-Picture (PiP), nine-square grid, and split-screen layouts, and add multiple video tracks. This topic describes the video merging process for the short video SDK for iOS and provides sample code.

Supported editions

Edition Supported
Professional Edition Supported
Standard Edition Supported
Basic Edition No

Related classes

Class Feature
AliyunMixComposer The core class for video merging. It merges multiple videos and creates multi-video effects on the same screen, such as sequential joining, left-right split-screen, Picture-in-Picture (PiP), and nine-square grid.
AliyunMixTrack A video track created by `AliyunMixComposer` to which video streams can be added.
AliyunMixStream A video stream that composes the merged video and is added to a video track.
AliyunPureColorBorderInfo Contains border information for a track stream.

Merging structure

拼接结构

Merging process

Stage Flow Description Sample code
Basic 1 Create a merging instance and set the delegate for callbacks. Initialization
2 Create multiple tracks and video streams, and then add the video streams to their respective tracks. Create tracks
3 Configure output parameters such as the path, width, and height of the merged video. Configure output parameters
4 Set callbacks and start merging. Start merging
Advanced 5 Cancel, pause, or resume merging as needed. Control merging

Initialization

Create a merging instance and set the delegate for callbacks. For more information about the parameters, see Related classes.

// Create a mixComposer instance.
AliyunMixComposer *mixComposer = [[AliyunMixComposer alloc] init];

// Set the delegate for callbacks.
mixComposer.delegate = self;

Create tracks

Create multiple tracks and video streams, and then add the video streams to their respective tracks. For more information about the parameters, see Related classes.

Add tracks
// Add a track on the left.
AliyunMixTrack *recordTrack = [mixComposer createTrack:CGRectMake(0,0,360,720)];

// Add a track on the right.
AliyunMixTrack *playerTrack = [mixComposer createTrack:CGRectMake(360,0,360,720)];

// Set track parameters.
// Specify the track for audio output.
recordTrack.outputAudioReferenceTrack = YES;
// Specify the audio output weight.
recordTrack.outputAudioWeight = 100;

// Specify this track to determine the duration of the output video.
recordTrack.outputDurationReferenceTrack = YES;

// Set the track border.
AliyunPureColorBorderInfo *info = [[AliyunPureColorBorderInfo alloc] init];
info.width = 10.f;
info.cornerRadius = 10.f;
info.color = [UIColor redColor];
recordTrack.borderInfo = info;
                
Note You can specify the audio for the output video by setting `outputAudioReferenceTrack` and `outputAudioWeight` for each track. If `outputDurationReferenceTrack` is set to `YES` for multiple tracks, the setting of the last track created overwrites the previous ones.
Add video streams to tracks
// Add a video stream to the left track.
AliyunMixStream *recordStream = [[AliyunMixStream alloc] init];
recordStream.filePath = videoPath;
recordStream.mode = AlivcContentModeScaleAspectFit;
[recordTrack addStream:recordStream];

// Add a video stream to the right track.
AliyunMixStream *playerStream = [[AliyunMixStream alloc] init];
playerStream.filePath = mixVideoFilePath;
playerStream.mode = AlivcContentModeScaleAspectFit;
[playerTrack addStream:playerStream];

Configure output parameters

Configure output parameters such as the path, resolution, and frame rate of the merged video. For more information about the parameters, see Related classes.

// Output path
mixComposer.outputPath = self.outputPath;

// Output resolution
mixComposer.outputSize = CGSizeMake(720,720);

// Output frame rate
mixComposer.fps = 30;

// Group of Pictures (GOP)
mixComposer.gop = 90;

// Video quality
mixComposer.videoQuality = AliyunVideoQualityHight;

Start merging

Start merging and use the delegate for callbacks. For more information about the parameters, see Related classes.

// Start merging.
[mixComposer start];


// Delegate callbacks.
// Merging progress.
- (void)mixComposerOnProgress:(float)progress {
}

// Merging completed.
- (void)mixComposerDidComplete {
}

// Merging error.
- (void)mixComposerDidError:(int)error {
}

Control merging

Cancel, pause, or resume merging as needed. For more information about the parameters, see Related classes.

// Pause merging.
[mixComposer pause];

// Resume merging.
[mixComposer resume];

// Cancel merging.
[mixComposer cancel];