react-native-compressor 1.2.1 → 1.3.0
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 -1
- package/android/src/main/java/com/reactnativecompressor/.DS_Store +0 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.java +3 -3
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +5 -1
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +74 -0
- package/android/src/main/java/com/reactnativecompressor/Video/.DS_Store +0 -0
- 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 +9 -7
- package/android/src/main/java/com/reactnativecompressor/Video/videoslimmer/VideoSlimEncoder.java +629 -0
- package/android/src/main/java/com/reactnativecompressor/Video/videoslimmer/VideoSlimTask.java +59 -0
- package/android/src/main/java/com/reactnativecompressor/Video/videoslimmer/VideoSlimmer.java +20 -0
- package/android/src/main/java/com/reactnativecompressor/Video/videoslimmer/listner/SlimProgressListener.java +6 -0
- package/android/src/main/java/com/reactnativecompressor/Video/videoslimmer/muxer/CodecInputSurface.java +202 -0
- package/android/src/main/java/com/reactnativecompressor/Video/videoslimmer/render/TextureRenderer.java +196 -0
- 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
|
@@ -58,6 +58,5 @@ dependencies {
|
|
|
58
58
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0"
|
|
59
59
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.0"
|
|
60
60
|
implementation "com.googlecode.mp4parser:isoparser:1.0.6"
|
|
61
|
-
implementation 'com.github.zolad:VideoSlimmer:master-SNAPSHOT'
|
|
62
61
|
implementation 'io.github.lizhangqu:coreprogress:1.0.2'
|
|
63
62
|
}
|
|
Binary file
|
|
@@ -14,9 +14,9 @@ import android.os.Build;
|
|
|
14
14
|
|
|
15
15
|
import androidx.annotation.RequiresApi;
|
|
16
16
|
|
|
17
|
-
import com.
|
|
18
|
-
import com.
|
|
19
|
-
import com.
|
|
17
|
+
import com.reactnativecompressor.Video.videoslimmer.VideoSlimmer;
|
|
18
|
+
import com.reactnativecompressor.Video.videoslimmer.listner.SlimProgressListener;
|
|
19
|
+
import com.reactnativecompressor.Video.videoslimmer.muxer.CodecInputSurface;
|
|
20
20
|
|
|
21
21
|
import java.io.File;
|
|
22
22
|
import java.io.IOException;
|
|
@@ -23,7 +23,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
|
23
23
|
import com.reactnativecompressor.Image.ImageCompressor;
|
|
24
24
|
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
25
25
|
import com.reactnativecompressor.Video.VideoCompressorHelper;
|
|
26
|
-
import com.
|
|
26
|
+
import com.reactnativecompressor.Video.videoslimmer.VideoSlimmer;
|
|
27
27
|
|
|
28
28
|
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
29
29
|
import java.io.ByteArrayOutputStream;
|
|
@@ -110,6 +110,10 @@ public class CompressorModule extends ReactContextBaseJavaModule {
|
|
|
110
110
|
Log.d("nomi onFinish", "onProgress: ");
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
@Override
|
|
114
|
+
public void onError(String errorMessage) {
|
|
115
|
+
|
|
116
|
+
}
|
|
113
117
|
|
|
114
118
|
@Override
|
|
115
119
|
public void onProgress(float percent) {
|
|
@@ -1,15 +1,89 @@
|
|
|
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;
|
|
8
|
+
import com.facebook.react.bridge.ReactContext;
|
|
9
|
+
import com.facebook.react.bridge.WritableMap;
|
|
10
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
11
|
+
import com.reactnativecompressor.Video.videoslimmer.VideoSlimTask;
|
|
12
|
+
import com.reactnativecompressor.Video.videoslimmer.VideoSlimmer;
|
|
4
13
|
|
|
5
14
|
import java.io.File;
|
|
15
|
+
import java.util.HashMap;
|
|
16
|
+
import java.util.Map;
|
|
6
17
|
import java.util.UUID;
|
|
7
18
|
|
|
8
19
|
public class Utils {
|
|
20
|
+
static int videoCompressionThreshold=10;
|
|
21
|
+
static int currentVideoCompression=0;
|
|
22
|
+
static Map<String, VideoSlimTask> compressorExports = new HashMap<>();
|
|
23
|
+
|
|
9
24
|
public static String generateCacheFilePath(String extension, ReactApplicationContext reactContext){
|
|
10
25
|
File outputDir = reactContext.getCacheDir();
|
|
11
26
|
|
|
12
27
|
String outputUri = String.format("%s/%s." + extension, outputDir.getPath(), UUID.randomUUID().toString());
|
|
13
28
|
return outputUri;
|
|
14
29
|
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
public static void compressVideo(String srcPath, String destinationPath, int resultWidth, int resultHeight, float videoBitRate, String uuid, Promise promise, ReactApplicationContext reactContext){
|
|
33
|
+
try{
|
|
34
|
+
VideoSlimTask export=VideoSlimmer.convertVideo(srcPath, destinationPath, resultWidth, resultHeight, (int) videoBitRate, new VideoSlimmer.ProgressListener() {
|
|
35
|
+
@Override
|
|
36
|
+
public void onStart() {
|
|
37
|
+
//convert start
|
|
38
|
+
}
|
|
39
|
+
@Override
|
|
40
|
+
public void onFinish(boolean result) {
|
|
41
|
+
//convert finish,result(true is success,false is fail)
|
|
42
|
+
promise.resolve("file:/"+destinationPath);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@Override
|
|
46
|
+
public void onError(String errorMessage) {
|
|
47
|
+
promise.resolve(srcPath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Override
|
|
51
|
+
public void onProgress(float percent) {
|
|
52
|
+
int roundProgress=Math.round(percent);
|
|
53
|
+
if(roundProgress%videoCompressionThreshold==0&&roundProgress>currentVideoCompression) {
|
|
54
|
+
WritableMap params = Arguments.createMap();
|
|
55
|
+
WritableMap data = Arguments.createMap();
|
|
56
|
+
params.putString("uuid", uuid);
|
|
57
|
+
data.putDouble("progress", percent / 100);
|
|
58
|
+
params.putMap("data", data);
|
|
59
|
+
sendEvent(reactContext, "videoCompressProgress", params);
|
|
60
|
+
currentVideoCompression=roundProgress;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
compressorExports.put(uuid, export);
|
|
65
|
+
} catch (Exception ex) {
|
|
66
|
+
promise.reject(ex);
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
currentVideoCompression=0;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public static void cancelCompressionHelper(String uuid){
|
|
74
|
+
try{
|
|
75
|
+
VideoSlimTask export=compressorExports.get(uuid);
|
|
76
|
+
export.cancel(true);
|
|
77
|
+
}
|
|
78
|
+
catch (Exception ex) {
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private static void sendEvent(ReactContext reactContext,
|
|
83
|
+
String eventName,
|
|
84
|
+
@Nullable WritableMap params) {
|
|
85
|
+
reactContext
|
|
86
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
87
|
+
.emit(eventName, params);
|
|
88
|
+
}
|
|
15
89
|
}
|
|
Binary file
|
|
@@ -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,14 +14,11 @@ 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
23
|
public class VideoModule extends ReactContextBaseJavaModule {
|
|
29
24
|
public static final String NAME = "VideoCompressor";
|
|
@@ -67,6 +62,13 @@ public class VideoModule extends ReactContextBaseJavaModule {
|
|
|
67
62
|
|
|
68
63
|
}
|
|
69
64
|
|
|
65
|
+
@ReactMethod
|
|
66
|
+
public void cancelCompression(
|
|
67
|
+
String uuid) {
|
|
68
|
+
cancelCompressionHelper(uuid);
|
|
69
|
+
Log.d("cancelCompression", uuid);
|
|
70
|
+
}
|
|
71
|
+
|
|
70
72
|
@ReactMethod
|
|
71
73
|
public void upload(
|
|
72
74
|
String fileUrl,
|