react-native-compressor 1.3.2 → 1.4.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 +60 -2
- package/android/.gradle/6.1.1/executionHistory/executionHistory.lock +0 -0
- package/android/.gradle/6.1.1/fileChanges/last-build.bin +0 -0
- package/android/.gradle/6.1.1/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/6.1.1/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +1 -1
- package/android/.gradle/checksums/checksums.lock +0 -0
- package/android/.gradle/checksums/md5-checksums.bin +0 -0
- package/android/.gradle/checksums/sha1-checksums.bin +0 -0
- package/android/build.gradle +1 -5
- package/android/src/main/java/com/.DS_Store +0 -0
- package/android/src/main/java/com/reactnativecompressor/.DS_Store +0 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.java +0 -1
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +14 -2
- package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.java +210 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +30 -6
- 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 +23 -15
- package/ios/.DS_Store +0 -0
- package/ios/Compressor-Bridging-Header.h +1 -1
- package/ios/Compressor.m +39 -12
- package/ios/Image/ImageCompressor.h +2 -0
- package/ios/Image/ImageCompressor.m +152 -1
- package/ios/Video/VideoCompressor.swift +57 -47
- package/lib/commonjs/Audio/index.js.map +1 -1
- package/lib/commonjs/Video/index.js +13 -2
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/commonjs/index.js +15 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/index.js +12 -4
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/module/Audio/index.js.map +1 -1
- package/lib/module/Video/index.js +9 -1
- package/lib/module/Video/index.js.map +1 -1
- package/lib/module/index.js +5 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/index.js +6 -4
- package/lib/module/utils/index.js.map +1 -1
- package/lib/typescript/Video/index.d.ts +3 -0
- package/lib/typescript/index.d.ts +4 -2
- package/lib/typescript/utils/index.d.ts +2 -0
- package/package.json +2 -1
- package/src/Audio/index.tsx +1 -1
- package/src/Video/index.tsx +12 -7
- package/src/index.tsx +5 -1
- package/src/utils/index.tsx +7 -3
- package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
**react-native-compressor** package is a set of functions that allow you compress `Image`,`Audio` and `Video`
|
|
26
26
|
|
|
27
|
-
If you find this package useful hit the star 🌟
|
|
27
|
+
**If you find this package useful hit the star** 🌟
|
|
28
28
|
|
|
29
29
|
## Installation
|
|
30
30
|
|
|
@@ -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.15
|
|
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>`
|
|
@@ -299,6 +334,29 @@ type FileSystemUploadOptions = (
|
|
|
299
334
|
};
|
|
300
335
|
```
|
|
301
336
|
|
|
337
|
+
# Get Real Path
|
|
338
|
+
if you want to convert
|
|
339
|
+
|
|
340
|
+
- `content://` to `file:///` for android
|
|
341
|
+
- `ph://` to `file:///` for IOS
|
|
342
|
+
|
|
343
|
+
the you can you `getRealPath` function like this
|
|
344
|
+
```js
|
|
345
|
+
import { getRealPath } from 'react-native-compressor';
|
|
346
|
+
|
|
347
|
+
const realPath = await getRealPath(fileUri, 'video'); // file://file_path.extension
|
|
348
|
+
```
|
|
349
|
+
- ###### `getRealPath(path: string, type: string = 'video'|'image')`
|
|
350
|
+
|
|
351
|
+
# Get Temp file Path
|
|
352
|
+
if you wanna make random file path in cache folder then you can use this method like this
|
|
353
|
+
|
|
354
|
+
```js
|
|
355
|
+
import { getRealPath } from 'react-native-compressor';
|
|
356
|
+
|
|
357
|
+
const realPath = await generateFilePath(fileUri, 'video'); // file://file_path.extension
|
|
358
|
+
```
|
|
359
|
+
|
|
302
360
|
## Contributing
|
|
303
361
|
|
|
304
362
|
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
File without changes
|
|
Binary file
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#
|
|
1
|
+
#Thu Jan 27 18:54:59 PKT 2022
|
|
2
2
|
gradle.version=6.9
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/android/build.gradle
CHANGED
|
@@ -56,11 +56,7 @@ 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
|
-
implementation 'com.github.nomi9995:VideoCompressor:
|
|
60
|
+
implementation 'com.github.nomi9995:VideoCompressor:715bcc16e9'
|
|
65
61
|
// implementation project(path: ':videocompressor')
|
|
66
62
|
}
|
|
Binary file
|
|
Binary file
|
|
@@ -20,9 +20,10 @@ import com.facebook.react.module.annotations.ReactModule;
|
|
|
20
20
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
21
21
|
import com.reactnativecompressor.Image.ImageCompressor;
|
|
22
22
|
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
23
|
+
import com.reactnativecompressor.Utils.Utils;
|
|
23
24
|
import com.reactnativecompressor.Video.VideoCompressorHelper;
|
|
24
|
-
|
|
25
25
|
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
26
|
+
|
|
26
27
|
import com.reactnativecompressor.Audio.AudioCompressor;
|
|
27
28
|
|
|
28
29
|
@ReactModule(name = CompressorModule.NAME)
|
|
@@ -55,6 +56,7 @@ public class CompressorModule extends ReactContextBaseJavaModule {
|
|
|
55
56
|
ReadableMap optionMap,
|
|
56
57
|
Promise promise) {
|
|
57
58
|
try {
|
|
59
|
+
imagePath=Utils.getRealPath(imagePath,reactContext);
|
|
58
60
|
final ImageCompressorOptions options = ImageCompressorOptions.fromMap(optionMap);
|
|
59
61
|
|
|
60
62
|
if(options.compressionMethod==ImageCompressorOptions.CompressionMethod.auto)
|
|
@@ -99,7 +101,7 @@ public class CompressorModule extends ReactContextBaseJavaModule {
|
|
|
99
101
|
|
|
100
102
|
//General
|
|
101
103
|
@ReactMethod
|
|
102
|
-
public void
|
|
104
|
+
public void generateFilePath(String extension, Promise promise) {
|
|
103
105
|
try {
|
|
104
106
|
final String outputUri =generateCacheFilePath(extension,reactContext);
|
|
105
107
|
promise.resolve(outputUri);
|
|
@@ -107,4 +109,14 @@ public class CompressorModule extends ReactContextBaseJavaModule {
|
|
|
107
109
|
promise.reject(e);
|
|
108
110
|
}
|
|
109
111
|
}
|
|
112
|
+
|
|
113
|
+
@ReactMethod
|
|
114
|
+
public void getRealPath(String path,String type, Promise promise) {
|
|
115
|
+
try {
|
|
116
|
+
final String realPath =Utils.getRealPath(path,reactContext);
|
|
117
|
+
promise.resolve("file://"+realPath);
|
|
118
|
+
} catch (Exception e) {
|
|
119
|
+
promise.reject(e);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
110
122
|
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
package com.reactnativecompressor.Utils;
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint;
|
|
4
|
+
import android.content.ContentUris;
|
|
5
|
+
import android.content.Context;
|
|
6
|
+
import android.database.Cursor;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.os.Build;
|
|
9
|
+
import android.os.Environment;
|
|
10
|
+
import android.provider.DocumentsContract;
|
|
11
|
+
import android.provider.MediaStore;
|
|
12
|
+
|
|
13
|
+
import androidx.loader.content.CursorLoader;
|
|
14
|
+
|
|
15
|
+
public class RealPathUtil {
|
|
16
|
+
|
|
17
|
+
public static String getRealPath(Context context, Uri fileUri) {
|
|
18
|
+
String realPath;
|
|
19
|
+
// SDK < API11
|
|
20
|
+
if (Build.VERSION.SDK_INT < 11) {
|
|
21
|
+
realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(context, fileUri);
|
|
22
|
+
}
|
|
23
|
+
// SDK >= 11 && SDK < 19
|
|
24
|
+
else if (Build.VERSION.SDK_INT < 19) {
|
|
25
|
+
realPath = RealPathUtil.getRealPathFromURI_API11to18(context, fileUri);
|
|
26
|
+
}
|
|
27
|
+
// SDK > 19 (Android 4.4) and up
|
|
28
|
+
else {
|
|
29
|
+
realPath = RealPathUtil.getRealPathFromURI_API19(context, fileUri);
|
|
30
|
+
}
|
|
31
|
+
return realPath;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@SuppressLint("NewApi")
|
|
36
|
+
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
|
|
37
|
+
String[] proj = {MediaStore.Images.Media.DATA};
|
|
38
|
+
String result = null;
|
|
39
|
+
|
|
40
|
+
CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
|
|
41
|
+
Cursor cursor = cursorLoader.loadInBackground();
|
|
42
|
+
|
|
43
|
+
if (cursor != null) {
|
|
44
|
+
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
45
|
+
cursor.moveToFirst();
|
|
46
|
+
result = cursor.getString(column_index);
|
|
47
|
+
cursor.close();
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
|
|
53
|
+
String[] proj = {MediaStore.Images.Media.DATA};
|
|
54
|
+
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
|
|
55
|
+
int column_index = 0;
|
|
56
|
+
String result = "";
|
|
57
|
+
if (cursor != null) {
|
|
58
|
+
column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
59
|
+
cursor.moveToFirst();
|
|
60
|
+
result = cursor.getString(column_index);
|
|
61
|
+
cursor.close();
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get a file path from a Uri. This will get the the path for Storage Access
|
|
69
|
+
* Framework Documents, as well as the _data field for the MediaStore and
|
|
70
|
+
* other file-based ContentProviders.
|
|
71
|
+
*
|
|
72
|
+
* @param context The context.
|
|
73
|
+
* @param uri The Uri to query.
|
|
74
|
+
* @author paulburke
|
|
75
|
+
*/
|
|
76
|
+
@SuppressLint("NewApi")
|
|
77
|
+
public static String getRealPathFromURI_API19(final Context context, final Uri uri) {
|
|
78
|
+
|
|
79
|
+
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
|
80
|
+
|
|
81
|
+
// DocumentProvider
|
|
82
|
+
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
|
|
83
|
+
// ExternalStorageProvider
|
|
84
|
+
if (isExternalStorageDocument(uri)) {
|
|
85
|
+
final String docId = DocumentsContract.getDocumentId(uri);
|
|
86
|
+
final String[] split = docId.split(":");
|
|
87
|
+
final String type = split[0];
|
|
88
|
+
|
|
89
|
+
if ("primary".equalsIgnoreCase(type)) {
|
|
90
|
+
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// TODO handle non-primary volumes
|
|
94
|
+
}
|
|
95
|
+
// DownloadsProvider
|
|
96
|
+
else if (isDownloadsDocument(uri)) {
|
|
97
|
+
|
|
98
|
+
final String id = DocumentsContract.getDocumentId(uri);
|
|
99
|
+
final Uri contentUri = ContentUris.withAppendedId(
|
|
100
|
+
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
|
|
101
|
+
|
|
102
|
+
return getDataColumn(context, contentUri, null, null);
|
|
103
|
+
}
|
|
104
|
+
// MediaProvider
|
|
105
|
+
else if (isMediaDocument(uri)) {
|
|
106
|
+
final String docId = DocumentsContract.getDocumentId(uri);
|
|
107
|
+
final String[] split = docId.split(":");
|
|
108
|
+
final String type = split[0];
|
|
109
|
+
|
|
110
|
+
Uri contentUri = null;
|
|
111
|
+
if ("image".equals(type)) {
|
|
112
|
+
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
113
|
+
} else if ("video".equals(type)) {
|
|
114
|
+
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
|
115
|
+
} else if ("audio".equals(type)) {
|
|
116
|
+
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
final String selection = "_id=?";
|
|
120
|
+
final String[] selectionArgs = new String[]{
|
|
121
|
+
split[1]
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return getDataColumn(context, contentUri, selection, selectionArgs);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// MediaStore (and general)
|
|
128
|
+
else if ("content".equalsIgnoreCase(uri.getScheme())) {
|
|
129
|
+
|
|
130
|
+
// Return the remote address
|
|
131
|
+
if (isGooglePhotosUri(uri))
|
|
132
|
+
return uri.getLastPathSegment();
|
|
133
|
+
|
|
134
|
+
return getDataColumn(context, uri, null, null);
|
|
135
|
+
}
|
|
136
|
+
// File
|
|
137
|
+
else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
|
138
|
+
return uri.getPath();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Get the value of the data column for this Uri. This is useful for
|
|
146
|
+
* MediaStore Uris, and other file-based ContentProviders.
|
|
147
|
+
*
|
|
148
|
+
* @param context The context.
|
|
149
|
+
* @param uri The Uri to query.
|
|
150
|
+
* @param selection (Optional) Filter used in the query.
|
|
151
|
+
* @param selectionArgs (Optional) Selection arguments used in the query.
|
|
152
|
+
* @return The value of the _data column, which is typically a file path.
|
|
153
|
+
*/
|
|
154
|
+
public static String getDataColumn(Context context, Uri uri, String selection,
|
|
155
|
+
String[] selectionArgs) {
|
|
156
|
+
|
|
157
|
+
Cursor cursor = null;
|
|
158
|
+
final String column = "_data";
|
|
159
|
+
final String[] projection = {
|
|
160
|
+
column
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
|
|
165
|
+
null);
|
|
166
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
167
|
+
final int index = cursor.getColumnIndexOrThrow(column);
|
|
168
|
+
return cursor.getString(index);
|
|
169
|
+
}
|
|
170
|
+
} finally {
|
|
171
|
+
if (cursor != null)
|
|
172
|
+
cursor.close();
|
|
173
|
+
}
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param uri The Uri to check.
|
|
180
|
+
* @return Whether the Uri authority is ExternalStorageProvider.
|
|
181
|
+
*/
|
|
182
|
+
public static boolean isExternalStorageDocument(Uri uri) {
|
|
183
|
+
return "com.android.externalstorage.documents".equals(uri.getAuthority());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param uri The Uri to check.
|
|
188
|
+
* @return Whether the Uri authority is DownloadsProvider.
|
|
189
|
+
*/
|
|
190
|
+
public static boolean isDownloadsDocument(Uri uri) {
|
|
191
|
+
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param uri The Uri to check.
|
|
196
|
+
* @return Whether the Uri authority is MediaProvider.
|
|
197
|
+
*/
|
|
198
|
+
public static boolean isMediaDocument(Uri uri) {
|
|
199
|
+
return "com.android.providers.media.documents".equals(uri.getAuthority());
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param uri The Uri to check.
|
|
204
|
+
* @return Whether the Uri authority is Google Photos.
|
|
205
|
+
*/
|
|
206
|
+
public static boolean isGooglePhotosUri(Uri uri) {
|
|
207
|
+
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
package com.reactnativecompressor.Utils;
|
|
2
2
|
|
|
3
|
+
import android.net.Uri;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
|
|
3
6
|
import androidx.annotation.Nullable;
|
|
4
7
|
|
|
5
8
|
import com.facebook.react.bridge.Arguments;
|
|
@@ -10,7 +13,6 @@ import com.facebook.react.bridge.WritableMap;
|
|
|
10
13
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
11
14
|
import numan.dev.videocompressor.VideoCompressTask;
|
|
12
15
|
import numan.dev.videocompressor.VideoCompressor;
|
|
13
|
-
|
|
14
16
|
import java.io.File;
|
|
15
17
|
import java.util.HashMap;
|
|
16
18
|
import java.util.Map;
|
|
@@ -18,7 +20,7 @@ import java.util.UUID;
|
|
|
18
20
|
|
|
19
21
|
public class Utils {
|
|
20
22
|
static int videoCompressionThreshold=10;
|
|
21
|
-
static
|
|
23
|
+
private static final String TAG = "react-native-compessor";
|
|
22
24
|
static Map<String, VideoCompressTask> compressorExports = new HashMap<>();
|
|
23
25
|
|
|
24
26
|
public static String generateCacheFilePath(String extension, ReactApplicationContext reactContext){
|
|
@@ -30,6 +32,7 @@ public class Utils {
|
|
|
30
32
|
|
|
31
33
|
|
|
32
34
|
public static void compressVideo(String srcPath, String destinationPath, int resultWidth, int resultHeight, float videoBitRate, String uuid, Promise promise, ReactApplicationContext reactContext){
|
|
35
|
+
final int[] currentVideoCompression = {0};
|
|
33
36
|
try{
|
|
34
37
|
VideoCompressTask export=VideoCompressor.convertVideo(srcPath, destinationPath, resultWidth, resultHeight, (int) videoBitRate, new VideoCompressor.ProgressListener() {
|
|
35
38
|
@Override
|
|
@@ -44,20 +47,27 @@ public class Utils {
|
|
|
44
47
|
|
|
45
48
|
@Override
|
|
46
49
|
public void onError(String errorMessage) {
|
|
47
|
-
|
|
50
|
+
if(errorMessage.equals(("class java.lang.AssertionError")))
|
|
51
|
+
{
|
|
52
|
+
promise.resolve(srcPath);
|
|
53
|
+
}
|
|
54
|
+
else
|
|
55
|
+
{
|
|
56
|
+
promise.reject("Compression has canncelled");
|
|
57
|
+
}
|
|
48
58
|
}
|
|
49
59
|
|
|
50
60
|
@Override
|
|
51
61
|
public void onProgress(float percent) {
|
|
52
62
|
int roundProgress=Math.round(percent);
|
|
53
|
-
if(roundProgress%videoCompressionThreshold==0&&roundProgress>currentVideoCompression) {
|
|
63
|
+
if(roundProgress%videoCompressionThreshold==0&&roundProgress> currentVideoCompression[0]) {
|
|
54
64
|
WritableMap params = Arguments.createMap();
|
|
55
65
|
WritableMap data = Arguments.createMap();
|
|
56
66
|
params.putString("uuid", uuid);
|
|
57
67
|
data.putDouble("progress", percent / 100);
|
|
58
68
|
params.putMap("data", data);
|
|
59
69
|
sendEvent(reactContext, "videoCompressProgress", params);
|
|
60
|
-
currentVideoCompression=roundProgress;
|
|
70
|
+
currentVideoCompression[0] =roundProgress;
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
73
|
});
|
|
@@ -66,7 +76,7 @@ public class Utils {
|
|
|
66
76
|
promise.reject(ex);
|
|
67
77
|
}
|
|
68
78
|
finally {
|
|
69
|
-
currentVideoCompression=0;
|
|
79
|
+
currentVideoCompression[0] =0;
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
|
|
@@ -86,4 +96,18 @@ public class Utils {
|
|
|
86
96
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
87
97
|
.emit(eventName, params);
|
|
88
98
|
}
|
|
99
|
+
|
|
100
|
+
public static String getRealPath(String fileUrl,ReactApplicationContext reactContext){
|
|
101
|
+
if(fileUrl.startsWith("content://"))
|
|
102
|
+
{
|
|
103
|
+
try {
|
|
104
|
+
Uri uri= Uri.parse(fileUrl);
|
|
105
|
+
fileUrl= RealPathUtil.getRealPath(reactContext,uri);
|
|
106
|
+
}
|
|
107
|
+
catch (Exception ex) {
|
|
108
|
+
Log.d(TAG, " Please see this issue: https://github.com/Shobbak/react-native-compressor/issues/25");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return fileUrl;
|
|
112
|
+
}
|
|
89
113
|
}
|
|
@@ -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
|
|