Video Cutter Library , Android - sagarnayak/VideoCutter GitHub Wiki

Name - VideoCutterLibrary for Android (V1.1)

Author - Sagar kumar nayak

Date - 16/8/2016

Introduction

This is a library that can be just integrated to your android application to give it a functionality of a video cutter. you can either record a video or choose a video from the gallery of the android device. after that you can trim the video by this library. you will get the uri of the video at the end, and you can do whatever you like with the trimmed video. as simple as that.

How to Use

  • Importing the library to your android app-

    1. if you are downloading this library project then add the k4l and lib modules as lib in your android application. and add these as compiling lib to your android application gradle.

    2. if you want to just add the dependency to your application. follow these steps:

      add this your project level gradle-

       allprojects {
         repositories {
       ...
       maven { url "https://jitpack.io" }
         }
       }
      

      add this to your application level gradle-

       dependencies {
       compile 'com.github.sagarnayak:VideoCutter:1.1'
       }
      
  • when you want to trim a video in your android application you have three options:

    1. give options to user to choose from record a video or choose from gallery.
    2. directly go to gallery to choose a video.
    3. directly go to record a video.
    4. internally pass a uri to the library to trim the video.
  • The default max video duration can be trimmed is 10 seconds. you can change it in your application.

  • To start the library start the activity a startActivityForResult(). and below is a format.

      startActivityForResult(new Intent(MainActivity.this, RecordAndSave.class)
           .putExtra(Utils.operation_type, Utils.PERFORM_CHOOSE_FROM_GALLERY_OR_RECORD_VIDEO)
           .putExtra(Utils.MAX_DURATION, 20)
           ,Utils.REQUEST_CODE_PEFORM_VIDEO_TRIM);
    
    1. if you want to show options to user to choose from gallery and record a video set operation type as Utils.PERFORM_CHOOSE_FROM_GALLERY_OR_RECORD_VIDEO.
    2. if you want to directly go to record a video set the operation type to Utils.PERFORM_CHOOSE_RECORD_VIDEO.
    3. if you want to directly go to gallery to choose a video to trim set operation type to Utils.PERFORM_CHOOSE_FROM_GALLERY.
    4. if you want to pass a uri to the library to do the trim operation on it set the operation type to Utils.PERFORM_SEND_PATH.
  • The MainActivity is the activity you want to call the library. and the RecordAndSave is the library class. you can change the MainaActiviy as you want.

  • The request code is predefined to make the operation easy.

  • If you are choosing to pass a uri to the library for trimming you can pass the path with the intent. this compulsory in case you are choosing to pass a path. and if you are not passing a path the lib will return without any operation.

      .putExtra(Utils.FILE_PATH,"<Your file uri>")
    
  • To change the default max trimming video length

      .putExtra(Utils.MAX_DURATION,20)
    
  • In any step if any wrong input is passed the library will return null value. if you dont pass a operation type it will return null with log report. if you dont pass path where it is necessary it will return null. and you can easily read these error to know the fault. in version 1.2 new feature is added so that you can specifically know the type of the error that has happens and show the message to user accordingly.

  • Getting result in onActivityResult()

    1. you get result as RESULT_OK if the trimming operation is done correctly.

    2. if any kind of problem happens during the operation you will get a result code as RESULT_CANCELLED with a proper error message.

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
             if (requestCode == Utils.REQUEST_CODE_PEFORM_VIDEO_TRIM) {  // request code is predefined
                                                                         // for ease of operation.
                 if (resultCode == Activity.RESULT_OK) {
                     // the result is okay and you have a uri of the result;
                     data.getStringExtra(Utils.REPORT); // this is the uri;
                     data.getIntExtra(Utils.REPORT_CODE);//this is the error code or success code to check for
                                                         //specific report code if you want.
                 } else {
                     // if your application has failed to trim the video 
                     // at any point it will return result as cancelled.
                     // with a meaningful report.
                     data.getStringExtra(Utils.REPORT);  // this is the error report.
                     data.getIntExtra(Utils.REPORT_CODE);//report code for checking specific error.
            }
          }
       }
      
  • REPORT_CODE List

        ERROR TYPE                                   CODE      REMARK
        REPORT_CODE_SUCCESS                          100       when the result is okay.
        REPORT_CODE_FILE_PATH_NOT_VALID              101       when a file path is passed but 
                                                               this is not valid for a video.
        REPORT_CODE_NO_FILE_PATH_FOUND               102       if no file path is passed to the library
                                                               when it is required.
        REPORT_CODE_INVALID_OPERATION_TYPE           103       if operation type that is passed is not valid
                                                               or no operation type is passed.
        REPORT_CODE_DIALOG_CANCELLED                 104       option dialog is cancelled.
        REPORT_CODE_OUTPUT_FILE_CAN_NOT_BE_READ      105       the saved recorded video cant be read.
        REPORT_CODE_INAPPROPRIATE_SELECTION          106       wrong type of file is chosen for trimming.
        REPORT_CODE_TRIMMING_CANCELLED               107       cancel button is pressed at trimming.
        REPORT_CODE_ERROR_DURING_TRIMMING_VIDEO      108       error during trimming the video.
    

Credits - video cutter library (k4l video trimmer)