All Products
Search
Document Center

MixRecording

Last Updated: Jul 30, 2019

The short video SDK provides the duet feature for recording a duet that consists of two videos. The core classes are AliyunMixRecorder and AliyunMixComposer.

Edition difference

Edition Description
Professional edition All features are supported.
Standard edition All features are supported.
Basic edition The duet feature is not supported.

Related class

Name Function
AliyunMixRecorder The core class for recording a duet.
AliyunMixComposer The core class for producing a video from multiple videos.

Concepts

This section describes some basic concepts that help you better understand the duet feature.

1. Duet (AliyunMixRecorder)

The duet feature allows you to produce a video from two videos. One is a sample video and the other is taken by the camera. The two videos are arranged in the specified layout, for example, left-right split-screen, up-down split-screen, or picture-in-picture. Each frame of the produced video contains images from the two videos, and the audio of the sample video is used as the audio of the produced video. The following figure shows some sample layouts. In fact, the short video SDK allows you to completely customize the layout, which will be described later in this topic.track

2. Track (AliyunMixTrack)

The two videos mentioned earlier are abstracted to two tracks in the short video SDK: track A and track B. Track A stores the video collected by the camera and track B stores the sample video. This abstraction helps you better understand the concept of track layout.

3. Track layout (CGRect:trackDisplayFrame)

In iOS, a track contains the trackDisplayFrame property when being created. This property specifies the position and size of the area for displaying the track in the produced video. The position and size are represented in a coordinate system with the upper-left corner of the screen as the origin (0,0), as shown in the following figure.layout

According to this figure:The resolution of the produced video is 640 × 640. Track A is displayed in the area of (0,0,320,640) and track B is displayed in the area of (320,0,320,640). The sample code is available at the end of this topic.

Record a video by using AliyunMixRecorder

AliyunMixRecorder allows you to record a video and play another video simultaneously. For example, you can record a video by using the camera in the left part of the screen, and play another video in the right part of the screen.

Initialization

  • Initialize an AliyunMixRecorder object.Create an AliyunMixRecorder object and initialize it.
  1. /**
  2. Initializes an AliyunMixRecorder object.
  3. @param param The media information.
  4. @param outputSize The resolution of the produced video.
  5. @return The AliyunMixRecorder object.
  6. */
  7. - (instancetype)initWithMediaInfo:(AliyunMixMediaInfoParam *)param outputSize:(CGSize)outputSize;

An AliyunMixMediaInfoParam object specifies the window for recording a duet, the layout of the recorded video and sample video, the resolution of the recorded video, and information of the sample video. The properties are as follows:

  1. /**
  2. Required. The view of the window for recording a duet.
  3. */
  4. @property(nonatomic, strong) UIView *outputSizeView;
  5. /**
  6. The position and size of the window for playing the sample video.
  7. The SDK displays this window in the view specified by outputSizeView based on value of this property.
  8. */
  9. @property(nonatomic, assign) CGRect mixVideoViewFrame;
  10. /**
  11. The position and size of the window for recording a video by using the camera.
  12. The SDK displays this window in the view specified by outputSizeView based on value of this property.
  13. */
  14. @property(nonatomic, assign) CGRect previewViewFrame;
  15. /**
  16. The resolution of the video recorded by the camera.
  17. */
  18. @property(nonatomic, assign) CGSize previewVideoSize;
  19. /**
  20. The path of the sample video.
  21. */
  22. @property(nonatomic, copy) NSString *mixVideoFilePath;
  23. /**
  24. The start time of the sample video to be added to the duet.
  25. */
  26. @property(nonatomic, assign) CGFloat streamStartTime;
  27. /**
  28. The end time of the sample video to be added to the duet.
  29. */
  30. @property(nonatomic, assign) CGFloat streamEndTime;

``Other properties and methods of the AliyunMixRecorder class are similar to those of the AliyunIRecorder class. The recording process using the AliyunMixRecorder class is similar to that using the AliyunIRecorder class.

Control preview

  • Start the preview.
  1. /**
  2. Starts the preview.
  3. @param cameraPosition The camera to be used for recording, which is the front or rear camera.
  4. */
  5. - (void)startPreviewWithPositon:(AliyunIRecorderCameraPosition)cameraPosition;
  6. /**
  7. Starts the preview. By default, the front camera is used.
  8. */
  9. - (void)startPreview;
  • Stop the preview.
  1. - (void)stopPreview;
  • Destroy the AliyunMixRecorder object.
  1. - (void)destroyRecorder;

Record a video

  • Start recording a video clip.

    1. - (int)startRecording;
  • Stop recording a video clip.

    1. - (void)stopRecording;

startRecording and stopRecording must be called in pairs.

Set other parameters

Other parameters are the same as those for recording a video by using the AliyunIRecorder class. For more information, see Recording.

Produce a video by using AliyunMixComposer

AliyunMixComposer allows you to produce a video from multiple videos. It is an underlying class for displaying multiple videos on the same screen in the specified layout, such as left-right split-screen, up-down split-screen, picture-in-picture, or nine-square grid.

AliyunMixStream

