Duet recording

Updated at:

The short video SDK provides the duet recording feature. This feature lets you record a video that combines an existing sample video with a new video captured by the camera. The two videos are arranged in a specific layout, such as a left-right split screen, an up-down split screen, or Picture-in-Picture (PiP). Each frame of the duet video contains images from both video sources, but the audio comes from the sample video. Duet recording is an upgrade to the basic recording feature and adds a new local video track.

Supported editions

Edition

Supported

Professional Edition

Supported

Standard Edition

Supported

Basic Edition

No

Key concepts

To better understand the feature, review the definitions of duet recording, track, and track layout.

Related classes

Name

Feature

AliyunMixRecorder

The core class for the duet recording feature. It includes core recording functions such as recording, preview settings, effect settings, and callback settings.

AliyunMixMediaInfoParam

A class for duet recording parameter configuration. It includes parameters such as the size and position of the camera window and the sample video window.

AliyunMixMediaRecordVideoInfo

A class to configure the captured video. Use it to set the video capture resolution.

AliyunMixMediaFileVideoInfo

A class to configure the sample video. Use it to set the file path, start time, and end time of the sample video for the duet.

Duet recording flow

Note

The duet recording feature requires camera and microphone permissions. Recording will fail if these permissions are not granted.

The duet recording flow is similar to the basic recording flow. The main differences are the input and output parameters and the preview view settings.

Phase

Flow

Description

Sample code

Basic

1

Create a recording instance and configure recording parameters.

Configure duet recording parameters

2

Configure callbacks.

Configure callbacks

3

Start or stop the preview.

Control preview

4

Start or stop recording.

Start recording

5

Generate a record for a completed recording.

Finish recording

Advanced

6

Like basic recording, duet recording supports effects such as retouching, filters, and background music, and features such as taking photos.

Other settings

Configure duet recording parameters

Set the duet recording parameters to initialize the duet recording instance. For more information about the parameters, see the API reference in Related classes.

Set duet recording parameters

AliyunMixMediaInfoParam *mixMediaInfo = [[AliyunMixMediaInfoParam alloc] init];
mixMediaInfo.outputSizeView = previewView;
mixMediaInfo.mixVideoInfo.filePath = mixVideoFile; // Path of the sample video

Set layout parameters

The coordinate system for the layout of the two videos is the same as the system coordinate system. The following example shows how to set a PiP layout.

// Set the sample video window size and position: place it on the bottom layer to cover the full window.
CGFloat mixWidth = previewView.bounds.size.width;
CGFloat mixHeight = previewView.bounds.size.height;
mixMediaInfo.mixVideoInfo.frame = CGRectMake(0, 0, mixWidth, mixHeight);
mixMediaInfo.mixVideoInfo.layerLevel = 1;

// Set the recording video with an aspect ratio of 9:16 in portrait mode, a resolution of 360p, and place it in the lower-right corner, above the sample video.
CGFloat recordRatio = 9.0 / 16.0;
mixMediaInfo.recordVideoInfo.resolution = CGSizeMake(360, 360 / recordRatio); 
CGFloat recordHeight = previewView.bounds.size.height * 0.5;
CGFloat recordWidth = recordHeight * recordRatio;
mixMediaInfo.recordVideoInfo.frame = CGRectMake(previewView.bounds.size.width - recordWidth - 4.0, previewView.bounds.size.height - recordHeight - 4, recordWidth, recordHeight);
mixMediaInfo.recordVideoInfo.layerLevel = 2;

Set other parameters

// (Optional) Set the border for the sample video.
mixMediaInfo.mixVideoInfo.borderInfo.color = UIColor.blueColor;
mixMediaInfo.mixVideoInfo.borderInfo.width = 2;
mixMediaInfo.mixVideoInfo.borderInfo.cornerRadius = 6.0;

// (Optional) Set the border for the recorded video.
mixMediaInfo.recordVideoInfo.borderInfo.color = UIColor.blueColor;
mixMediaInfo.recordVideoInfo.borderInfo.width = 1;
mixMediaInfo.recordVideoInfo.borderInfo.cornerRadius = 6.0;

Create a duet recording object

The parameters for creating a duet recording object are similar to those for basic recording. For more information, see Basic recording.

AliyunMixRecorder *_recorder = [[AliyunMixRecorder alloc] initWithMediaInfo:mixMediaInfo outputSize:CGSizeMake(720, 720)];
_recorder.outputType = AliyunIRecorderVideoOutputPixelFormatType420f;
_recorder.useFaceDetect = YES;
_recorder.faceDetectCount = 2;
_recorder.faceDectectSync = NO;
_recorder.frontCaptureSessionPreset = AVCaptureSessionPreset1280x720;
_recorder.GOP = 250;
_recorder.videoQuality = AliyunVideoQualityHight;
_recorder.recordFps = 30;
_recorder.outputPath = [taskPath stringByAppendingPathComponent:@"output.mp4"];
_recorder.cameraRotate = 0;
_recorder.beautifyStatus = YES;
_recorder.frontCameraSupportVideoZoomFactor = YES;

self.aliyunMixRecorder = _recorder;

Set the recording duration

Note

The duration of the final composited duet video is determined by the longer of the two videos. For example, if the recorded video is 10 s long and the sample video is 5 s long, the output duet video will be 10 s long.

// We recommend that you set the maximum duet recording duration to be the same as the sample video duration. When the maximum duration is reached, the recorderDidStopWithMaxDuration callback is triggered.
AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:[self resourcePath:mixVideoPath]]];
[self.aliyunMixRecorder setRecordMaxDuration:asset.aliyunVideoDuration];
[self.aliyunMixRecorder setRecordMinDuration:1.0];

Set the duet background

// Set the duet background color.
[self.aliyunMixRecorder setBackgroundColor:0xFF0100];

// Set the duet background image.
NSString *imgPath = [self.class resourcePath:@"pig.jpeg"];
[self.aliyunMixRecorder setBackgroundImageFilePath:imgPath imageDisplayMode:AliyunMixVideoBackgroundImageModeScaleAspectFit];

Set the output audio

You can enable acoustic echo cancellation (AEC) for the recording. You can also mix two audio tracks, output a single track, or mute the audio.

Note

If the output video is muted or uses only the sample video's audio, you do not need to enable echo cancellation.

// Set the hardware echo cancellation effect. The Hardware mode is recommended.
self.aliyunMixRecorder.recorderAECType = AliyunIRecorderAECTypeHardware;

// Set the composited video to use the recorded audio track.
[self.aliyunMixRecorder setMixAudioSource:MixAudioSourceTypeBoth];
[self.aliyunMixRecorder setMixAudioOriginalWeight:50 recordWeight:50];

Configure callbacks

Set callbacks to promptly obtain the progress and status of audio and video processing. For more information about the parameters, see the API reference in Related classes.

- (void)recorderDeviceAuthorization:(AliyunIRecorderDeviceAuthor)status {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (status == AliyunIRecorderDeviceAuthorAudioDenied) {
            [DeviceAuthorization openSetting:@"Microphone permission not granted."];
        } else if (status == AliyunIRecorderDeviceAuthorVideoDenied) {
            [DeviceAuthorization openSetting:@"Camera permission not granted."];
        }
    });
}

// Recording progress
- (void)recorderVideoDuration:(CGFloat)duration {
    NSLog(@"Mix Record Video Duration: %f", duration);
}

// A recording segment has stopped.
- (void)recorderDidStopRecording {
    NSLog(@"Mix Record Stop Recording");
}

// Recording stopped after reaching the maximum duration. You can now finish the recording.
- (void)recorderDidStopWithMaxDuration {
    NSLog(@"Mix Record Stop Recording With Max Duration");
    [self.aliyunMixRecorder finishRecording]; // Finish recording.
}

// Recording finished.
- (void)recorderDidFinishRecording {
    NSLog(@"Mix Record Did Finish Recording");
}

// Duet composition completed.
- (void)mixRecorderComposerDidComplete {
    NSLog(@"Mix Record Complete");
    [[MBProgressHUD HUDForView:self.view] hideAnimated:YES];
}

// An error occurred during composition.
- (void)mixRecorderComposerDidError:(int)errorCode {
    NSLog(@"Mix Record Error");
    [[MBProgressHUD HUDForView:self.view] hideAnimated:YES];
}

// Duet composition started.
- (void)mixRecorderComposerDidStart {
    NSLog(@"Mix Record Start");
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeDeterminate;
    hud.removeFromSuperViewOnHide = YES;
    hud.label.text = @"Compositing...";
}

// Duet composition progress.
- (void)mixRecorderComposerOnProgress:(CGFloat)progress {
    NSLog(@"Mix Record Progress: %f", progress);
    MBProgressHUD *hub = [MBProgressHUD HUDForView:self.view];
    hub.progress = progress / self.aliyunMixRecorder.recordDuration;
}

Control preview

For more information about the parameters, see the API reference in Related classes.

// Start the preview.
[self.aliyunMixRecorder startPreviewWithPositon:AliyunIRecorderCameraPositionFront];

// Stop the preview. This is usually called after recording is finished.
[self.aliyunMixRecorder stopPreview];

Start recording

`startRecording` and `stopRecording` must be called in pairs. You can call them one or more times. The SDK generates one or more temporary video clips accordingly. For more information about the parameters, see the API reference in Related classes.

// Start recording a video clip.
[self.aliyunMixRecorder startRecording];  

// Stop recording a video clip.
[self.aliyunMixRecorder stopRecording];

Finish recording

After recording is complete, call the finishRecording API to composite the two videos. For more information about the parameters, see the API reference in Related classes.

// Finish recording and composite the two videos.
[self.aliyunMixRecorder finishRecording];

Other settings

Similar to basic recording, duet recording supports effects such as retouching, filters, and background music. It also supports features such as taking photos. You can use the AliyunMixRecorder class to control these settings. The APIs are similar to those for basic recording. For more information, see Basic recording.