Video cropping

Updated at:
Copy as MD

Use the Short Video SDK cropping module to crop videos by duration and aspect ratio, audio by duration, and images by aspect ratio on iOS.

Supported editions

Edition Supported
Professional Yes
Standard Yes
Basic Yes

Related classes

Class Description
AliyunCrop Provides audio and video cropping, parameter configuration, and callback settings.
AliyunImageCrop Provides image cropping, parameter configuration, and callback settings.
Important Audio and video cropping is asynchronous. AliyunCrop instances must be member variables, not local variables. Switching to the background during cropping causes failure.

Regular cropping

Regular cropping re-encodes the video. You can configure output resolution, bitrate, frame rate, keyframe interval, encoding format, and video quality.

For more information about operation parameters, see AliyunCrop.

Perform initialization
// Call the initialization method to create a cropping object.
self.crop = [[AliyunCrop alloc] initWithDelegate:self];
Set input path, output path, and time range
// Specify the input path and output path of the video.
self.crop.inputPath = [self.class resourcePath:@"input.mp4"];
self.crop.outputPath =  [self.class resourcePath:@"output.mp4"];

// Specify the points in time to start and end cropping. Unit: seconds.
self.crop.startTime = 0.0;
self.crop.endTime = 5.0;
Note Ensure the output directory exists before cropping.
Set cropping parameters
// Specify the resolution of the output video.
self.crop.outputSize = CGSizeMake(720, 720);

// Specify the cropping area.
self.crop.rect = CGRectMake(0, (1280-720) / 2, 720, 720);

// Specify the cropping mode.
self.crop.cropMode = AliyunCropModeScaleAspectCut;

// Specify the bitrate. Unit: bit/s.
self.crop.bitrate = 1000 * 1000;

// Specify the frame rate.
self.cropl.fps = 30;

// Specify the keyframe interval.
self.crop.gop = 90;

// Specify the video quality.
self.crop.videoQuality = AliyunVideoQualityHight;

// Set the encoding mode to hardware encoding.
self.crop.encodeMode = 1;
Start cropping
[self.crop startCrop]; 
Set callbacks
// The callback that is returned when an error occurs during cropping.
- (void)cropOnError:(int)error {
}
 
 // The callback for cropping progress.
- (void)cropTaskOnProgress:(float)progress {
}

// The callback that is returned when the cropping is complete.
- (void)cropTaskOnComplete {
}

// The callback that is returned when the cropping is canceled.
- (void)cropTaskOnCancel {
}

Fast cropping

Fast cropping trims the video to a specified duration without re-encoding. Other parameters cannot be configured.

For more information about operation parameters, see AliyunCrop.

Perform initialization
// Call the initialization method to create a cropping object.
self.crop = [[AliyunCrop alloc] initWithDelegate:self];
Enable fast cropping
// Enable cropping optimization to allow fast cropping.
self.crop.shouldOptimize = YES;
Set input path, output path, and time range
// Specify the input path and output path of the video.
self.crop.inputPath = [self.class resourcePath:@"input.mp4"];
self.crop.outputPath =  [self.class resourcePath:@"output.mp4"];

// Specify the points in time to start and end cropping.
self.crop.startTime = 0.0;
self.crop.endTime = 5.0;
Note Ensure the output directory exists before cropping.
Start cropping
[self.crop startCrop]; 
Set callbacks
// The callback that is returned when an error occurs during cropping.
- (void)cropOnError:(int)error {
}
 
 // The callback for cropping progress.
- (void)cropTaskOnProgress:(float)progress {
}

// The callback that is returned when the cropping is complete.
- (void)cropTaskOnComplete {
}

// The callback that is returned when the cropping is canceled.
- (void)cropTaskOnCancel {
}

Audio cropping

For more information about operation parameters, see AliyunCrop.

Perform initialization
// Call the initialization method to create a cropping object.
self.crop = [[AliyunCrop alloc] initWithDelegate:self];
Set input path, output path, and time range
// Specify the input path and output path of the audio file.
self.crop.inputPath = [self.class resourcePath:@"input.mp3"];
self.crop.outputPath =  [self.class resourcePath:@"output.mp3"];

// Specify the points in time to start and end cropping. Unit: seconds.
self.crop.startTime = 0.0;
self.crop.endTime = 5.0;
Note Ensure the output directory exists before cropping.
Start cropping
[self.crop startCrop]; 
Set callbacks
// The callback that is returned when an error occurs during cropping.
- (void)cropOnError:(int)error {
}
 
 // The callback for cropping progress.
- (void)cropTaskOnProgress:(float)progress {
}

// The callback that is returned when the cropping is complete.
- (void)cropTaskOnComplete {
}

// The callback that is returned when the cropping is canceled.
- (void)cropTaskOnCancel {
}

Image cropping

For more information about operation parameters, see AliyunImageCrop.

Perform initialization
// Call the initialization method to create a cropping object.
AliyunImageCrop *imageCrop  = [[AliyunImageCrop alloc] init];
Specify the input image
imageCrop.originImage = image;
Set cropping parameters
// Specify the resolution of the output image.
imageCrop.outputSize = CGSizeMake(200, 200);
// (Optional) Specify the cropping area. Unit: pixels.
imageCrop.cropRect = CGRectMake(50, 0, 200, 200);
// (Optional) Specify the image cropping mode.
imageCrop.cropMode = AliyunImageCropModeAspectCut;
Generate the cropped image
UIImage *outputImage = [imageCrop generateImage];