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.

Supported editions

Edition Supported
Professional Yes
Standard Yes
Basic Yes

Related classes

Class Description
AliyunICrop The core class that defines cropping features, including cropping, cropping parameter settings, and callback settings.
AliyunCropCreator The cropping factory class that is used to obtain cropping instances.
CropParam A class that defines cropping parameters, including the output width and height, cropping mode, and output path.
CropCallback A class that defines cropping callbacks, including the cropping completion callback, cropping progress callback, and cropping failure callback.

Cropping process

Step Description Sample code
1 Create a cropping instance. Create a cropping instance
2 Configure cropping parameters such as the output width and height, cropping mode, and output path. Configure cropping parameters
3 Configure callbacks such as the cropping completion callback, cropping progress callback, and cropping failure callback. Configure cropping callbacks
4 Start cropping. Start cropping
5 Release resources. Release resources

Create a cropping instance

Create a cropping instance. For more information about the parameters that are used in the code, see Related classes.

Create a cropping instance.
AliyunICrop cropper = AliyunCropCreator.createCropInstance(context);

Configure cropping parameters

Configure cropping parameters such as the output width and height, cropping mode, and output path. The setOutputWidth(), setOutputHeight(), setOutputPath(), and setInputPath() parameters are required. For more information about the parameters that are used in the code, see Related classes.

// Configure cropping parameters.
CropParam cropParam = new CropParam();
// Specify the output width. Unit: pixels.
cropParam.setOutputWidth(720);  
// Specify the output height. Unit: pixels.
cropParam.setOutputHeight(1080); 
 // Specify the cropping mode. The value VideoDisplayMode.SCALE specifies the scaling mode. The value VideoDisplayMode.FILL specifies the padding mode.
cropParam.setScaleMode(VideoDisplayMode.Scale);
// Specify the output file path.
cropParam.setOutputPath(outputPath); 
// Specify the input video file path.
cropParam.setInputPath(inputPath); 
// Specify the start time of cropping. Unit: microseconds.
cropParam.setStartTime(startTime); 
// Specify the end time of cropping. Unit: microseconds.
cropParam.setEndTime(endTime); 
// Specify the type of the media file, such as the image, video, or audio file.
cropParam.setMediaType(MediaType.ANY_VIDEO_TYPE); 
// Specify the cropping rectangle.
int startCropPosX = 0;
int startCropPoxY = 0;
Rect cropRect = new Rect(startCropPosX, startCropPoxY, startCropPosX + outputWidth,
    startCropPoxY + outputHeight);
cropParam.setCropRect(cropRect);
// Specify the frame rate.
cropParam.setFrameRate(30);
//gop
cropParam.setGop(5);
// Specify the video quality.
cropParam.setQuality(VideoQuality.HD);
// Specify the video encoding format.
cropParam.setVideoCodec(VideoCodecs.H264_HARDWARE);
// Specify the fill color.
cropParam.setFillColor(Color.BLACK);
cropper.setCropParam(cropParam);

Configure cropping callbacks

Configure callbacks such as the cropping completion callback, cropping progress callback, and cropping failure callback. For more information about the parameters that are used in the code, see Related classes.

Configure callbacks.
cropper.setCropCallback(new CropCallback() {
    @Override
    public void onProgress(int i) {
      // The cropping progress.
    }
      

    @Override
    public void onError(int i) {
      // The error code.
        Log.i("TestCrop", "testCrop,onError:" + i);
    }

    @Override
    public void onComplete(long executeTime) {
      // The cropping is complete.
    }

    @Override
    public void onCancelComplete() {
    }
});

Start cropping

For more information about the parameters that are used in the code, see Related classes.

Start cropping.
cropper.startCrop();

Release resources

After cropping is complete, you need to destroy the instance and release the resources. For more information about the parameters that are used in the code, see Related classes.

Release resources.
cropper.dispose();

Sample code for video cropping

// 1. Create an instance.
AliyunICrop aliyunICrop = AliyunCropCreator.createCropInstance(context);
CropParam cropParam = new CropParam();
// 2. Configure cropping parameters.
// The OutputWidth, OutputHeight, InputPath, and OutputPath parameters are required.
int outputWidth = 720;
int outputHeight = 1080;
cropParam.setOutputWidth(outputWidth);
cropParam.setOutputHeight(outputHeight);
cropParam.setOutputPath("/storage/emulated/0/DCIM/Camera/test.mp4");
cropParam.setInputPath("/storage/emulated/0/DCIM/Camera/lesson-01.mp4");
// You can configure the following parameters based on your business requirements:
// Specify the type of the media file. Default value: ANY_VIDEO_TYPE.
cropParam.setMediaType(MediaType.ANY_VIDEO_TYPE);
// Specify the cropping rectangle.
int startCropPosX = 0;
int startCropPoxY = 0;
Rect cropRect = new Rect(startCropPosX, startCropPoxY, startCropPosX + outputWidth,
    startCropPoxY + outputHeight);
cropParam.setCropRect(cropRect);
// Specify the cropping mode. The value VideoDisplayMode.SCALE specifies the scaling mode. The value VideoDisplayMode.FILL specifies the padding mode.
cropParam.setScaleMode(VideoDisplayMode.SCALE);
// Specify the frame rate.
cropParam.setFrameRate(30);
//gop
cropParam.setGop(5);
// Specify the video quality.
cropParam.setQuality(VideoQuality.HD);
// Specify the video encoding format.
cropParam.setVideoCodec(VideoCodecs.H264_HARDWARE);
// Specify the fill color.
cropParam.setFillColor(Color.BLACK);
aliyunICrop.setCropParam(cropParam);
// 3. Configure cropping callbacks.
aliyunICrop.setCropCallback(new CropCallback() {
    @Override
    public void onProgress(int i) {
    }

    @Override
    public void onError(int i) {
        Log.i("TestCrop", "testCrop,onError:" + i);
    }

    @Override
    public void onComplete(long l) {
        Log.i("TestCrop", "onComplete:" + l);
        Toast.makeText(context, "The cropping is complete. Consumed time:" + l, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancelComplete() {
    }
});
// 4. Start cropping.
aliyunICrop.startCrop();