All Products
Search
Document Center

Import for cropping

Last Updated: Aug 01, 2019

Overview

The cropping module of the Alibaba Cloud short video SDK provides the video cropping feature. You can crop video clips to adjust the duration of videos. You can also adjust the frame rate, bitrate, resolution, or aspect ratio of videos.

Integrate the cropping feature

Tips: The cropping feature is not distinguished among the basic, standard, and professional editions of the short video SDK.

  • Create a sample project AliyunCropDemo. undefined

Step 1: Import files

  • Import the selected library folder and Gradle-related folders into the sample project. Copy files for SmartVideo cropping

  • Modules

    | Name | Description | Required | | ------------------- | ------------------- | ---- | | AlivcCore | The core components of the short video SDK. | Yes | | AliyunCrop | The module that contains the code used to implement the cropping UI. | Yes | | AlivcMedia | The media library module. | Yes | | AliyunVideoSdk | The core editing features of the short video SDK. | Yes | | AliyunSVideoBase | The basic package of the short video SDK. | Yes | | AliyunVideoCommon | The public module. | Yes | | thirdparty-lib | The module that contains dependent third-party libraries required by the project. | Yes |

  • Copy SO files from the app/src/main/jniLibs directory to the specified library folder of your project.

Tips: Currently, the short video SDK provides a library folder for each of the following two CPU architectures: armeabi-v7a and arm64-v8a.

Name Description Required
libfdk-aac.so The dependent third-party audio library of the short video SDK. Yes
liblive-openh264.so The dependent third-party library of the short video SDK. Yes
libQuCore.so The core library of the short video SDK. Yes
libsvideo_alivcffmpeg.so The dependent third-party library of the short video SDK. Yes
  • If you integrate other functional modules of the short video SDK, the same folders and library folder also apply to these modules.

Step 2: Import packages and build.gradle configuration

  • Add the following reference to the settings.gradle file of your project:
include ':AlivcCore' // The core components of the short video SDK.
include ':AliyunSVideoBase' // The basic package of the short video SDK.
include ':AliyunVideoSdk' // The core editing features of the short video SDK.
include ':AliyunCrop:crop_demo' // The module that contains the code used to implement the cropping UI.
include ':AliyunVideoCommon' // The public module, which contains some utility classes.
include ':AlivcMedia' // The media library module.
  • Add the native development kit (NDK) and dependency configuration to the build.gradle file of your project so that you can initialize and use the short video SDK.
defaultConfig {
        //...
        ndk {
            abiFilters "armeabi-v7a","arm64-v8a"
            // Currently, the short video SDK does not support the x86 architecture.
        }
    }

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //...

    // The dependent libraries of the cropping module.
    implementation project(':AliyunCrop:crop_demo')
}
  • Import the configuration in the thirdparty-lib/config.gradle directory into the build.gradle file of your project.
buildscript {
        //...
        apply from: 'thirdparty-lib/config.gradle'

        dependencies {
            classpath '...'

    }
}

Step 3: Add permissions

Add the following permissions to the AndroidManifest.xml file (skip this step if permissions are added):

<! -- Adds the permission to write data to the SD card. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <! -- Adds the permission to read data from the SD card. -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <! -- Adds the network permission required by the SDK license. -->
    <uses-permission android:name="android.permission.INTERNET"/>

You need to apply for a dynamic permission for Android 6.0 and later versions.

Step 4: Use the feature

  • Add a button to the sample project. undefined

  • Add a click event to the MainActivity class. undefined

    Tips: The cropping feature needs the storage permissions. To guarantee that you can use this feature properly on devices in Android 6.0 or later versions, you need to apply for a dynamic permission to handle exceptions such as the black screen.

  • Use the default parameter settings to access the cropping UI.

AliyunSnapVideoParam mCropParam = new AliyunSnapVideoParam.Builder().build();
    AliyunVideoCropActivity.startCropForResult(MainActivity.this,REQUEST_CROP,mCropParam);
  • Set cropping parameters. The complete sample code is as follows:
AliyunSnapVideoParam mCropParam = new AliyunSnapVideoParam.Builder()
                    .setFrameRate(frameRate) // The frame rate.
                    .setGop(gop) // The keyframe interval.
                    .setCropMode(cropMode) // The cropping mode.
                    .setVideoQuality(videoQuality) // The video quality.
                    .setResolutionMode(resolutionMode) // The resolution.
                    .setRatioMode(ratioMode) // The aspect ratio.
                    .setSortMode(AliyunSnapVideoParam.SORT_MODE_VIDEO) // The display sorting mode. Valid values: SORT_MODE_VIDEO (indicating videos), SORT_MODE_PHOTO (indicating images), and SORT_MODE_MERGE (indicating both images and videos).
                    .setNeedRecord(false)
                    .setCropUseGPU(false) // Indicates whether to use the GPU. The GPU is disabled by default.
                    //.setVideoBitrate() // The bitrate.
                    .build();
            AliyunVideoCropActivity.startCropForResult(this,REQUEST_CROP,mCropParam);
  • Call the onActivityResult method to obtain the path of the cropped video.
