All Products
Search
Document Center

Recording

Last Updated: Jul 30, 2019

The short video SDK provides the basic recording feature and allows you to add recording effects such as music, speed adjustment, and face stickers. The core recording class is AliyunIRecorder.

Edition difference

Edition Description
Professional edition All features are supported.
Standard edition The basic recording and music features are supported. The facial recognition feature is not supported.
Basic edition The basic recording feature is supported. The music and facial recognition features are not supported.

Related class

Name Function
AliyunIRecorder The core recording class.
AliyunClipManager The clip manager class. It is used to obtain clip information and delete video clips.
AliyunIRecorderDelegate The recording delegation protocol.

Recording process

The camera and microphone permissions are required for recording. Otherwise, the recording fails. The following figure shows the video recording process.

Recording sequence chart

Initialize

  • Initialize a recording instance.
  1. - (instancetype)initWithDelegate:(id<AliyunIRecorderDelegate>)delegate videoSize:(CGSize)videoSize;

videoSize specifies the resolution of the output video. Both the width and height must be even numbers. The recommended resolutions include 320 × 480, 540 × 960, and 720 × 1280.

  • Set taskPath.

taskPath specifies the path of the folder for storing configuration information.

  1. @property (nonatomic, copy) NSString *taskPath;
  • Set the path of the recorded video.
  1. @property (nonatomic, copy) NSString *outputPath;
  • Set the preview view.
  1. @property (nonatomic, strong) UIView *preview;

preview must use the same aspect ratio as videoSize.

Control preview

  • Start the preview.
  1. - (void)startPreview;
  2. - (void)startPreviewWithPositon:(AliyunIRecorderCameraPosition)cameraPosition;
  • Stop the preview.

You need to stop preview when the recording is completed or the application is switched to the background.

  1. - (void)stopPreview;
  • Destroy the recording object.

After the preview is completed, destroy the recording object to release recording resources.

  1. - (void)destroyRecorder;

Set recording parameters

You can set multiple recording parameters. Note that you can set the parameters only during preview, but not during recording.

  • Set the exposure.
  1. @property (nonatomic, assign) CGFloat exposureValue;
  • Set the resolution of the video collected by the front camera.

Default value: AVCaptureSessionPreset640x480. For more parameter values, see AVCaptureSession.h.

  1. @property (nonatomic, copy) NSString *frontCaptureSessionPreset;
  • Set the resolution of the video collected by the rear camera.

Default value: AVCaptureSessionPreset1280x720. For more parameter values, see AVCaptureSession.h.

  1. @property (nonatomic, copy) NSString *backCaptureSessionPreset;
  • Set the camera angle.
  1. @property (nonatomic, assign) int cameraRotate;
  • Set the manual focus point.
  1. @property (nonatomic, assign) CGPoint focusPoint;
  • Set the zoom factor.

Each time you call videoZoomFactor, the actual zoom factor is 1/100 of the specified value. For example, if you set videoZoomFactor to 10.0f, the actual zoom factor is 0.1f.

  1. @property (nonatomic, assign) CGFloat videoZoomFactor;

If the front camera is used for recording, this setting is invalid.

  • Set the encoding mode.
  1. /**
  2. 0: software encoding. 1: hardware encoding.
  3. */
  4. @property (nonatomic, assign) int encodeMode;
  • Set the keyframe interval.

Default value: 5. Recommended value: 1-300.

  1. @property (nonatomic, assign) int GOP;
  • Set the recording frame rate.

Default value: 25.

  1. @property (nonatomic, assign) int recordFps;
  • Set whether to mute the audio during recording.
  1. @property (nonatomic, assign) BOOL mute;

If music is added during recording, this setting is invalid.

  • Set the video quality.
  1. @property (nonatomic, assign) AliyunVideoQuality videoQuality;

If the bitrate parameter is set, this setting is invalid.

  • Set the bitrate of the output video. Unit: bit/s.
  1. @property (nonatomic, assign) int bitrate;
  • Set the video format.

The short video SDK supports the following video formats:

  • kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

  • kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange

  • kCVPixelFormatType_32BGRA

Default format: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange.

  1. @property (nonatomic, assign) AliyunIRecorderVideoOutputPixelFormatType outputType;
  • Asynchronously obtain an image that is taken.
  1. /**
  2. image: The rendered image.
  3. rawImage: The original image that is collected.
  4. */
  5. - (void)takePhoto:(void (^)(UIImage *image, UIImage *rawImage))handler;
  • Switch between the front and rear cameras.
  1. /**
  2. @return The camera position after switching.
  3. */
  4. - (AliyunIRecorderCameraPosition)switchCameraPosition;
  • Switch among flash modes circularly.The following flash modes are supported: off, on, and auto. The switching order is off > on > auto. Default value: off.
  1. /**
  2. @return The flash mode after switching.
  3. */
  4. - (AliyunIRecorderTorchMode)switchTorchMode;
  • Switch to the specified flash mode.
  1. /**
  2. @param torchMode The specified flash mode.
  3. @return YES if the operation is successful.
  4. */
  5. - (BOOL)switchTorchWithMode:(AliyunIRecorderTorchMode)torchMode;