AliyunMixStream specifies a video stream to be added to a track of the produced video.

Initialize an AliyunMixComposer object and set parameters

  1. /**
  2. The delegate object of production callbacks.
  3. */
  4. @property(nonatomic, weak) id<AlivcMixComposerDelegate> delegate;
  5. /**
  6. Required. The resolution of the produced video.
  7. */
  8. @property(nonatomic, assign) CGSize outputSize;
  9. /**
  10. Required. The path of the produced video.
  11. */
  12. @property(nonatomic, copy) NSString *outputPath;
  13. /**
  14. The bitrate. Unit: bit/s.
  15. */
  16. @property(nonatomic, assign) NSInteger bitrate;
  17. /**
  18. The video quality.
  19. */
  20. @property(nonatomic, assign) AliyunVideoQuality videoQuality;
  21. /**
  22. The average frame rate. Default value: 30.
  23. */
  24. @property(nonatomic, assign) CGFloat fps;
  25. /**
  26. The keyframe interval. Default value: 5.
  27. */
  28. @property(nonatomic, assign) NSInteger gop;

Create tracks

  • Create a track.
  1. /**
  2. Creates a track.
  3. @param trackDisplayFrame The area for displaying the track. The resolution of the produced video is used as the reference coordinate system. For example, if the resolution of the produced video is 960 × 960, an area of (0,0,480,960) indicates that the track occupies the left half of the screen.
  4. @return The AlivcMixTrack object.
  5. */
  6. - (AliyunMixTrack *)createTrack:(CGRect)trackDisplayFrame;
  • Specify the reference duration.
  1. /**
  2. Specifies the duration of a track as the duration of the produced video.
  3. @param referenceTrack The track.
  4. */
  5. - (void)setOutputDurationReferenceTrack:(AliyunMixTrack *)referenceTrack;
  • Specify the reference audio.
  1. /**
  2. Specifies the audio of a track as the audio of the produced video.
  3. @param referenceTrack The track.
  4. */
  5. - (void)setOutputAudioReferenceTrack:(AliyunMixTrack *)referenceTrack;

Add video streams

AliyunMixStream

AliyunMixStreamprovides the following properties:

  1. /**
  2. The path of the video file.
  3. */
  4. @property(nonatomic, copy) NSString *filePath;
  5. /**
  6. Optional.
  7. The cropping range of the video displayed in the track, which takes effect when the scaling mode is set to cropping.
  8. */
  9. @property(nonatomic, assign) CGRect innerCropFrame;
  10. /**
  11. The start time of the video displayed in the track.
  12. Unit: seconds.
  13. */
  14. @property(nonatomic, assign) CGFloat streamStartTime;
  15. /**
  16. The end time of the video displayed in the track.
  17. Unit: seconds.
  18. */
  19. @property(nonatomic, assign) CGFloat streamEndTime;
  20. /**
  21. The scaling mode of the video in the track, which can be padding or cropping.
  22. */
  23. @property(nonatomic, assign) AlivcContentMode mode;

Add a video stream

  1. /**
  2. Adds a video stream to a track.
  3. @param stream The video stream.
  4. */
  5. - (void)addStream:(AliyunMixStream *)stream;

Control production

  • Start the production.
  1. /**
  2. Starts the production.
  3. @return The return value.
  4. */
  5. - (int)start;
  • Pause the production.
  1. /**
  2. Pauses the production.
  3. @return The return value.
  4. */
  5. - (int)pause;
  • Resume the production.
  1. /**
  2. Resumes the production.
  3. @return The return value.
  4. */
  5. - (int)resume;
  • Cancel the production.
  1. /**
  2. Cancels the production.
  3. @return The return value.
  4. */
  5. - (int)cancel;

Sample code

  1. AliyunMixComposer *mixComposer = [[AliyunMixComposer alloc] init];
  2. mixComposer.outputPath = self.outputPath;
  3. mixComposer.outputSize = CGSizeMake(720,720);
  4. mixComposer.fps = 30;
  5. mixComposer.bitrate = 10000000;
  6. mixComposer.videoQuality = AliyunVideoQualityHight;
  7. mixComposer.gop = 5;
  8. mixComposer.delegate = (id)self;
  9. AliyunMixTrack *recordTrack = [mixComposer createTrack:CGRectMake(0,0,360,720)];
  10. NSString *videoPath = [videoAbsPaths objectAtIndex:idx];
  11. AliyunMixStream *recordStream = [[AliyunMixStream alloc] init];
  12. recordStream.filePath = videoPath;
  13. recordStream.mode = AlivcContentModeScaleAspectFit;
  14. [recordTrack addStream:recordStream];
  15. AliyunMixTrack *playerTrack = [mixComposer createTrack:CGRectMake(360,0,360,720)];
  16. AliyunMixStream *playerStream = [[AliyunMixStream alloc] init];
  17. playerStream.filePath = mixVideoFilePath;
  18. playerStream.mode = AlivcContentModeScaleAspectFit;
  19. [playerTrack addStream:playerStream];
  20. [mixComposer setOutputAudioReferenceTrack:playerTrack];
  21. [mixComposer start];