The short video SDK provides a cropping module to allow you to crop videos by duration and aspect ratio, crop audio by duration, and crop images by aspect ratio. This topic describes how to use the short video SDK for iOS to crop videos.
Supported editions
Edition | Supported |
---|---|
Professional | Yes |
Standard | Yes |
Basic | Yes |
Related classes
Class | Description |
---|---|
AliyunCrop | A core class that defines audio and video cropping features, including video and audio cropping, cropping parameter settings, and callback settings. |
AliyunImageCrop | A core class that defines image cropping features, including image cropping, cropping parameter settings, and callback settings. |
Video cropping - regular cropping
Videos are encoded and decoded again during regular cropping. You can configure the parameters of the output video, such as the output resolution, bitrate, frame rate, keyframe interval, encoding format, and video quality.
For more information about operation parameters, see AliyunCrop.
// Call the initialization method to create a cropping object.
self.crop = [[AliyunCrop alloc] initWithDelegate:self];
// 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;
// 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;
[self.crop startCrop];
// 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 {
}
Video cropping - fast cropping
Videos are not encoded and decoded again during fast cropping. You can crop the video to a specified duration but cannot configure other parameters.
For more information about operation parameters, see AliyunCrop.
// Call the initialization method to create a cropping object.
self.crop = [[AliyunCrop alloc] initWithDelegate:self];
// Enable cropping optimization to allow fast cropping.
self.crop.shouldOptimize = YES;
// 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;
[self.crop startCrop];
// 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.
// Call the initialization method to create a cropping object.
self.crop = [[AliyunCrop alloc] initWithDelegate:self];
// 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;
[self.crop startCrop];
// 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.
// Call the initialization method to create a cropping object.
AliyunImageCrop *imageCrop = [[AliyunImageCrop alloc] init];
imageCrop.originImage = image;
// 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;
UIImage *outputImage = [imageCrop generateImage];