Configure effects

Beautification

  • Turn on or off the beautification switch.
  1. @property (nonatomic, assign) BOOL beautifyStatus;
  • Set the beautification level.

Range: [0,100].

  1. @property (nonatomic, assign) int beautifyValue;

Common filter

The filter resources of the short video SDK are stored in the filter folder, which contains a configuration file and related resources. AliyunEffectFilter indicates a filter resource. You can use the initialization method to construct a filter instance. The path parameter specifies the path of the folder for storing the filter resource.

  • Generate a filter object.
  1. - (id)initWithFile:(NSString *)path;
  • Add a common filter.
  1. - (int)applyFilter:(AliyunEffectFilter *)filter;

Filters cannot be overlaid. The new filter replaces the existing one each time you call the applyFilter method.

  • Remove a common filter.
  1. - (void)deleteFilter;

Animated filter

Both animated filters and common filters are created by using AliyunEffectFilter, but they use different resources. The demo provides the resource files of several animated filters, such as jitter, phantom, sci-fi, hazy, and ghost.

  • Add an animated filter.
  1. - (int)applyAnimationFilter:(AliyunEffectFilter *)filter;
  • Remove an animated filter.
  1. - (void)deleteAnimationFilter;

Music

AliyunEffectMusic indicates a music resource. You can use the initialization method to construct a music instance. The path parameter specifies the path of the music file.

  • Generate a music object.
  1. - (id)initWithFile:(NSString *)path;
  • Add music.
  1. - (int)applyMusic:(AliyunEffectMusic *)music;

You can call this method only before recording starts. This method cannot be called during or after recording.

MV

The MV resources of the short video SDK are stored in the MV folder, which contains a configuration file and related resources. The downloaded MV package contains four folders with MV resources in different aspect ratios, which apply to different output resolutions. AliyunEffectMV indicates an MV resource. You can use the initialization method to construct an MV instance. The path parameter specifies the path of the folder for storing the MV resource.

  • Generate an MV object.
  1. - (id)initWithFile:(NSString *)path;
  • Add an MV.
  1. - (int)applyMV:(AliyunEffectMV *)mv;
  • Remove an MV.
  1. [recorder applyMV:nil]

Sticker and facial recognition

The sticker base class is AliyunEffectPaster.

  • Add an animated sticker.
  1. /**
  2. @param paster The animated sticker.
  3. */
  4. - (int)applyPaster:(AliyunEffectPaster *)paster;
  • Set whether to enable facial recognition.

After facial recognition is enabled, the system automatically tracks faces when detecting animated face stickers.

  1. @property (nonatomic, assign) BOOL useFaceDetect;
  • Set the maximum number of faces that can be recognized.

    1. /**
    2. Maximum value: 3. Minimum value: 1. If facial recognition is not required, set useFaceDetect to NO.
    3. */
    4. @property (nonatomic, assign) int faceDetectCount;
  • Set whether animated face stickers are attached to faces in real time.

    Default value: YES. If animated face stickers are attached to faces in real time, the effect is better, but the video may freeze on earlier devices. If animated face stickers are not attached to faces in real time, the video is smooth, but the effect is poor.

    1. @property (nonatomic, assign) BOOL faceDectectSync;

    We recommend that you set this parameter to YES for iPhone 6 and later models and to NO for models earlier than iPhone 6.

  • Remove an animated sticker.

  1. `- (void)deletePaster:(AliyunEffectPaster *)paster;`

Watermark

You set a watermark by adding an image and rendering it.

  • Set the position of an animated sticker or a watermark.
  1. /**
  2. @param rect The position (x,y,width,height), where x, y, width, and height are all ratios.
  3. @param effect The specified effect. (Currently, only the watermark and common animated sticker effects are supported.)
  4. */
  5. - (void)setEffectView:(CGRect)rect effect:(AliyunEffect *)effect;

The rect parameter specifies the position (x,y,width,height), where x, y, width, and height are all ratios. For example, if the size of the video view is (400,400) and the animated sticker position is (50,50,100,100), the value of rect is (0.125,0.125,0.25,0.25). The effect parameter specifies the effect. (Currently, only the watermark and common animated sticker effects are supported.)

Custom rendering