@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CROP){
            if(resultCode == Activity.RESULT_OK && data! = null){
                int type = data.getIntExtra(MediaActivity.RESULT_TYPE,0);
                if(type ==  MediaActivity.RESULT_TYPE_CROP){
                    String path = data.getStringExtra(CropKey.RESULT_KEY_CROP_PATH);
                    Toast.makeText(this,
                        "File path"+ path + "Duration" + data.getLongExtra(CropKey.RESULT_KEY_DURATION,0),
                        Toast.LENGTH_SHORT).show();
                }
            }else if(resultCode == Activity.RESULT_CANCELED){
                Toast.makeText(this,"Cropping canceled by the user",Toast.LENGTH_SHORT).show();
            }
        }
    }
  • Configure the UI.

Specify the theme of the cropping activity to configure the UI. The following sample code uses the basic edition as an example, where the recording UI and cropping UI share a style. The theme is optional. If you do not specify a theme, the default theme is used. The sample code is as follows:

<style name="AliyunVideoUIStytle" >
        <item name="qusnap_background_color">@color/color_bg</item> // The theme color of the background.
        <item name="qusnap_tint_color">@color/tint_color</item> // The color of the recording progress bar.
        <item name="qusnap_timeline_backgound_color">@color/timeline_backgound_color</item> // The background color of the recording progress bar.
        <item name="qusnap_timeline_del_backgound_color">@color/timeline_background_del_color</item> // The color used to identify the position of a recorded video clip on the progress bar when the video clip is deleted.
        <item name="qusnap_back_icon">@mipmap/icon_back</item> // The back icon.
        <item name="qusnap_switch_light_icon">@drawable/snap_switch_light_selector</item> // The flash selector.
        <item name="qusnap_switch_light_icon_disable">@mipmap/icon_light_dis</item> // The icon indicating that the flash is disabled.
        <item name="qusnap_switch_light_icon_visibility">visible</item> // The icon indicating whether to display or hide the flash.
        <item name="qusnap_switch_camera_icon">@drawable/snap_switch_camera</item> // The camera selector.
        <item name="qusnap_switch_camera_icon_visibility">visible</item> // The icon indicating whether to display or hide the camera.
        <item name="qusnap_beauty_icon">@drawable/snap_switch_beauty</item> // The face filter selector. 
        <item name="qusnap_beauty_icon_visibility">visible</item> // The icon indicating whether to display or hide the face filter.
        <item name="qusnap_record_icon">@drawable/snap_record_state_selector</item> // The recording selector.
        <item name="qusnap_delete_icon">@drawable/snap_icon_delete</item> // The deletion selector.
        <item name="qusnap_complete_icon">@drawable/snap_icon_complete</item> // The completion selector.
        <item name="qusnap_gallery_icon">@mipmap/icon_default</item> // The album icon.
        <item name="qusnap_gallery_icon_visibility">visible</item> // The icon indicating whether to display or hide the album.
        <item name="qusnap_time_txt_color">@android:color/white</item> // The color of the text describing the recording time.
        <item name="qusnap_time_txt_size">15dp</item> // The size of the text describing the recording time.
        <item name="qusnap_time_txt_visibility">visible</item> // The icon indicating whether to display or hide the text describing the recording time.
        <item name="qusnap_time_line_pos_y">0dp</item> // The upward offset of the recording progress bar.
        <item name="qusnap_crop_sweep_left">@mipmap/icon_sweep_left</item> // The icon indicating the left border (start point) of the cropping slider.
        <item name="qusnap_crop_sweep_right">@mipmap/icon_sweep_right</item> // The icon indicating the right border (end point) of the cropping slider.
        <item name="qusnap_crop_seek_frame">@mipmap/icon_frame</item> // The icon indicating the position of the playhead on the cropped video.
        <item name="qusnap_crop_seek_padding_color">@android:color/holo_red_dark</item> // The color of the top and bottom borders of the cropping slider.
        <item name="qusnap_crop_icon_transform">@drawable/snap_transform_selector</item> // The switching selector of the cropping mode.
        <item name="qusnap_crop_icon_transform_visibility">visible</item> // The icon indicating whether to display or hide the cropping mode.
        <item name="qusnap_crop_time_txt_color">@android:color/white</item> // The color of the text describing the cropped video duration.  
        <item name="qusnap_crop_time_txt_size">15dp</item> // The size of the text describing the cropped video duration.
        <item name="qusnap_crop_txt_visibility">visible</item> // The icon indicating whether to display or hide the text describing the cropped video duration.
</style>
  • Register a redirect activity after cropping. If you need to redirect users to a custom page after cropping, register a redirect activity in the AliyunSvideoActionConfig class.
// Returns to the previous page by default after cropping.
AliyunSvideoActionConfig.getInstance().registerCropFinishActivity(null);