react-native-compressor 0.5.13 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +39 -43
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +6 -0
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java +24 -58
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +6 -42
- package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +19 -17
- package/ios/Compressor.m +2 -1
- package/ios/Video/VideoCompressor.swift +24 -16
- package/lib/commonjs/Video/index.js +45 -30
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/module/Video/index.js +38 -29
- package/lib/module/Video/index.js.map +1 -1
- package/lib/typescript/Video/index.d.ts +4 -0
- package/package.json +1 -1
- package/react-native-compressor.podspec +1 -1
- package/src/Video/index.tsx +48 -36
package/android/build.gradle
CHANGED
|
@@ -1,65 +1,61 @@
|
|
|
1
1
|
buildscript {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
if (project == rootProject) {
|
|
3
|
+
repositories {
|
|
4
|
+
google()
|
|
5
|
+
mavenCentral()
|
|
6
|
+
}
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
8
|
+
dependencies {
|
|
9
|
+
classpath 'com.android.tools.build:gradle:3.5.3'
|
|
11
10
|
}
|
|
11
|
+
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
apply plugin: 'com.android.library'
|
|
15
15
|
|
|
16
16
|
def safeExtGet(prop, fallback) {
|
|
17
|
-
|
|
17
|
+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
android {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
lintOptions {
|
|
37
|
-
disable 'GradleCompatible'
|
|
38
|
-
}
|
|
39
|
-
compileOptions {
|
|
40
|
-
sourceCompatibility JavaVersion.VERSION_1_8
|
|
41
|
-
targetCompatibility JavaVersion.VERSION_1_8
|
|
21
|
+
compileSdkVersion safeExtGet('Compressor_compileSdkVersion', 29)
|
|
22
|
+
buildToolsVersion safeExtGet('Compressor_buildToolsVersion', '29.0.2')
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion safeExtGet('Compressor_minSdkVersion', 16)
|
|
25
|
+
targetSdkVersion safeExtGet('Compressor_targetSdkVersion', 29)
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
buildTypes {
|
|
32
|
+
release {
|
|
33
|
+
minifyEnabled false
|
|
42
34
|
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
disable 'GradleCompatible'
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
42
|
+
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
repositories {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
google()
|
|
47
|
+
mavenLocal()
|
|
48
|
+
maven {
|
|
49
|
+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
|
|
50
|
+
url("$rootDir/../node_modules/react-native/android")
|
|
51
|
+
}
|
|
52
|
+
mavenCentral()
|
|
53
|
+
maven { url "https://www.jitpack.io" }
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
dependencies {
|
|
57
|
-
|
|
57
|
+
//noinspection GradleDynamicVersion
|
|
58
58
|
implementation "com.facebook.react:react-native:+" // From node_modules
|
|
59
|
-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0"
|
|
60
|
-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.0"
|
|
61
|
-
implementation "com.googlecode.mp4parser:isoparser:1.0.6"
|
|
62
|
-
implementation 'com.github.zolad:VideoSlimmer:master-SNAPSHOT'
|
|
63
59
|
implementation 'io.github.lizhangqu:coreprogress:1.0.2'
|
|
64
60
|
implementation 'com.github.nomi9995:VideoCompressor:43ec740572'
|
|
65
61
|
// implementation project(path: ':videocompressor')
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
package com.reactnativecompressor.Utils;
|
|
2
2
|
|
|
3
|
+
import androidx.annotation.Nullable;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.bridge.Arguments;
|
|
6
|
+
import com.facebook.react.bridge.Promise;
|
|
3
7
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
4
8
|
import com.facebook.react.bridge.ReactContext;
|
|
5
9
|
import com.facebook.react.bridge.WritableMap;
|
|
@@ -8,6 +12,8 @@ import numan.dev.videocompressor.VideoCompressTask;
|
|
|
8
12
|
import numan.dev.videocompressor.VideoCompressor;
|
|
9
13
|
|
|
10
14
|
import java.io.File;
|
|
15
|
+
import java.util.HashMap;
|
|
16
|
+
import java.util.Map;
|
|
11
17
|
import java.util.UUID;
|
|
12
18
|
|
|
13
19
|
public class Utils {
|
|
@@ -1,94 +1,60 @@
|
|
|
1
1
|
package com.reactnativecompressor.Video.AutoVideoCompression;
|
|
2
2
|
|
|
3
|
-
import android.media.MediaCodecInfo;
|
|
4
3
|
import android.media.MediaMetadataRetriever;
|
|
5
|
-
import android.media.session.MediaController;
|
|
6
4
|
import android.net.Uri;
|
|
7
|
-
import android.os.Build;
|
|
8
5
|
|
|
9
6
|
import androidx.annotation.Nullable;
|
|
10
7
|
|
|
11
|
-
import com.facebook.react.bridge.Arguments;
|
|
12
8
|
import com.facebook.react.bridge.Promise;
|
|
13
9
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
14
10
|
import com.facebook.react.bridge.ReactContext;
|
|
15
11
|
import com.facebook.react.bridge.WritableMap;
|
|
16
12
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
17
13
|
import com.reactnativecompressor.Video.VideoCompressorHelper;
|
|
18
|
-
import com.
|
|
14
|
+
import static com.reactnativecompressor.Utils.Utils.compressVideo;
|
|
19
15
|
|
|
20
16
|
import java.io.File;
|
|
21
17
|
|
|
22
18
|
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
23
19
|
|
|
24
20
|
public class AutoVideoCompression {
|
|
25
|
-
static int videoCompressionThreshold=10;
|
|
26
|
-
static int currentVideoCompression=0;
|
|
27
|
-
|
|
28
21
|
public static void createCompressionSettings(String fileUrl,VideoCompressorHelper options,Promise promise, ReactApplicationContext reactContext) {
|
|
29
22
|
float maxSize = options.maxSize;
|
|
30
23
|
float minimumFileSizeForCompress=options.minimumFileSizeForCompress;
|
|
31
24
|
try{
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
float scale = actualWidth > actualHeight ? maxSize / actualWidth : maxSize / actualHeight;
|
|
47
|
-
int resultWidth = Math.round(actualWidth * scale / 2) * 2;
|
|
48
|
-
int resultHeight = Math.round(actualHeight * scale / 2) * 2;
|
|
49
|
-
|
|
50
|
-
float videoBitRate = makeVideoBitrate(
|
|
51
|
-
actualHeight, actualWidth,
|
|
52
|
-
bitrate,
|
|
53
|
-
resultHeight, resultWidth
|
|
54
|
-
);
|
|
25
|
+
Uri uri= Uri.parse(fileUrl);
|
|
26
|
+
String srcPath = uri.getPath();
|
|
27
|
+
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
|
|
28
|
+
metaRetriever.setDataSource(srcPath);
|
|
29
|
+
File file=new File(srcPath);
|
|
30
|
+
float sizeInBytes = file.length();
|
|
31
|
+
float sizeInMb = sizeInBytes / (1024 * 1024);
|
|
32
|
+
if(sizeInMb>minimumFileSizeForCompress)
|
|
33
|
+
{
|
|
34
|
+
String destinationPath = generateCacheFilePath("mp4", reactContext);
|
|
35
|
+
int actualHeight =Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
36
|
+
int actualWidth = Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
37
|
+
int bitrate = Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
|
|
55
38
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
//convert start
|
|
60
|
-
}
|
|
61
|
-
@Override
|
|
62
|
-
public void onFinish(boolean result) {
|
|
63
|
-
//convert finish,result(true is success,false is fail)
|
|
64
|
-
promise.resolve("file:/"+destinationPath);
|
|
65
|
-
}
|
|
66
|
-
@Override
|
|
67
|
-
public void onProgress(float percent) {
|
|
68
|
-
int roundProgress=Math.round(percent);
|
|
69
|
-
if(roundProgress%videoCompressionThreshold==0&&roundProgress>currentVideoCompression) {
|
|
70
|
-
WritableMap params = Arguments.createMap();
|
|
71
|
-
WritableMap data = Arguments.createMap();
|
|
72
|
-
params.putString("uuid", options.uuid);
|
|
73
|
-
data.putDouble("progress", percent / 100);
|
|
74
|
-
params.putMap("data", data);
|
|
75
|
-
sendEvent(reactContext, "videoCompressProgress", params);
|
|
76
|
-
currentVideoCompression=roundProgress;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
});
|
|
39
|
+
float scale = actualWidth > actualHeight ? maxSize / actualWidth : maxSize / actualHeight;
|
|
40
|
+
int resultWidth = Math.round(actualWidth * scale / 2) * 2;
|
|
41
|
+
int resultHeight = Math.round(actualHeight * scale / 2) * 2;
|
|
80
42
|
|
|
43
|
+
float videoBitRate = makeVideoBitrate(
|
|
44
|
+
actualHeight, actualWidth,
|
|
45
|
+
bitrate,
|
|
46
|
+
resultHeight, resultWidth
|
|
47
|
+
);
|
|
48
|
+
compressVideo(srcPath, destinationPath, resultWidth, resultHeight, videoBitRate,options.uuid,promise,reactContext);
|
|
81
49
|
}
|
|
82
50
|
else
|
|
83
51
|
{
|
|
84
52
|
promise.resolve(fileUrl);
|
|
85
53
|
}
|
|
54
|
+
|
|
86
55
|
} catch (Exception ex) {
|
|
87
56
|
promise.reject(ex);
|
|
88
57
|
}
|
|
89
|
-
finally {
|
|
90
|
-
currentVideoCompression=0;
|
|
91
|
-
}
|
|
92
58
|
}
|
|
93
59
|
|
|
94
60
|
public static int makeVideoBitrate(int originalHeight, int originalWidth, int originalBitrate, int height, int width) {
|
|
@@ -8,7 +8,6 @@ import android.os.PowerManager;
|
|
|
8
8
|
|
|
9
9
|
import androidx.annotation.Nullable;
|
|
10
10
|
|
|
11
|
-
import com.facebook.react.bridge.Arguments;
|
|
12
11
|
import com.facebook.react.bridge.LifecycleEventListener;
|
|
13
12
|
import com.facebook.react.bridge.Promise;
|
|
14
13
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
@@ -17,13 +16,12 @@ import com.facebook.react.bridge.ReadableMap;
|
|
|
17
16
|
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
18
17
|
import com.facebook.react.bridge.WritableMap;
|
|
19
18
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
20
|
-
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
21
19
|
import com.reactnativecompressor.Utils.FileUplaoder.FileUploader;
|
|
22
20
|
import com.reactnativecompressor.Video.AutoVideoCompression.AutoVideoCompression;
|
|
23
|
-
import com.zolad.videoslimmer.VideoSlimmer;
|
|
24
21
|
|
|
25
22
|
import java.util.UUID;
|
|
26
23
|
|
|
24
|
+
import static com.reactnativecompressor.Utils.Utils.compressVideo;
|
|
27
25
|
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
28
26
|
|
|
29
27
|
public class VideoCompressorHelper {
|
|
@@ -50,8 +48,8 @@ public class VideoCompressorHelper {
|
|
|
50
48
|
};
|
|
51
49
|
|
|
52
50
|
private static void sendEventString(ReactContext reactContext,
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
String eventName,
|
|
52
|
+
@Nullable String params) {
|
|
55
53
|
reactContext
|
|
56
54
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
57
55
|
.emit(eventName, params);
|
|
@@ -118,7 +116,7 @@ public class VideoCompressorHelper {
|
|
|
118
116
|
case "maxSize":
|
|
119
117
|
options.maxSize = (float) map.getDouble(key);
|
|
120
118
|
break;
|
|
121
|
-
|
|
119
|
+
case "uuid":
|
|
122
120
|
options.uuid = map.getString(key);
|
|
123
121
|
break;
|
|
124
122
|
case "minimumFileSizeForCompress":
|
|
@@ -166,44 +164,10 @@ public class VideoCompressorHelper {
|
|
|
166
164
|
}
|
|
167
165
|
}
|
|
168
166
|
float videoBitRate = (options.bitrate>0)?options.bitrate: (float) (height * width * 1.5);
|
|
169
|
-
|
|
170
|
-
VideoSlimmer.convertVideo(srcPath, destinationPath, width, height, (int) videoBitRate, new VideoSlimmer.ProgressListener() {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
@Override
|
|
174
|
-
public void onStart() {
|
|
175
|
-
//convert start
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
@Override
|
|
180
|
-
public void onFinish(boolean result) {
|
|
181
|
-
//convert finish,result(true is success,false is fail)
|
|
182
|
-
promise.resolve("file:/"+destinationPath);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
@Override
|
|
187
|
-
public void onProgress(float percent) {
|
|
188
|
-
int roundProgress=Math.round(percent);
|
|
189
|
-
if(roundProgress%videoCompressionThreshold==0&&roundProgress>currentVideoCompression) {
|
|
190
|
-
WritableMap params = Arguments.createMap();
|
|
191
|
-
WritableMap data = Arguments.createMap();
|
|
192
|
-
params.putString("uuid", options.uuid);
|
|
193
|
-
data.putDouble("progress", percent / 100);
|
|
194
|
-
params.putMap("data", data);
|
|
195
|
-
sendEvent(reactContext, "videoCompressProgress", params);
|
|
196
|
-
currentVideoCompression=roundProgress;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
|
|
167
|
+
compressVideo(srcPath, destinationPath, width, height, videoBitRate,options.uuid,promise,reactContext);
|
|
201
168
|
} catch (Exception ex) {
|
|
202
169
|
promise.reject(ex);
|
|
203
170
|
}
|
|
204
|
-
finally {
|
|
205
|
-
currentVideoCompression=0;
|
|
206
|
-
}
|
|
207
171
|
}
|
|
208
172
|
|
|
209
173
|
|
|
@@ -212,4 +176,4 @@ public class VideoCompressorHelper {
|
|
|
212
176
|
}
|
|
213
177
|
|
|
214
178
|
|
|
215
|
-
|
|
179
|
+
}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
package com.reactnativecompressor.Video;
|
|
2
2
|
|
|
3
|
-
import android.
|
|
4
|
-
import android.net.Uri;
|
|
3
|
+
import android.util.Log;
|
|
5
4
|
|
|
6
5
|
import androidx.annotation.NonNull;
|
|
7
6
|
import androidx.annotation.Nullable;
|
|
8
7
|
|
|
9
|
-
import com.facebook.react.bridge.Arguments;
|
|
10
8
|
import com.facebook.react.bridge.Promise;
|
|
11
9
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
12
10
|
import com.facebook.react.bridge.ReactContext;
|
|
@@ -16,16 +14,13 @@ import com.facebook.react.bridge.ReadableMap;
|
|
|
16
14
|
import com.facebook.react.bridge.WritableMap;
|
|
17
15
|
import com.facebook.react.module.annotations.ReactModule;
|
|
18
16
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
19
|
-
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
20
|
-
import com.zolad.videoslimmer.VideoSlimmer;
|
|
21
17
|
|
|
22
|
-
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
23
18
|
import static com.reactnativecompressor.Video.VideoCompressorHelper.video_activateBackgroundTask_helper;
|
|
24
19
|
import static com.reactnativecompressor.Video.VideoCompressorHelper.video_deactivateBackgroundTask_helper;
|
|
25
20
|
import static com.reactnativecompressor.Video.VideoCompressorHelper.video_upload_helper;
|
|
26
|
-
|
|
21
|
+
import static com.reactnativecompressor.Utils.Utils.cancelCompressionHelper;
|
|
27
22
|
@ReactModule(name = VideoModule.NAME)
|
|
28
|
-
public class
|
|
23
|
+
public class VideoModule extends ReactContextBaseJavaModule {
|
|
29
24
|
public static final String NAME = "VideoCompressor";
|
|
30
25
|
private final ReactApplicationContext reactContext;
|
|
31
26
|
public VideoModule(ReactApplicationContext reactContext) {
|
|
@@ -53,18 +48,25 @@ public class VideoModule extends ReactContextBaseJavaModule {
|
|
|
53
48
|
String fileUrl,
|
|
54
49
|
ReadableMap optionMap,
|
|
55
50
|
Promise promise) {
|
|
56
|
-
|
|
51
|
+
final VideoCompressorHelper options = VideoCompressorHelper.fromMap(optionMap);
|
|
52
|
+
|
|
53
|
+
if(options.compressionMethod==VideoCompressorHelper.CompressionMethod.auto)
|
|
54
|
+
{
|
|
55
|
+
VideoCompressorHelper.VideoCompressAuto(fileUrl,options,promise,reactContext);
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
{
|
|
59
|
+
VideoCompressorHelper.VideoCompressManual(fileUrl,options,promise,reactContext);
|
|
60
|
+
}
|
|
57
61
|
|
|
58
|
-
if(options.compressionMethod==VideoCompressorHelper.CompressionMethod.auto)
|
|
59
|
-
{
|
|
60
|
-
VideoCompressorHelper.VideoCompressAuto(fileUrl,options,promise,reactContext);
|
|
61
|
-
}
|
|
62
|
-
else
|
|
63
|
-
{
|
|
64
|
-
VideoCompressorHelper.VideoCompressManual(fileUrl,options,promise,reactContext);
|
|
65
|
-
}
|
|
66
62
|
|
|
63
|
+
}
|
|
67
64
|
|
|
65
|
+
@ReactMethod
|
|
66
|
+
public void cancelCompression(
|
|
67
|
+
String uuid) {
|
|
68
|
+
cancelCompressionHelper(uuid);
|
|
69
|
+
Log.d("cancelCompression", uuid);
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
@ReactMethod
|
package/ios/Compressor.m
CHANGED
|
@@ -34,6 +34,7 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
|
|
|
34
34
|
var hasListener: Bool=false
|
|
35
35
|
var uploadResolvers: [String: RCTPromiseResolveBlock] = [:]
|
|
36
36
|
var uploadRejectors: [String: RCTPromiseRejectBlock] = [:]
|
|
37
|
+
var compressorExports: [String: NextLevelSessionExporter] = [:]
|
|
37
38
|
let videoCompressionThreshold:Int=7
|
|
38
39
|
|
|
39
40
|
|
|
@@ -71,7 +72,7 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
|
|
|
71
72
|
@objc(compress:withOptions:withResolver:withRejecter:)
|
|
72
73
|
func compress(fileUrl: String, options: [String: Any], resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
|
|
73
74
|
compressVideo(url: URL(string: fileUrl)!, options:options,
|
|
74
|
-
onProgress: { progress in
|
|
75
|
+
onProgress: { progress in
|
|
75
76
|
print("Progress", progress)
|
|
76
77
|
if(self.hasListener){
|
|
77
78
|
self.sendEvent(withName: "videoCompressProgress", body: ["uuid": options["uuid"], "data": ["progress": progress]])
|
|
@@ -83,6 +84,11 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
|
|
|
83
84
|
})
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
@objc(cancelCompression:)
|
|
88
|
+
func cancelCompression(uuid: String) -> Void {
|
|
89
|
+
compressorExports[uuid]?.cancelExport()
|
|
90
|
+
}
|
|
91
|
+
|
|
86
92
|
func makeValidUri(filePath: String) -> String {
|
|
87
93
|
let fileWithUrl = URL(fileURLWithPath: filePath)
|
|
88
94
|
let absoluteUrl = fileWithUrl.deletingLastPathComponent()
|
|
@@ -219,7 +225,7 @@ func makeValidUri(filePath: String) -> String {
|
|
|
219
225
|
}
|
|
220
226
|
else
|
|
221
227
|
{
|
|
222
|
-
manualCompressionHelper(url: url,
|
|
228
|
+
manualCompressionHelper(url: url, options:options) { progress in
|
|
223
229
|
onProgress(progress)
|
|
224
230
|
} onCompletion: { outputURL in
|
|
225
231
|
onCompletion(outputURL)
|
|
@@ -263,6 +269,7 @@ func makeValidUri(filePath: String) -> String {
|
|
|
263
269
|
|
|
264
270
|
func autoCompressionHelper(url: URL, options: [String: Any], onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
265
271
|
let maxSize:Float = options["maxSize"] as! Float;
|
|
272
|
+
let uuid:String = options["uuid"] as! String
|
|
266
273
|
|
|
267
274
|
let asset = AVAsset(url: url)
|
|
268
275
|
guard asset.tracks.count >= 1 else {
|
|
@@ -286,8 +293,8 @@ func makeValidUri(filePath: String) -> String {
|
|
|
286
293
|
originalBitrate: Int(bitrate),
|
|
287
294
|
height: Int(resultHeight), width: Int(resultWidth)
|
|
288
295
|
);
|
|
289
|
-
|
|
290
|
-
exportVideoHelper(url: url, asset: asset, bitRate: videoBitRate, resultWidth: resultWidth, resultHeight: resultHeight) { progress in
|
|
296
|
+
|
|
297
|
+
exportVideoHelper(url: url, asset: asset, bitRate: videoBitRate, resultWidth: resultWidth, resultHeight: resultHeight,uuid: uuid) { progress in
|
|
291
298
|
onProgress(progress)
|
|
292
299
|
} onCompletion: { outputURL in
|
|
293
300
|
onCompletion(outputURL)
|
|
@@ -296,9 +303,9 @@ func makeValidUri(filePath: String) -> String {
|
|
|
296
303
|
}
|
|
297
304
|
}
|
|
298
305
|
|
|
299
|
-
func manualCompressionHelper(url: URL,
|
|
300
|
-
|
|
301
|
-
var
|
|
306
|
+
func manualCompressionHelper(url: URL, options: [String: Any], onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
307
|
+
let uuid:String = options["uuid"] as! String
|
|
308
|
+
var bitRate=options["bitrate"] as! Float?;
|
|
302
309
|
let asset = AVAsset(url: url)
|
|
303
310
|
guard asset.tracks.count >= 1 else {
|
|
304
311
|
let error = CompressionError(message: "Invalid video URL, no track found")
|
|
@@ -321,12 +328,12 @@ func makeValidUri(filePath: String) -> String {
|
|
|
321
328
|
}
|
|
322
329
|
else
|
|
323
330
|
{
|
|
324
|
-
|
|
331
|
+
bitRate=bitRate ?? Float(abs(track.estimatedDataRate))*0.8
|
|
325
332
|
}
|
|
326
333
|
|
|
327
|
-
let videoBitRate =
|
|
334
|
+
let videoBitRate = bitRate ?? height*width*1.5
|
|
328
335
|
|
|
329
|
-
exportVideoHelper(url: url, asset: asset, bitRate: Int(videoBitRate), resultWidth: width, resultHeight: height) { progress in
|
|
336
|
+
exportVideoHelper(url: url, asset: asset, bitRate: Int(videoBitRate), resultWidth: width, resultHeight: height,uuid: uuid) { progress in
|
|
330
337
|
onProgress(progress)
|
|
331
338
|
} onCompletion: { outputURL in
|
|
332
339
|
onCompletion(outputURL)
|
|
@@ -365,7 +372,7 @@ func makeValidUri(filePath: String) -> String {
|
|
|
365
372
|
AVSampleRateKey: NSNumber(value: Float(44100))
|
|
366
373
|
]
|
|
367
374
|
|
|
368
|
-
|
|
375
|
+
compressorExports[uuid] = exporter
|
|
369
376
|
exporter.export(progressHandler: { (progress) in
|
|
370
377
|
let _progress:Float=progress*100;
|
|
371
378
|
if(Int(_progress)==videoCompressionCounter)
|
|
@@ -380,14 +387,13 @@ func makeValidUri(filePath: String) -> String {
|
|
|
380
387
|
case .completed:
|
|
381
388
|
onCompletion(exporter.outputURL!)
|
|
382
389
|
break
|
|
390
|
+
case .cancelled:
|
|
391
|
+
let error = CompressionError(message: "Compression has canncelled")
|
|
392
|
+
onFailure(error)
|
|
393
|
+
break
|
|
383
394
|
default:
|
|
384
395
|
onCompletion(url)
|
|
385
396
|
break
|
|
386
|
-
}
|
|
387
|
-
break
|
|
388
|
-
case .failure(let error):
|
|
389
|
-
onCompletion(url)
|
|
390
|
-
break
|
|
391
397
|
}
|
|
392
398
|
})
|
|
393
399
|
}
|
|
@@ -405,4 +411,6 @@ func makeValidUri(filePath: String) -> String {
|
|
|
405
411
|
let track = asset.tracks[videoTrackIndex];
|
|
406
412
|
return track;
|
|
407
413
|
}
|
|
414
|
+
|
|
415
|
+
|
|
408
416
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default = void 0;
|
|
6
|
+
exports.default = exports.cancelCompression = exports.backgroundUpload = void 0;
|
|
7
7
|
|
|
8
8
|
var _reactNative = require("react-native");
|
|
9
9
|
|
|
@@ -11,6 +11,45 @@ var _utils = require("../utils");
|
|
|
11
11
|
|
|
12
12
|
const VideoCompressEventEmitter = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.VideoCompressor);
|
|
13
13
|
const NativeVideoCompressor = _reactNative.NativeModules.VideoCompressor;
|
|
14
|
+
|
|
15
|
+
const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
16
|
+
const uuid = (0, _utils.uuidv4)();
|
|
17
|
+
let subscription = null;
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
if (onProgress) {
|
|
21
|
+
subscription = VideoCompressEventEmitter.addListener('VideoCompressorProgress', event => {
|
|
22
|
+
if (event.uuid === uuid) {
|
|
23
|
+
onProgress(event.data.written, event.data.total);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (_reactNative.Platform.OS === 'android' && fileUrl.includes('file://')) {
|
|
29
|
+
fileUrl = fileUrl.replace('file://', '');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const result = await NativeVideoCompressor.upload(fileUrl, {
|
|
33
|
+
uuid,
|
|
34
|
+
method: options.httpMethod,
|
|
35
|
+
headers: options.headers,
|
|
36
|
+
url
|
|
37
|
+
});
|
|
38
|
+
return result;
|
|
39
|
+
} finally {
|
|
40
|
+
if (subscription) {
|
|
41
|
+
VideoCompressEventEmitter.removeSubscription(subscription);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
exports.backgroundUpload = backgroundUpload;
|
|
47
|
+
|
|
48
|
+
const cancelCompression = cancellationId => {
|
|
49
|
+
return NativeVideoCompressor.cancelCompression(cancellationId);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
exports.cancelCompression = cancelCompression;
|
|
14
53
|
const Video = {
|
|
15
54
|
compress: async (fileUrl, options, onProgress) => {
|
|
16
55
|
const uuid = (0, _utils.uuidv4)();
|
|
@@ -46,37 +85,11 @@ const Video = {
|
|
|
46
85
|
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
47
86
|
}
|
|
48
87
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
} finally {
|
|
52
|
-
if (subscription) {
|
|
53
|
-
VideoCompressEventEmitter.removeSubscription(subscription);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
backgroundUpload: async (url, fileUrl, options, onProgress) => {
|
|
58
|
-
const uuid = (0, _utils.uuidv4)();
|
|
59
|
-
let subscription = null;
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
if (onProgress) {
|
|
63
|
-
subscription = VideoCompressEventEmitter.addListener('VideoCompressorProgress', event => {
|
|
64
|
-
if (event.uuid === uuid) {
|
|
65
|
-
onProgress(event.data.written, event.data.total);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
88
|
+
if (options !== null && options !== void 0 && options.getCancellationId) {
|
|
89
|
+
options === null || options === void 0 ? void 0 : options.getCancellationId(uuid);
|
|
68
90
|
}
|
|
69
91
|
|
|
70
|
-
|
|
71
|
-
fileUrl = fileUrl.replace('file://', '');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const result = await NativeVideoCompressor.upload(fileUrl, {
|
|
75
|
-
uuid,
|
|
76
|
-
method: options.httpMethod,
|
|
77
|
-
headers: options.headers,
|
|
78
|
-
url
|
|
79
|
-
});
|
|
92
|
+
const result = await NativeVideoCompressor.compress(fileUrl, modifiedOptions);
|
|
80
93
|
return result;
|
|
81
94
|
} finally {
|
|
82
95
|
if (subscription) {
|
|
@@ -84,6 +97,8 @@ const Video = {
|
|
|
84
97
|
}
|
|
85
98
|
}
|
|
86
99
|
},
|
|
100
|
+
backgroundUpload: backgroundUpload,
|
|
101
|
+
cancelCompression,
|
|
87
102
|
|
|
88
103
|
activateBackgroundTask(onExpired) {
|
|
89
104
|
if (onExpired) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["VideoCompressEventEmitter","NativeEventEmitter","NativeModules","VideoCompressor","NativeVideoCompressor","
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["VideoCompressEventEmitter","NativeEventEmitter","NativeModules","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","removeSubscription","cancelCompression","cancellationId","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","getCancellationId","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AACA;;AAgEA,MAAMA,yBAAyB,GAAG,IAAIC,+BAAJ,CAChCC,2BAAcC,eADkB,CAAlC;AAIA,MAAMC,qBAAqB,GAAGF,2BAAcC,eAA5C;;AAEO,MAAME,gBAAgB,GAAG,OAC9BC,GAD8B,EAE9BC,OAF8B,EAG9BC,OAH8B,EAI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,YAAAA,OAAO,CAAE6B,iBAAT,CAA2B3B,IAA3B;AACD;;AACD,YAAMW,MAAM,GAAG,MAAMjB,qBAAqB,CAAC0B,QAAtB,CACnBvB,OADmB,EAEnByB,eAFmB,CAArB;AAIA,aAAOX,MAAP;AACD,KAzCD,SAyCU;AACR,UAAIV,YAAJ,EAAkB;AAChBX,QAAAA,yBAAyB,CAAC0B,kBAA1B,CAA6Cf,YAA7C;AACD;AACF;AACF,GAtDgC;AAuDjCN,EAAAA,gBAAgB,EAAEA,gBAvDe;AAwDjCsB,EAAAA,iBAxDiC;;AAyDjCW,EAAAA,sBAAsB,CAACC,SAAD,EAAa;AACjC,QAAIA,SAAJ,EAAe;AACb,YAAM5B,YAAY,GAAGX,yBAAyB,CAACY,WAA1B,CACnB,uBADmB,EAElBC,KAAD,IAAgB;AACd0B,QAAAA,SAAS,CAAC1B,KAAD,CAAT;AACAb,QAAAA,yBAAyB,CAAC0B,kBAA1B,CAA6Cf,YAA7C;AACD,OALkB,CAArB;AAOD;;AACD,WAAOP,qBAAqB,CAACkC,sBAAtB,CAA6C,EAA7C,CAAP;AACD,GApEgC;;AAqEjCE,EAAAA,wBAAwB,GAAG;AACzBxC,IAAAA,yBAAyB,CAACyC,kBAA1B,CAA6C,uBAA7C;AACA,WAAOrC,qBAAqB,CAACoC,wBAAtB,CAA+C,EAA/C,CAAP;AACD;;AAxEgC,CAAnC;eA2EeX,K","sourcesContent":["import { NativeModules, NativeEventEmitter, Platform } from 'react-native';\nimport { uuidv4 } from '../utils';\n\nexport declare enum FileSystemUploadType {\n BINARY_CONTENT = 0,\n MULTIPART = 1,\n}\n\nexport declare type FileSystemAcceptedUploadHttpMethod =\n | 'POST'\n | 'PUT'\n | 'PATCH';\nexport type compressionMethod = 'auto' | 'manual';\ntype videoCompresssionType = {\n bitrate?: number;\n maxSize?: number;\n compressionMethod?: compressionMethod;\n minimumFileSizeForCompress?: number;\n getCancellationId?: (cancellationId: string) => void;\n};\n\nexport declare enum FileSystemSessionType {\n BACKGROUND = 0,\n FOREGROUND = 1,\n}\n\nexport declare type HTTPResponse = {\n status: number;\n headers: Record<string, string>;\n body: string;\n};\n\nexport declare type FileSystemUploadOptions = (\n | {\n uploadType?: FileSystemUploadType.BINARY_CONTENT;\n }\n | {\n uploadType: FileSystemUploadType.MULTIPART;\n fieldName?: string;\n mimeType?: string;\n parameters?: Record<string, string>;\n }\n) & {\n headers?: Record<string, string>;\n httpMethod?: FileSystemAcceptedUploadHttpMethod;\n sessionType?: FileSystemSessionType;\n};\n\nexport type VideoCompressorType = {\n compress(\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ): Promise<string>;\n cancelCompression(cancellationId: string): void;\n backgroundUpload(\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n ): Promise<any>;\n activateBackgroundTask(onExpired?: (data: any) => void): Promise<any>;\n deactivateBackgroundTask(): Promise<any>;\n};\n\nconst VideoCompressEventEmitter = new NativeEventEmitter(\n NativeModules.VideoCompressor\n);\n\nconst NativeVideoCompressor = NativeModules.VideoCompressor;\n\nexport const backgroundUpload = async (\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n): Promise<any> => {\n const uuid = uuidv4();\n let subscription = null;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'VideoCompressorProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.written, event.data.total);\n }\n }\n );\n }\n if (Platform.OS === 'android' && fileUrl.includes('file://')) {\n fileUrl = fileUrl.replace('file://', '');\n }\n const result = await NativeVideoCompressor.upload(fileUrl, {\n uuid,\n method: options.httpMethod,\n headers: options.headers,\n url,\n });\n return result;\n } finally {\n if (subscription) {\n VideoCompressEventEmitter.removeSubscription(subscription);\n }\n }\n};\n\nexport const cancelCompression = (cancellationId: string) => {\n return NativeVideoCompressor.cancelCompression(cancellationId);\n};\n\nconst Video: VideoCompressorType = {\n compress: async (\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ) => {\n const uuid = uuidv4();\n let subscription = null;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'videoCompressProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.progress);\n }\n }\n );\n }\n const modifiedOptions: {\n uuid: string;\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n } = { uuid };\n if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;\n if (options?.compressionMethod) {\n modifiedOptions.compressionMethod = options?.compressionMethod;\n } else {\n modifiedOptions.compressionMethod = 'manual';\n }\n if (options?.maxSize) {\n modifiedOptions.maxSize = options?.maxSize;\n } else {\n modifiedOptions.maxSize = 640;\n }\n if (options?.minimumFileSizeForCompress) {\n modifiedOptions.minimumFileSizeForCompress =\n options?.minimumFileSizeForCompress;\n }\n if (options?.getCancellationId) {\n options?.getCancellationId(uuid);\n }\n const result = await NativeVideoCompressor.compress(\n fileUrl,\n modifiedOptions\n );\n return result;\n } finally {\n if (subscription) {\n VideoCompressEventEmitter.removeSubscription(subscription);\n }\n }\n },\n backgroundUpload: backgroundUpload,\n cancelCompression,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription = VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n VideoCompressEventEmitter.removeSubscription(subscription);\n }\n );\n }\n return NativeVideoCompressor.activateBackgroundTask({});\n },\n deactivateBackgroundTask() {\n VideoCompressEventEmitter.removeAllListeners('backgroundTaskExpired');\n return NativeVideoCompressor.deactivateBackgroundTask({});\n },\n} as VideoCompressorType;\n\nexport default Video;\n"]}
|
|
@@ -2,6 +2,39 @@ import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
|
|
|
2
2
|
import { uuidv4 } from '../utils';
|
|
3
3
|
const VideoCompressEventEmitter = new NativeEventEmitter(NativeModules.VideoCompressor);
|
|
4
4
|
const NativeVideoCompressor = NativeModules.VideoCompressor;
|
|
5
|
+
export const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
6
|
+
const uuid = uuidv4();
|
|
7
|
+
let subscription = null;
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
if (onProgress) {
|
|
11
|
+
subscription = VideoCompressEventEmitter.addListener('VideoCompressorProgress', event => {
|
|
12
|
+
if (event.uuid === uuid) {
|
|
13
|
+
onProgress(event.data.written, event.data.total);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (Platform.OS === 'android' && fileUrl.includes('file://')) {
|
|
19
|
+
fileUrl = fileUrl.replace('file://', '');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const result = await NativeVideoCompressor.upload(fileUrl, {
|
|
23
|
+
uuid,
|
|
24
|
+
method: options.httpMethod,
|
|
25
|
+
headers: options.headers,
|
|
26
|
+
url
|
|
27
|
+
});
|
|
28
|
+
return result;
|
|
29
|
+
} finally {
|
|
30
|
+
if (subscription) {
|
|
31
|
+
VideoCompressEventEmitter.removeSubscription(subscription);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export const cancelCompression = cancellationId => {
|
|
36
|
+
return NativeVideoCompressor.cancelCompression(cancellationId);
|
|
37
|
+
};
|
|
5
38
|
const Video = {
|
|
6
39
|
compress: async (fileUrl, options, onProgress) => {
|
|
7
40
|
const uuid = uuidv4();
|
|
@@ -37,37 +70,11 @@ const Video = {
|
|
|
37
70
|
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
38
71
|
}
|
|
39
72
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
} finally {
|
|
43
|
-
if (subscription) {
|
|
44
|
-
VideoCompressEventEmitter.removeSubscription(subscription);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
backgroundUpload: async (url, fileUrl, options, onProgress) => {
|
|
49
|
-
const uuid = uuidv4();
|
|
50
|
-
let subscription = null;
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
if (onProgress) {
|
|
54
|
-
subscription = VideoCompressEventEmitter.addListener('VideoCompressorProgress', event => {
|
|
55
|
-
if (event.uuid === uuid) {
|
|
56
|
-
onProgress(event.data.written, event.data.total);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
73
|
+
if (options !== null && options !== void 0 && options.getCancellationId) {
|
|
74
|
+
options === null || options === void 0 ? void 0 : options.getCancellationId(uuid);
|
|
59
75
|
}
|
|
60
76
|
|
|
61
|
-
|
|
62
|
-
fileUrl = fileUrl.replace('file://', '');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const result = await NativeVideoCompressor.upload(fileUrl, {
|
|
66
|
-
uuid,
|
|
67
|
-
method: options.httpMethod,
|
|
68
|
-
headers: options.headers,
|
|
69
|
-
url
|
|
70
|
-
});
|
|
77
|
+
const result = await NativeVideoCompressor.compress(fileUrl, modifiedOptions);
|
|
71
78
|
return result;
|
|
72
79
|
} finally {
|
|
73
80
|
if (subscription) {
|
|
@@ -75,6 +82,8 @@ const Video = {
|
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
84
|
},
|
|
85
|
+
backgroundUpload: backgroundUpload,
|
|
86
|
+
cancelCompression,
|
|
78
87
|
|
|
79
88
|
activateBackgroundTask(onExpired) {
|
|
80
89
|
if (onExpired) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["NativeModules","NativeEventEmitter","Platform","uuidv4","VideoCompressEventEmitter","VideoCompressor","NativeVideoCompressor","
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["NativeModules","NativeEventEmitter","Platform","uuidv4","VideoCompressEventEmitter","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","OS","includes","replace","result","upload","method","httpMethod","headers","removeSubscription","cancelCompression","cancellationId","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","getCancellationId","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":"AAAA,SAASA,aAAT,EAAwBC,kBAAxB,EAA4CC,QAA5C,QAA4D,cAA5D;AACA,SAASC,MAAT,QAAuB,UAAvB;AAgEA,MAAMC,yBAAyB,GAAG,IAAIH,kBAAJ,CAChCD,aAAa,CAACK,eADkB,CAAlC;AAIA,MAAMC,qBAAqB,GAAGN,aAAa,CAACK,eAA5C;AAEA,OAAO,MAAME,gBAAgB,GAAG,OAC9BC,GAD8B,EAE9BC,OAF8B,EAG9BC,OAH8B,EAI9BC,UAJ8B,KAKb;AACjB,QAAMC,IAAI,GAAGT,MAAM,EAAnB;AACA,MAAIU,YAAY,GAAG,IAAnB;;AACA,MAAI;AACF,QAAIF,UAAJ,EAAgB;AACdE,MAAAA,YAAY,GAAGT,yBAAyB,CAACU,WAA1B,CACb,yBADa,EAEZC,KAAD,IAAgB;AACd,YAAIA,KAAK,CAACH,IAAN,KAAeA,IAAnB,EAAyB;AACvBD,UAAAA,UAAU,CAACI,KAAK,CAACC,IAAN,CAAWC,OAAZ,EAAqBF,KAAK,CAACC,IAAN,CAAWE,KAAhC,CAAV;AACD;AACF,OANY,CAAf;AAQD;;AACD,QAAIhB,QAAQ,CAACiB,EAAT,KAAgB,SAAhB,IAA6BV,OAAO,CAACW,QAAR,CAAiB,SAAjB,CAAjC,EAA8D;AAC5DX,MAAAA,OAAO,GAAGA,OAAO,CAACY,OAAR,CAAgB,SAAhB,EAA2B,EAA3B,CAAV;AACD;;AACD,UAAMC,MAAM,GAAG,MAAMhB,qBAAqB,CAACiB,MAAtB,CAA6Bd,OAA7B,EAAsC;AACzDG,MAAAA,IADyD;AAEzDY,MAAAA,MAAM,EAAEd,OAAO,CAACe,UAFyC;AAGzDC,MAAAA,OAAO,EAAEhB,OA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AyB,CAACsC,kBAA1B,CAA6C,uBAA7C;AACA,WAAOpC,qBAAqB,CAACmC,wBAAtB,CAA+C,EAA/C,CAAP;AACD;;AAxEgC,CAAnC;AA2EA,eAAeX,KAAf","sourcesContent":["import { NativeModules, NativeEventEmitter, Platform } from 'react-native';\nimport { uuidv4 } from '../utils';\n\nexport declare enum FileSystemUploadType {\n BINARY_CONTENT = 0,\n MULTIPART = 1,\n}\n\nexport declare type FileSystemAcceptedUploadHttpMethod =\n | 'POST'\n | 'PUT'\n | 'PATCH';\nexport type compressionMethod = 'auto' | 'manual';\ntype videoCompresssionType = {\n bitrate?: number;\n maxSize?: number;\n compressionMethod?: compressionMethod;\n minimumFileSizeForCompress?: number;\n getCancellationId?: (cancellationId: string) => void;\n};\n\nexport declare enum FileSystemSessionType {\n BACKGROUND = 0,\n FOREGROUND = 1,\n}\n\nexport declare type HTTPResponse = {\n status: number;\n headers: Record<string, string>;\n body: string;\n};\n\nexport declare type FileSystemUploadOptions = (\n | {\n uploadType?: FileSystemUploadType.BINARY_CONTENT;\n }\n | {\n uploadType: FileSystemUploadType.MULTIPART;\n fieldName?: string;\n mimeType?: string;\n parameters?: Record<string, string>;\n }\n) & {\n headers?: Record<string, string>;\n httpMethod?: FileSystemAcceptedUploadHttpMethod;\n sessionType?: FileSystemSessionType;\n};\n\nexport type VideoCompressorType = {\n compress(\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ): Promise<string>;\n cancelCompression(cancellationId: string): void;\n backgroundUpload(\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n ): Promise<any>;\n activateBackgroundTask(onExpired?: (data: any) => void): Promise<any>;\n deactivateBackgroundTask(): Promise<any>;\n};\n\nconst VideoCompressEventEmitter = new NativeEventEmitter(\n NativeModules.VideoCompressor\n);\n\nconst NativeVideoCompressor = NativeModules.VideoCompressor;\n\nexport const backgroundUpload = async (\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n): Promise<any> => {\n const uuid = uuidv4();\n let subscription = null;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'VideoCompressorProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.written, event.data.total);\n }\n }\n );\n }\n if (Platform.OS === 'android' && fileUrl.includes('file://')) {\n fileUrl = fileUrl.replace('file://', '');\n }\n const result = await NativeVideoCompressor.upload(fileUrl, {\n uuid,\n method: options.httpMethod,\n headers: options.headers,\n url,\n });\n return result;\n } finally {\n if (subscription) {\n VideoCompressEventEmitter.removeSubscription(subscription);\n }\n }\n};\n\nexport const cancelCompression = (cancellationId: string) => {\n return NativeVideoCompressor.cancelCompression(cancellationId);\n};\n\nconst Video: VideoCompressorType = {\n compress: async (\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ) => {\n const uuid = uuidv4();\n let subscription = null;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'videoCompressProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.progress);\n }\n }\n );\n }\n const modifiedOptions: {\n uuid: string;\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n } = { uuid };\n if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;\n if (options?.compressionMethod) {\n modifiedOptions.compressionMethod = options?.compressionMethod;\n } else {\n modifiedOptions.compressionMethod = 'manual';\n }\n if (options?.maxSize) {\n modifiedOptions.maxSize = options?.maxSize;\n } else {\n modifiedOptions.maxSize = 640;\n }\n if (options?.minimumFileSizeForCompress) {\n modifiedOptions.minimumFileSizeForCompress =\n options?.minimumFileSizeForCompress;\n }\n if (options?.getCancellationId) {\n options?.getCancellationId(uuid);\n }\n const result = await NativeVideoCompressor.compress(\n fileUrl,\n modifiedOptions\n );\n return result;\n } finally {\n if (subscription) {\n VideoCompressEventEmitter.removeSubscription(subscription);\n }\n }\n },\n backgroundUpload: backgroundUpload,\n cancelCompression,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription = VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n VideoCompressEventEmitter.removeSubscription(subscription);\n }\n );\n }\n return NativeVideoCompressor.activateBackgroundTask({});\n },\n deactivateBackgroundTask() {\n VideoCompressEventEmitter.removeAllListeners('backgroundTaskExpired');\n return NativeVideoCompressor.deactivateBackgroundTask({});\n },\n} as VideoCompressorType;\n\nexport default Video;\n"]}
|
|
@@ -9,6 +9,7 @@ declare type videoCompresssionType = {
|
|
|
9
9
|
maxSize?: number;
|
|
10
10
|
compressionMethod?: compressionMethod;
|
|
11
11
|
minimumFileSizeForCompress?: number;
|
|
12
|
+
getCancellationId?: (cancellationId: string) => void;
|
|
12
13
|
};
|
|
13
14
|
export declare enum FileSystemSessionType {
|
|
14
15
|
BACKGROUND = 0,
|
|
@@ -33,9 +34,12 @@ export declare type FileSystemUploadOptions = ({
|
|
|
33
34
|
};
|
|
34
35
|
export declare type VideoCompressorType = {
|
|
35
36
|
compress(fileUrl: string, options?: videoCompresssionType, onProgress?: (progress: number) => void): Promise<string>;
|
|
37
|
+
cancelCompression(cancellationId: string): void;
|
|
36
38
|
backgroundUpload(url: string, fileUrl: string, options: FileSystemUploadOptions, onProgress?: (writtem: number, total: number) => void): Promise<any>;
|
|
37
39
|
activateBackgroundTask(onExpired?: (data: any) => void): Promise<any>;
|
|
38
40
|
deactivateBackgroundTask(): Promise<any>;
|
|
39
41
|
};
|
|
42
|
+
export declare const backgroundUpload: (url: string, fileUrl: string, options: FileSystemUploadOptions, onProgress?: ((writtem: number, total: number) => void) | undefined) => Promise<any>;
|
|
43
|
+
export declare const cancelCompression: (cancellationId: string) => any;
|
|
40
44
|
declare const Video: VideoCompressorType;
|
|
41
45
|
export default Video;
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@ Pod::Spec.new do |s|
|
|
|
10
10
|
s.license = package["license"]
|
|
11
11
|
s.authors = package["author"]
|
|
12
12
|
|
|
13
|
-
s.platforms = { :ios => "
|
|
13
|
+
s.platforms = { :ios => "11.0" }
|
|
14
14
|
s.source = { :git => "https://github.com/Shobbak/react-native-compressor.git", :tag => "#{s.version}" }
|
|
15
15
|
|
|
16
16
|
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
package/src/Video/index.tsx
CHANGED
|
@@ -16,6 +16,7 @@ type videoCompresssionType = {
|
|
|
16
16
|
maxSize?: number;
|
|
17
17
|
compressionMethod?: compressionMethod;
|
|
18
18
|
minimumFileSizeForCompress?: number;
|
|
19
|
+
getCancellationId?: (cancellationId: string) => void;
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
export declare enum FileSystemSessionType {
|
|
@@ -51,6 +52,7 @@ export type VideoCompressorType = {
|
|
|
51
52
|
options?: videoCompresssionType,
|
|
52
53
|
onProgress?: (progress: number) => void
|
|
53
54
|
): Promise<string>;
|
|
55
|
+
cancelCompression(cancellationId: string): void;
|
|
54
56
|
backgroundUpload(
|
|
55
57
|
url: string,
|
|
56
58
|
fileUrl: string,
|
|
@@ -67,15 +69,50 @@ const VideoCompressEventEmitter = new NativeEventEmitter(
|
|
|
67
69
|
|
|
68
70
|
const NativeVideoCompressor = NativeModules.VideoCompressor;
|
|
69
71
|
|
|
72
|
+
export const backgroundUpload = async (
|
|
73
|
+
url: string,
|
|
74
|
+
fileUrl: string,
|
|
75
|
+
options: FileSystemUploadOptions,
|
|
76
|
+
onProgress?: (writtem: number, total: number) => void
|
|
77
|
+
): Promise<any> => {
|
|
78
|
+
const uuid = uuidv4();
|
|
79
|
+
let subscription = null;
|
|
80
|
+
try {
|
|
81
|
+
if (onProgress) {
|
|
82
|
+
subscription = VideoCompressEventEmitter.addListener(
|
|
83
|
+
'VideoCompressorProgress',
|
|
84
|
+
(event: any) => {
|
|
85
|
+
if (event.uuid === uuid) {
|
|
86
|
+
onProgress(event.data.written, event.data.total);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (Platform.OS === 'android' && fileUrl.includes('file://')) {
|
|
92
|
+
fileUrl = fileUrl.replace('file://', '');
|
|
93
|
+
}
|
|
94
|
+
const result = await NativeVideoCompressor.upload(fileUrl, {
|
|
95
|
+
uuid,
|
|
96
|
+
method: options.httpMethod,
|
|
97
|
+
headers: options.headers,
|
|
98
|
+
url,
|
|
99
|
+
});
|
|
100
|
+
return result;
|
|
101
|
+
} finally {
|
|
102
|
+
if (subscription) {
|
|
103
|
+
VideoCompressEventEmitter.removeSubscription(subscription);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const cancelCompression = (cancellationId: string) => {
|
|
109
|
+
return NativeVideoCompressor.cancelCompression(cancellationId);
|
|
110
|
+
};
|
|
111
|
+
|
|
70
112
|
const Video: VideoCompressorType = {
|
|
71
113
|
compress: async (
|
|
72
114
|
fileUrl: string,
|
|
73
|
-
options?:
|
|
74
|
-
bitrate?: number;
|
|
75
|
-
compressionMethod?: compressionMethod;
|
|
76
|
-
maxSize?: number;
|
|
77
|
-
minimumFileSizeForCompress?: number;
|
|
78
|
-
},
|
|
115
|
+
options?: videoCompresssionType,
|
|
79
116
|
onProgress?: (progress: number) => void
|
|
80
117
|
) => {
|
|
81
118
|
const uuid = uuidv4();
|
|
@@ -113,6 +150,9 @@ const Video: VideoCompressorType = {
|
|
|
113
150
|
modifiedOptions.minimumFileSizeForCompress =
|
|
114
151
|
options?.minimumFileSizeForCompress;
|
|
115
152
|
}
|
|
153
|
+
if (options?.getCancellationId) {
|
|
154
|
+
options?.getCancellationId(uuid);
|
|
155
|
+
}
|
|
116
156
|
const result = await NativeVideoCompressor.compress(
|
|
117
157
|
fileUrl,
|
|
118
158
|
modifiedOptions
|
|
@@ -124,36 +164,8 @@ const Video: VideoCompressorType = {
|
|
|
124
164
|
}
|
|
125
165
|
}
|
|
126
166
|
},
|
|
127
|
-
backgroundUpload:
|
|
128
|
-
|
|
129
|
-
let subscription = null;
|
|
130
|
-
try {
|
|
131
|
-
if (onProgress) {
|
|
132
|
-
subscription = VideoCompressEventEmitter.addListener(
|
|
133
|
-
'VideoCompressorProgress',
|
|
134
|
-
(event: any) => {
|
|
135
|
-
if (event.uuid === uuid) {
|
|
136
|
-
onProgress(event.data.written, event.data.total);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
if (Platform.OS === 'android' && fileUrl.includes('file://')) {
|
|
142
|
-
fileUrl = fileUrl.replace('file://', '');
|
|
143
|
-
}
|
|
144
|
-
const result = await NativeVideoCompressor.upload(fileUrl, {
|
|
145
|
-
uuid,
|
|
146
|
-
method: options.httpMethod,
|
|
147
|
-
headers: options.headers,
|
|
148
|
-
url,
|
|
149
|
-
});
|
|
150
|
-
return result;
|
|
151
|
-
} finally {
|
|
152
|
-
if (subscription) {
|
|
153
|
-
VideoCompressEventEmitter.removeSubscription(subscription);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
},
|
|
167
|
+
backgroundUpload: backgroundUpload,
|
|
168
|
+
cancelCompression,
|
|
157
169
|
activateBackgroundTask(onExpired?) {
|
|
158
170
|
if (onExpired) {
|
|
159
171
|
const subscription = VideoCompressEventEmitter.addListener(
|