All Products
Search
Document Center

Cropping

Last Updated: Jul 30, 2019

The short video SDK allows you to crop videos by duration and frame size, crop music by duration, and crop images by frame size. The core cropping class is AliyunCrop.

``An instance object of AliyunCrop must be a property rather than a local variable.

Edition difference

Edition Description
Professional edition All features are supported.
Standard edition All features are supported.
Basic edition All features are supported.

Related class

Name Function
AliyunCrop The core cropping class.

Crop a video or music

Initialization

  • Call the initialization method to create a cropping object.
  1. /**
  2. @param error The error code.
  3. */
  4. - (instancetype)initWithDelegate:(id<AliyunCropDelegate>)delegate;
  • Set the path of the input video or music file.
  1. @property (nonatomic, copy) NSString *inputPath;
  • Set the path of the output video or music file.
  1. @property (nonatomic, copy) NSString *outputPath;

If the path of the output file contains folders in multiple levels, make sure that the folders have been created.

Set parameters

  • Set a resolution for the output video.
  1. @property (nonatomic, assign) CGSize outputSize;
  • Set the cropping start time. Unit: seconds.
  1. @property (nonatomic, assign) float startTime;
  • Set the cropping end time. Unit: seconds.
  1. @property (nonatomic, assign) float endTime;
  • Set the scaling mode.
  1. /**
  2. This property is not required for music cropping.
  3. */
  4. @property (nonatomic, assign) AliyunCropCutMode cropMode;
  • Set an effective area for video cropping.
  1. /**
  2. This property is not required for music cropping.
  3. */
  4. @property (nonatomic, assign) CGRect rect;

Calculation of the x, y, w, and h values within the cropping area rect: Set the upper-left corner of the video as the origin O (0,0), the horizontal direction as the x-axis, and the vertical direction as the y-axis. Assume that the video cropping area is Z. The x value is the distance along the x-axis between the upper-left vertex of the area Z and the origin O. The y value is the distance along the y-axis between the upper-left vertex of the area Z and the origin O. The w value is the width of the area Z. The h value is the height of the area Z. The x, y, w, and h values are measured in pixels.

  • Set the video quality.

Default value: AliyunVideoQualityMedium.

  1. /**
  2. This property is not required for music cropping.
  3. */
  4. @property (nonatomic, assign) AliyunVideoQuality videoQuality;
  • Set the encoding mode.

Hardware encoding is faster, while software encoding provides a higher definition.

  1. /**
  2. The encoding mode.
  3. 0: software encoding. 1: hardware encoding (default).
  4. */
  5. @property (nonatomic, assign) int encodeMode;
  • Set the frame rate.

Default value: 25.

  1. /**
  2. This property is not required for music cropping.
  3. */
  4. @property (nonatomic, assign) int fps;
  • Set the keyframe interval.

Default value: 5.

  1. @property (nonatomic, assign) int gop;
  • Set the bitrate. Unit: bit/s.
  1. /**
  2. This property is not required for music cropping.
  3. */
  4. @property (nonatomic, assign) int bitrate;
  • Set the background filling color in padding mode.
  1. /**
  2. This property is not required for music cropping.
  3. */
  4. @property (nonatomic, strong) UIColor *fillBackgroundColor;

Control cropping

  • Start cropping.
  1. - (int)startCrop;
  • Cancel cropping.
  1. - (void)cancel;

Listen to cropping callbacks

The delegate object needs to implement the AliyunCropDelegate protocol to monitor the cropping status. The AliyunCropDelegate protocol provides the following callbacks:

  • Callback to be called when the cropping fails
  1. /**
  2. @param progress The current progress in the range of 0-1.
  3. */
  4. - (void)cropOnError:(int)error;
  • Cropping progress callback
  1. - (void)cropTaskOnProgress:(float)progress;
  • Callback to be called when the cropping is completed
  1. - (void)cropTaskOnComplete;
  • Callback to be called when the cropping is canceled or the application is switched to the background
  1. - (void)cropTaskOnCancel;

Crop an image

The short video SDK provides the image cropping feature. The core class is AliyunImageCrop.

Initialization

Create an AliyunImageCrop object and initialize it.

Set parameters

  • Set the image to be cropped.
  1. @property (nonatomic, strong) UIImage *originImage;
  • Set a resolution for the output image.
  1. @property (nonatomic, assign) CGSize outputSize;
  • (Optional) Set the image scaling mode.
  1. /**
  2. 0: padding. 1: cropping.
  3. */
  4. @property (nonatomic, assign) AliyunImageCropMode cropMode;
  • (Optional) Set the cropping area (in pixels).
  1. @property (nonatomic, assign) CGRect cropRect;

This property is applicable to the cropping mode only. For example, for an image of 200 x 200 pixels, you can set cropRect to (0,0,100,100). (Unit: pixels)

  • (Optional) Set the background filling color in padding mode.
  1. @property (nonatomic, strong) UIColor *fillBackgroundColor;

Control cropping

  • Generate an image.

    1. - (UIImage *)generateImage;

The cropped image is returned. If the cropping fails, nil is returned.

Example

Crop a video.

  1. self.crop = [[AliyunCrop alloc] initWithDelegate:self];
  2. self.crop.inputPath = `The path of the video file to be cropped`;
  3. self.crop.outputPath = `The path of the output video file`;
  4. self.crop.cropMode = AliyunCropModeScaleAspectCut;
  5. self.crop.outputSize = CGSizeMake(540, 540); // Sets the resolution of the output video to 540 × 540.
  6. self.crop.rect = CGRectMake(0, 0, 320, 320); // It can be regarded that you draw a cropping frame on the current video. To prevent the video image from being cropped, set the width and height of the cropping frame to those of the input video. The aspect ratio of the cropping frame must be the same as that of the output video. Otherwise, distortion may occur.
  7. self.crop.startTime = 0.0;
  8. self.crop.endTime = 5.0;
  9. self.crop.fillBackgroundColor = [UIColor greenColor]; // Sets the background filling color in padding mode.
  10. [self.crop startCrop]; // Starts video cropping.

Crop music

  1. self.crop = [[AliyunCrop alloc] initWithDelegate:self];
  2. self.crop.inputPath = `The path of the music file to be cropped`;
  3. self.crop.outputPath = `The path of the output music file`;
  4. self.crop.startTime = 0.0;
  5. self.crop.endTime = 5.0;
  6. [self.crop startCrop]; // Starts music cropping.