The short video SDK provides multiple rendering callbacks, including audio callbacks (for services such as audio rendering) and video callbacks (including data or texture callbacks).

  • Return the original video data.
  1. /**
  2. @param sampleBuffer The video data.
  3. */
  4. - (void)recorderOutputVideoRawSampleBuffer:(CMSampleBufferRef)sampleBuffer;
  • Return the original audio data.
  1. /**
  2. @param sampleBuffer The audio data.
  3. */
  4. - (void)recorderOutputAudioRawSampleBuffer:(CMSampleBufferRef)sampleBuffer;
  • Custom rendering 1
  1. /**
  2. @param sampleBuffer The original data.
  3. @return The pixel buffer after custom rendering.
  4. */
  5. - (CVPixelBufferRef)customRenderedPixelBufferWithRawSampleBuffer:(CMSampleBufferRef)sampleBuffer;
  • Custom rendering 2

Use the pixel buffer and the ID of the texture (only in BGRA format).

  1. /**
  2. @param pixelBuffer The camera data.
  3. @param textureName The texture of the camera data.
  4. @return The ID of the texture after custom rendering.
  5. */
  6. - (NSInteger)recorderOutputVideoPixelBuffer:(CVPixelBufferRef)pixelBuffer textureName:(NSInteger)textureName;
  • Custom rendering 3
  1. /**
  2. @param srcTexture The ID of the original video frame texture.
  3. @param size The size of the original video frame texture.
  4. @return The texture ID.
  5. */
  6. - (int)customRender:(int)srcTexture size:(CGSize)size;
  • Custom rendering 4
  1. /**
  2. This method needs to be implemented when the camera data is in BGRA or YUV format.
  3. @param textureName The ID of the original texture.
  4. @return The ID of the texture after custom rendering.
  5. */
  6. - (NSInteger)recorderOutputVideoTextureName:(NSInteger)textureName textureSize:(CGSize)textureSie;
  1. /**
  2. This method needs to be implemented only when the camera data is in YUV format.
  3. @param textureName The ID of the original UV texture.
  4. @return The ID of the texture after custom rendering.
  5. */
  6. - (NSInteger)recorderOutputVideoUVTextureName:(NSInteger)textureName;

Record videos

  • Start recording a video clip.

The short video SDK supports recording of a single video clip or multiple video clips. Call this method in the preview status to start recording.

  1. - (int)startRecording;
  • Stop recording a video clip.
  1. - (void)stopRecording;
  • After you call the stopRecording method, the short video SDK saves the recorded video. You can perform other operations only after receiving the - (void)recorderDidStopRecording callback of AliyunIRecorderDelegate.
  • startRecording and stopRecording must be called in pairs. They can be called one or more times. The short video SDK generates one or more temporary video files accordingly.

Complete recording

  • Complete recording.
  1. - (void)finishRecording;
  1. > + After the `- (void)recorderDidFinishRecording` callback of `AliyunIRecorderDelegate` is received, the recording is completed and the recorded video file is saved in the path specified by `outputPath`.

Handle events

You need to handle special events, for example, the screen is locked, a call comes in, or the application is switched to the background.

You need to monitor the UIApplicationWillResignActiveNotification event. Before the application enters the inactive status, call the stopRecording and stopPreview methods. You also need to monitor the UIApplicationDidBecomeActiveNotification event. After the application enters the active state, call the startPreview method to start preview again.

Example

  1. - (void)viewDidLoad {
  2. // Initializes the recorder instance.
  3. self.recorder = [[AliyunIRecorder alloc] initWithDelegate:self videoSize:CGSizeMake(720,1280)];
  4. self.recorder.preview = self.previewView;
  5. self.recorder.outputPath = `The path of the recorded video`;
  6. self.recorder.outputType = AliyunIRecorderVideoOutputPixelFormatType420f; // To support facial recognition, the data must be in YUV format.
  7. self.recorder.taskPath = `taskPath`;
  8. // Sets video clip parameters.
  9. self.clipManager = self.recorder.clipManager;
  10. self.clipManager.maxDuration = 15;
  11. self.clipManager.minDuration = 0.5;
  12. }
  13. - (void)recordButtonTouchesBegin {
  14. // Starts recording when the recording button is tapped.
  15. [self.recorder startRecording];
  16. }
  17. - (void)recordButtonTouchesEnd {
  18. // Stops recording when the recording button is tapped again.
  19. [self.recorder stopRecording];
  20. }
  21. - (void)recorderDidStopRecording {
  22. // Completes recording when receiving the recording completion callback.
  23. [self.recorder finishrecording]
  24. }
  25. - (void)recorderDidStopWithMaxDuration {
  26. // Completes recording when receiving the callback indicating that the maximum duration is reached.
  27. [self.recorder finishrecording]
  28. }
  29. - (void)recorderDidFinishRecording {
  30. // The recording is completed and the video has been saved to the specified path.
  31. }