react-native-compressor 1.3.2 → 1.3.3
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/README.md +36 -1
- package/android/build.gradle +0 -4
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.java +0 -1
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +0 -1
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +0 -1
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java +3 -37
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +2 -38
- package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +19 -17
- package/ios/Compressor.m +2 -0
- package/ios/Video/VideoCompressor.swift +20 -11
- package/lib/commonjs/Video/index.js +12 -1
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/module/Video/index.js +8 -0
- package/lib/module/Video/index.js.map +1 -1
- package/lib/typescript/Video/index.d.ts +3 -0
- package/package.json +1 -1
- package/src/Video/index.tsx +11 -6
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ If you find this package useful hit the star 🌟
|
|
|
33
33
|
#### For React Native<0.65
|
|
34
34
|
|
|
35
35
|
```sh
|
|
36
|
-
yarn add react-native-compressor@0.5.
|
|
36
|
+
yarn add react-native-compressor@0.5.11
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
#### For React Native 0.65 or greater
|
|
@@ -176,6 +176,34 @@ const result = await Video.compress(
|
|
|
176
176
|
);
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
+
##### Cancel Video Compression
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
import { Video } from 'react-native-compressor';
|
|
183
|
+
|
|
184
|
+
let cancellationVideoId = '';
|
|
185
|
+
|
|
186
|
+
const result = await Video.compress(
|
|
187
|
+
'file://path_of_file/BigBuckBunny.mp4',
|
|
188
|
+
{
|
|
189
|
+
compressionMethod: 'auto',
|
|
190
|
+
// getCancellationId for get video id which we can use for cancel compression
|
|
191
|
+
getCancellationId: (cancellationId) =>
|
|
192
|
+
(cancellationVideoId = cancellationId),
|
|
193
|
+
},
|
|
194
|
+
(progress) => {
|
|
195
|
+
if (backgroundMode) {
|
|
196
|
+
console.log('Compression Progress: ', progress);
|
|
197
|
+
} else {
|
|
198
|
+
setCompressingProgress(progress);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
// we can cancel video compression by calling cancelCompression with cancel video id which we can get from getCancellationId function while compression
|
|
204
|
+
Video.cancelCompression(cancellationVideoId);
|
|
205
|
+
```
|
|
206
|
+
|
|
179
207
|
### Audio
|
|
180
208
|
|
|
181
209
|
```js
|
|
@@ -247,6 +275,9 @@ const uploadResult = await backgroundUpload(
|
|
|
247
275
|
|
|
248
276
|
- ###### `compress(url: string, options?: videoCompresssionType , onProgress?: (progress: number)): Promise<string>`
|
|
249
277
|
|
|
278
|
+
- ###### `cancelCompression(cancellationId: string): void`
|
|
279
|
+
we can get cancellationId from `getCancellationId` which is the callback method of compress method options
|
|
280
|
+
|
|
250
281
|
### videoCompresssionType
|
|
251
282
|
|
|
252
283
|
- ###### `compressionMethod: compressionMethod` (default: "manual")
|
|
@@ -262,8 +293,12 @@ const uploadResult = await backgroundUpload(
|
|
|
262
293
|
bitrate of video which reduce or increase video size. if compressionMethod will auto then this prop will not work
|
|
263
294
|
|
|
264
295
|
- ###### `minimumFileSizeForCompress: number` (default: 16)
|
|
296
|
+
|
|
265
297
|
16 means 16mb. default our package do not compress under 16mb video file. minimumFileSizeForCompress will allow us to change this 16mb offset. fixed [#26](https://github.com/Shobbak/react-native-compressor/issues/26)
|
|
266
298
|
|
|
299
|
+
- ###### `getCancellationId: function`
|
|
300
|
+
`getCancellationId` is a callback function that gives us compress video id, which can be used in `Video.cancelCompression` method to cancel the compression
|
|
301
|
+
|
|
267
302
|
## Audio
|
|
268
303
|
|
|
269
304
|
- ###### `compress(url: string, options?: audioCompresssionType): Promise<string>`
|
package/android/build.gradle
CHANGED
|
@@ -56,10 +56,6 @@ repositories {
|
|
|
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')
|
|
@@ -21,7 +21,6 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
|
21
21
|
import com.reactnativecompressor.Image.ImageCompressor;
|
|
22
22
|
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
23
23
|
import com.reactnativecompressor.Video.VideoCompressorHelper;
|
|
24
|
-
|
|
25
24
|
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
26
25
|
import com.reactnativecompressor.Audio.AudioCompressor;
|
|
27
26
|
|
|
@@ -10,7 +10,6 @@ import com.facebook.react.bridge.WritableMap;
|
|
|
10
10
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
11
11
|
import numan.dev.videocompressor.VideoCompressTask;
|
|
12
12
|
import numan.dev.videocompressor.VideoCompressor;
|
|
13
|
-
|
|
14
13
|
import java.io.File;
|
|
15
14
|
import java.util.HashMap;
|
|
16
15
|
import java.util.Map;
|
|
@@ -1,30 +1,23 @@
|
|
|
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;
|
|
@@ -52,43 +45,16 @@ public class AutoVideoCompression {
|
|
|
52
45
|
bitrate,
|
|
53
46
|
resultHeight, resultWidth
|
|
54
47
|
);
|
|
55
|
-
|
|
56
|
-
VideoSlimmer.convertVideo(srcPath, destinationPath, resultWidth, resultHeight, (int) videoBitRate, new VideoSlimmer.ProgressListener() {
|
|
57
|
-
@Override
|
|
58
|
-
public void onStart() {
|
|
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
|
-
});
|
|
80
|
-
|
|
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 {
|
|
@@ -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
|
|
|
@@ -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
|
var videoCompressionCounter:Int=0
|
|
39
40
|
|
|
@@ -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)
|
|
@@ -335,7 +342,7 @@ func makeValidUri(filePath: String) -> String {
|
|
|
335
342
|
}
|
|
336
343
|
}
|
|
337
344
|
|
|
338
|
-
func exportVideoHelper(url: URL,asset: AVAsset, bitRate: Int,resultWidth:Float,resultHeight:Float, onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
345
|
+
func exportVideoHelper(url: URL,asset: AVAsset, bitRate: Int,resultWidth:Float,resultHeight:Float,uuid:String, onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
339
346
|
var tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
340
347
|
.appendingPathComponent(ProcessInfo().globallyUniqueString)
|
|
341
348
|
.appendingPathExtension("mp4")
|
|
@@ -364,7 +371,7 @@ func makeValidUri(filePath: String) -> String {
|
|
|
364
371
|
AVSampleRateKey: NSNumber(value: Float(44100))
|
|
365
372
|
]
|
|
366
373
|
|
|
367
|
-
|
|
374
|
+
compressorExports[uuid] = exporter
|
|
368
375
|
exporter.export(progressHandler: { (progress) in
|
|
369
376
|
let _progress:Float=progress*100;
|
|
370
377
|
if(Int(_progress)==self.videoCompressionCounter)
|
|
@@ -403,4 +410,6 @@ func makeValidUri(filePath: String) -> String {
|
|
|
403
410
|
let track = asset.tracks[videoTrackIndex];
|
|
404
411
|
return track;
|
|
405
412
|
}
|
|
413
|
+
|
|
414
|
+
|
|
406
415
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default = exports.backgroundUpload = void 0;
|
|
6
|
+
exports.default = exports.cancelCompression = exports.backgroundUpload = void 0;
|
|
7
7
|
|
|
8
8
|
var _reactNative = require("react-native");
|
|
9
9
|
|
|
@@ -45,6 +45,12 @@ const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
exports.backgroundUpload = backgroundUpload;
|
|
48
|
+
|
|
49
|
+
const cancelCompression = cancellationId => {
|
|
50
|
+
return NativeVideoCompressor.cancelCompression(cancellationId);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
exports.cancelCompression = cancelCompression;
|
|
48
54
|
const Video = {
|
|
49
55
|
compress: async (fileUrl, options, onProgress) => {
|
|
50
56
|
const uuid = (0, _utils.uuidv4)();
|
|
@@ -80,6 +86,10 @@ const Video = {
|
|
|
80
86
|
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
81
87
|
}
|
|
82
88
|
|
|
89
|
+
if (options !== null && options !== void 0 && options.getCancellationId) {
|
|
90
|
+
options === null || options === void 0 ? void 0 : options.getCancellationId(uuid);
|
|
91
|
+
}
|
|
92
|
+
|
|
83
93
|
const result = await NativeVideoCompressor.compress(fileUrl, modifiedOptions);
|
|
84
94
|
return result;
|
|
85
95
|
} finally {
|
|
@@ -90,6 +100,7 @@ const Video = {
|
|
|
90
100
|
}
|
|
91
101
|
},
|
|
92
102
|
backgroundUpload,
|
|
103
|
+
cancelCompression,
|
|
93
104
|
|
|
94
105
|
activateBackgroundTask(onExpired) {
|
|
95
106
|
if (onExpired) {
|
|
@@ -1 +1 @@
|
|
|
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","remove","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AAMA;;
|
|
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","remove","cancelCompression","cancellationId","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","getCancellationId","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AAMA;;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,EAI9BC,UAJ8B,KAKb;AACjB,QAAMC,IAAI,GAAG,oBAAb;AACA,MAAIC,YAAJ;;AACA,MAAI;AACF,QAAIF,UAAJ,EAAgB;AACdE,MAAAA,YAAY,GAAGX,yBAAyB,CAACY,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,QAAIC,sBAASC,EAAT,KAAgB,SAAhB,IAA6BX,OAAO,CAACY,QAAR,CAAiB,SAAjB,CAAjC,EAA8D;AAC5DZ,MAAAA,OAAO,GAAGA,OAAO,CAACa,OAAR,CAAgB,SAAhB,EAA2B,EAA3B,CAAV;AACD;;AACD,UAAMC,MAAM,GAAG,MAAMjB,qBAAqB,CAACkB,MAAtB,CAA6Bf,OAA7B,EAAsC;AACzDG,MAAAA,IADyD;AAEzDa,MAAAA,MAAM,EAAEf,OAAO,CAACgB,UAFyC;AAGzDC,MAAAA,OAAO,EAAEjB,OAAO,CAACiB,OAHwC;AAIzDnB,MAAAA;AAJyD,KAA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A5EgC,CAAnC;eA+EeX,K","sourcesContent":["import {\n NativeModules,\n NativeEventEmitter,\n Platform,\n NativeEventSubscription,\n} 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: NativeEventSubscription;\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 // @ts-ignore\n if (subscription) {\n subscription.remove();\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: NativeEventSubscription;\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 // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n },\n backgroundUpload,\n cancelCompression,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription: NativeEventSubscription =\n VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n if (subscription) {\n subscription.remove();\n }\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"]}
|
|
@@ -33,6 +33,9 @@ export const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
+
export const cancelCompression = cancellationId => {
|
|
37
|
+
return NativeVideoCompressor.cancelCompression(cancellationId);
|
|
38
|
+
};
|
|
36
39
|
const Video = {
|
|
37
40
|
compress: async (fileUrl, options, onProgress) => {
|
|
38
41
|
const uuid = uuidv4();
|
|
@@ -68,6 +71,10 @@ const Video = {
|
|
|
68
71
|
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
69
72
|
}
|
|
70
73
|
|
|
74
|
+
if (options !== null && options !== void 0 && options.getCancellationId) {
|
|
75
|
+
options === null || options === void 0 ? void 0 : options.getCancellationId(uuid);
|
|
76
|
+
}
|
|
77
|
+
|
|
71
78
|
const result = await NativeVideoCompressor.compress(fileUrl, modifiedOptions);
|
|
72
79
|
return result;
|
|
73
80
|
} finally {
|
|
@@ -78,6 +85,7 @@ const Video = {
|
|
|
78
85
|
}
|
|
79
86
|
},
|
|
80
87
|
backgroundUpload,
|
|
88
|
+
cancelCompression,
|
|
81
89
|
|
|
82
90
|
activateBackgroundTask(onExpired) {
|
|
83
91
|
if (onExpired) {
|
|
@@ -1 +1 @@
|
|
|
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","remove","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":"AAAA,SACEA,aADF,EAEEC,kBAFF,EAGEC,QAHF,QAKO,cALP;AAMA,SAASC,MAAT,QAAuB,UAAvB;
|
|
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","remove","cancelCompression","cancellationId","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","getCancellationId","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":"AAAA,SACEA,aADF,EAEEC,kBAFF,EAGEC,QAHF,QAKO,cALP;AAMA,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,YAAJ;;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,O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,EAAe;AACb,YAAM3B,YAAqC,GACzCT,yBAAyB,CAACU,WAA1B,CACE,uBADF,EAEGC,KAAD,IAAgB;AACdyB,QAAAA,SAAS,CAACzB,KAAD,CAAT;;AACA,YAAIF,YAAJ,EAAkB;AAChBA,UAAAA,YAAY,CAACc,MAAb;AACD;AACF,OAPH,CADF;AAUD;;AACD,WAAOrB,qBAAqB,CAACiC,sBAAtB,CAA6C,EAA7C,CAAP;AACD,GAxEgC;;AAyEjCE,EAAAA,wBAAwB,GAAG;AACzBrC,IAAAA,yBAAyB,CAACsC,kBAA1B,CAA6C,uBAA7C;AACA,WAAOpC,qBAAqB,CAACmC,wBAAtB,CAA+C,EAA/C,CAAP;AACD;;AA5EgC,CAAnC;AA+EA,eAAeX,KAAf","sourcesContent":["import {\n NativeModules,\n NativeEventEmitter,\n Platform,\n NativeEventSubscription,\n} 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: NativeEventSubscription;\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 // @ts-ignore\n if (subscription) {\n subscription.remove();\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: NativeEventSubscription;\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 // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n },\n backgroundUpload,\n cancelCompression,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription: NativeEventSubscription =\n VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n if (subscription) {\n subscription.remove();\n }\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,10 +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
|
};
|
|
40
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;
|
|
41
44
|
declare const Video: VideoCompressorType;
|
|
42
45
|
export default Video;
|
package/package.json
CHANGED
package/src/Video/index.tsx
CHANGED
|
@@ -21,6 +21,7 @@ type videoCompresssionType = {
|
|
|
21
21
|
maxSize?: number;
|
|
22
22
|
compressionMethod?: compressionMethod;
|
|
23
23
|
minimumFileSizeForCompress?: number;
|
|
24
|
+
getCancellationId?: (cancellationId: string) => void;
|
|
24
25
|
};
|
|
25
26
|
|
|
26
27
|
export declare enum FileSystemSessionType {
|
|
@@ -56,6 +57,7 @@ export type VideoCompressorType = {
|
|
|
56
57
|
options?: videoCompresssionType,
|
|
57
58
|
onProgress?: (progress: number) => void
|
|
58
59
|
): Promise<string>;
|
|
60
|
+
cancelCompression(cancellationId: string): void;
|
|
59
61
|
backgroundUpload(
|
|
60
62
|
url: string,
|
|
61
63
|
fileUrl: string,
|
|
@@ -109,15 +111,14 @@ export const backgroundUpload = async (
|
|
|
109
111
|
}
|
|
110
112
|
};
|
|
111
113
|
|
|
114
|
+
export const cancelCompression = (cancellationId: string) => {
|
|
115
|
+
return NativeVideoCompressor.cancelCompression(cancellationId);
|
|
116
|
+
};
|
|
117
|
+
|
|
112
118
|
const Video: VideoCompressorType = {
|
|
113
119
|
compress: async (
|
|
114
120
|
fileUrl: string,
|
|
115
|
-
options?:
|
|
116
|
-
bitrate?: number;
|
|
117
|
-
compressionMethod?: compressionMethod;
|
|
118
|
-
maxSize?: number;
|
|
119
|
-
minimumFileSizeForCompress?: number;
|
|
120
|
-
},
|
|
121
|
+
options?: videoCompresssionType,
|
|
121
122
|
onProgress?: (progress: number) => void
|
|
122
123
|
) => {
|
|
123
124
|
const uuid = uuidv4();
|
|
@@ -155,6 +156,9 @@ const Video: VideoCompressorType = {
|
|
|
155
156
|
modifiedOptions.minimumFileSizeForCompress =
|
|
156
157
|
options?.minimumFileSizeForCompress;
|
|
157
158
|
}
|
|
159
|
+
if (options?.getCancellationId) {
|
|
160
|
+
options?.getCancellationId(uuid);
|
|
161
|
+
}
|
|
158
162
|
const result = await NativeVideoCompressor.compress(
|
|
159
163
|
fileUrl,
|
|
160
164
|
modifiedOptions
|
|
@@ -168,6 +172,7 @@ const Video: VideoCompressorType = {
|
|
|
168
172
|
}
|
|
169
173
|
},
|
|
170
174
|
backgroundUpload,
|
|
175
|
+
cancelCompression,
|
|
171
176
|
activateBackgroundTask(onExpired?) {
|
|
172
177
|
if (onExpired) {
|
|
173
178
|
const subscription: NativeEventSubscription =
|