1.2.1 • Published 5 years ago

react-native-sili-video-compression v1.2.1

Weekly downloads
34
License
MIT
Repository
github
Last release
5 years ago

react-native-video-compression

Getting started

$ npm install react-native-sili-video-compression --save

Mostly automatic installation

$ react-native link react-native-sili-video-compression

Manual installation

  1. Open the package.json file and under dependencies ➜ Add "react-native-sili-video-compression" : 1.0.6

  2. You should add a comma before the line to keep the JSON format of it.

  3. Open the terminal in the project ➜ Type npm install.

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-video-compression and add RNVideoCompression.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNVideoCompression.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

Usage

Import Library

import CompressModule from 'react-native-sili-video-compression';

Add External File read write permission into your Manifest.xml.

Add xmlns:tools="http://schemas.android.com/tools" to your <Manifest/> tag in your Manifest.xml.

Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  ...
  ...>

Add these 2 line to your <application/> tag.

<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      ...
      android:requestLegacyExternalStorage="true"   //Add this
      tools:replace="android:allowBackup"           //Add this
      />

Add Event Emitter for progress update

const EVENT_NAME = new NativeEventEmitter(NativeModules.CompressModule);
    this.subscription = EVENT_NAME.addListener(
                             CompressModule.VIDEO_COMPRESS_PROGRESS_EMITTER,
                             (value)=> console.log(value));

Add success listener

var successCall = (fileName, size, path, message) => {
      // Do your work
      console.log(message);
    };

Success method properties

PropertyTypeDescription
fileNameStringFile name with absolute path : '/storage/emulated/0/Download/20201003_095653.mp4'
sizeLongSize in Bytes
pathStringAbsolute file path of compressed file : '/storage/emulated/0/Download/20201003_095653.mp4'
messageStringCompress comprete message

Add Failure listener

var errorCall = (errorMessage) => { 
      // Do your work
      console.log(errorMessage);
    };

Failure method properties

PropertyTypeDescription
errorMessageStringError message with reason

Compression methode for predefined levels

//Check for External file access permission before video conversion
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      CompressModule.compressVideo('/storage/emulated/0/Download/20201003_095653.mp4', CompressModule.LOW,
      errorCall, successCall);
      // console.log(CompressModule.LOW);
    }

compressVideo() request Params

PropertyTypeDescription
inputFilePathStringAbsolute path for input file : '/storage/emulated/0/Download/20201003_095653.mp4'
compressLevelConstantLOW/MEDIUM/HIGH
errorCallbackCallBackErrorCallback object to handle error events
successCallbackCallBackSuccessCallback object to handle error events

Predefined 3 level of compress can be done.

ConstantDescription
CompressModule.LOWcompress level 10% and reduce Height and Width to 50%
CompressModule.MEDIUMcompress level 50% and reduce Height and Width to 50%
CompressModule.HIGHcompress level 100% and reduce Height and Width to 50%

Compression methode by using percentage

//Check for External file access permission before video conversion
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      CompressModule.compressVideoWithQuality('/storage/emulated/0/Download/20201003_095653.mp4', reduceVideoQualityToPercent, reduceHeightWidthToPercent, 
      errorCall, successCall);
      // console.log(CompressModule.LOW);
    }

compressVideoWithQuality() request Params

PropertyTypeDescription
inputFilePathStringAbsolute path for input file : '/storage/emulated/0/Download/20201003_095653.mp4'
reduceVideoQualityToPercentFloatvalue should be in between 0 to 1, default value is 0.5
reduceHeightWidthToPercentFloatvalue should be in between 0 to 1, default value is 0.5
errorCallbackCallBackErrorCallback object to handle error events
successCallbackCallBackSuccessCallback object to handle error events

VERY IMPORTANT FOR ANDROID Close the Emitter subscription if not using it or in task completion or task failed

this.subscription.remove();

License

MIT

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.1

5 years ago

1.1.2

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago