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.

Note: You need to create an AliyunVideo folder in your project to store all source code copied from the demo. This AliyunVideo folder can help you troubleshoot, update, and maintain the code in the future.

Step 1: Copy SDK files

The following table lists the two files that the cropping module is dependent on.

Name Type Description
AliyunVideoSDKPro.framework DLL The short video SDK, which is dependent on the alivcffmpeg library.
alivcffmpeg.framework DLL The ffmepg library.
  • Copy the files highlighted in red into your project, as shown in the following figure.


 undefined

Step 2: Copy business code files

  • Copy the files highlighted in red into your project, as shown in the following figure.


 undefined

  • Delete unnecessary files.


 undefined

Step 3: Configure the PCH file

Add the following reference to a .pch file in your project:

  1. #import "AlivcDefine.h"
  2. #import "AlivcMacro.h"

If your project does not contain a .pch file, you can copy the PrefixHeader.pch file from the demo. Keep the preceding reference in the .pch file.

  • Choose TARGETS > Build Settings to configure the PCH file, as shown in the following figure.


 WX20181020-002351@2x.png
Prefix Header: Enter the directory of the PrefixHeader.pch file in your project, for example, $(SRCROOT)/Your own directory/PrefixHeader.pch.

Step 4: Set up the project environment

  • Open the project and select the target. Choose General > Embedded Binaries to import files, as shown in the following figure.


 undefined

  • Press and hold command on the keyboard to select the files to be imported and click Add, as shown in the following figures.
     undefined  undefined
  • Open the project and select the target. Choose General > Linked Frameworks and Libraries. Click + to add the following system libraries:

    1. libz.tbd
    2. ImageIO.framework
    3. CoreMedia.framework
    4. CoreVideo.framework
    5. VideoToolBox.framework
    6. MediaPlayer.framework
    7. OpenAL.framework
    8. libc++.tbd
    9. libsqlite3.tbd
    10. libiconv.tbd


    undefined

  • Choose Build Settings > Linking > Other Linker Flags. Add the $(inherited) and -ObjC options.


 undefined

  • Choose Build Settings > Build Options. Set Enable Bitcode to No.undefined

  • Edit the Info.plist file and add the following fields to apply for the camera, album, and music access permissions:

    1. * Privacy - Photo Library Additions Usage Description
    2. * Privacy - Photo Library Usage Description

    You can also add the following code to the source code of the Info.plist file:

    1. <key>NSPhotoLibraryAddUsageDescription</key>
    2. <string></string>
    3. <key>NSPhotoLibraryUsageDescription</key>
    4. <string></string>

Step 5: Integrate CocoaPods

  • Add the following reference to a Podfile:

    1. pod 'MBProgressHUD', '~> 1.1.0'
    2. pod 'AlivcConan', '0.9.0' # The dependent data tracking of the short video SDK.
  • On a physical device where your project is run, run the cd command to go to the directory of the Podfile and run the pod install command.

Step 6: Compile the sample code for the cropping feature

  • Import header files.

    1. #import "AliyunPhotoViewController.h"
    2. #import "AliyunIConfig.h"
  • Specify a view controller delegate.

    1. @interface ViewController ()<AliyunPhotoViewControllerDelegate>
  • Compile the navigation code.

  1. // Initializes the cropping UI configuration.
  2. [AliyunIConfig setConfig:[[AliyunIConfig alloc] init]];
  3. AliyunMediaConfig *mediaConfig = [AliyunMediaConfig defaultConfig];
  4. // You can set the following parameters as required.
  5. // The minimum duration.
  6. mediaConfig.minDuration = 2.0;
  7. // The maximum duration.
  8. mediaConfig.maxDuration = 10.0*60;
  9. // The frame rate.
  10. mediaConfig.fps = 25;
  11. // The keyframe interval.
  12. mediaConfig.gop = 5;
  13. // The padding mode.
  14. mediaConfig.cutMode = AliyunMediaCutModeScaleAspectFill;
  15. // Indicates whether the video selection page displays only videos.
  16. mediaConfig.videoOnly = NO;
  17. // The background color.
  18. mediaConfig.backgroundColor = [UIColor blackColor];
  19. // For more information about parameter settings, see the AliyunMediaConfig.h file.
  20. // Initializes the album picker.
  21. AliyunPhotoViewController *control = [[AliyunPhotoViewController alloc]init];
  22. // Sets cropping parameters.
  23. [control setCutInfo:mediaConfig];
  24. control.delegate = self;
  25. #Warning: If you fail to be redirected to the custom page, check whether UINavigationController is configured in your code.
  26. // Pushes the view controller.
  27. if (self.navigationController) {
  28. [self.navigationController pushViewController:control animated:YES];
  29. }else{
  30. NSLog(@"----------------------------------------");
  31. NSLog(@"Check whether UINavigationController is configured.") ;
  32. NSLog(@"Check whether UINavigationController is configured.") ;
  33. NSLog(@"Check whether UINavigationController is configured.") ;
  34. NSLog(@"----------------------------------------");
  35. }
  • Implement the view controller delegate.

    1. -(void)cropFinished:(UIViewController *)cropViewController videoPath:(NSString *)videoPath sourcePath:(NSString *)sourcePath{
    2. NSLog(@"Storage path of the cropped video: %@",videoPath);
    3. // Performs other operations required after short video processing.
    4. // For example, returns to the homepage.
    5. [self.navigationController popToRootViewControllerAnimated:YES];
    6. }
    7. - (void)cropFinished:(UIViewController *)cropViewController mediaType:(kPhotoMediaType)type photo:(UIImage *)photo videoPath:(NSString *)videoPath{
    8. NSLog(@"Trigger a callback event after cropping.");
    9. // Performs other operations required after short video processing.
    10. // For example, returns to the homepage.
    11. [self.navigationController popToRootViewControllerAnimated:YES];
    12. }
    13. - (void)backBtnClick:(UIViewController *)vc {
    14. NSLog(@"Click Cancel.");
    15. [self.navigationController popToRootViewControllerAnimated:YES];
    16. }
  • Provide the cropped video.

    After video cropping, trigger the previously registered delegate event.

    1. - (void)cropFinished:(UIViewController *)cropViewController videoPath:(NSString *)videoPath sourcePath:(NSString *)sourcePath{
    2. NSLog(@"Storage path of the cropped video: %@",videoPath);
    3. // Performs other operations required after short video processing.
    4. // For example, returns to the homepage.
    5. [self.navigationController popToRootViewControllerAnimated:YES];
    6. }
    7. - (void)cropFinished:(UIViewController *)cropViewController mediaType:(kPhotoMediaType)type photo:(UIImage *)photo videoPath:(NSString *)videoPath{
    8. NSLog(@"Trigger a callback event after cropping.");
    9. // Performs other operations required after short video processing.
    10. // For example, returns to the homepage.
    11. [self.navigationController popToRootViewControllerAnimated:YES];
    12. }