Skip to content

Commit 3736ddc

Browse files
committed
chore: add minimumFileSizeForCompress for customize minimum video file to compress
1 parent 59b2caf commit 3736ddc

File tree

11 files changed

+99
-15
lines changed

11 files changed

+99
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ const result = await Audio.compress(
204204
The maximum size can be height in case of portrait video or can be width in case of landscape video.
205205

206206
- ###### `bitrate: string`
207+
207208
bitrate of video which reduce or increase video size. if compressionMethod will auto then this prop will not work
208209

210+
- ###### `minimumFileSizeForCompress: number` (default: 16)
211+
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)
212+
209213
## Audio
210214

211215
- ###### `compress(url: string, options?: audioCompresssionType): Promise<string>`

android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public class AutoVideoCompression {
2626
static int currentVideoCompression=0;
2727

2828
public static void createCompressionSettings(String fileUrl,VideoCompressorHelper options,Promise promise, ReactApplicationContext reactContext) {
29-
float maxSize = options.maxSize;;
29+
float maxSize = options.maxSize;
30+
float minimumFileSizeForCompress=options.minimumFileSizeForCompress;
3031
try{
3132
Uri uri= Uri.parse(fileUrl);
3233
String srcPath = uri.getPath();
@@ -35,7 +36,7 @@ public static void createCompressionSettings(String fileUrl,VideoCompressorHelpe
3536
File file=new File(srcPath);
3637
float sizeInBytes = file.length();
3738
float sizeInMb = sizeInBytes / (1024 * 1024);
38-
if(sizeInMb>16)
39+
if(sizeInMb>minimumFileSizeForCompress)
3940
{
4041
String destinationPath = generateCacheFilePath("mp4", reactContext);
4142
int actualHeight =Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));

android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public enum CompressionMethod {
102102
public float bitrate = 0;
103103
public String uuid = "";
104104
public float maxSize = 640.0f;
105+
public float minimumFileSizeForCompress = 16.0f;
105106

106107
public static VideoCompressorHelper fromMap(ReadableMap map) {
107108
final VideoCompressorHelper options = new VideoCompressorHelper();
@@ -120,6 +121,9 @@ public static VideoCompressorHelper fromMap(ReadableMap map) {
120121
case "uuid":
121122
options.uuid = map.getString(key);
122123
break;
124+
case "minimumFileSizeForCompress":
125+
options.minimumFileSizeForCompress =(float) map.getDouble(key);
126+
break;
123127

124128
}
125129
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

example/ios/Podfile.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,24 @@ PODS:
282282
- React-jsinspector (0.64.1)
283283
- react-native-background-upload (6.2.4):
284284
- React
285+
<<<<<<< HEAD
285286
- react-native-compressor (0.5.4):
287+
=======
288+
- react-native-compressor (1.0.3):
289+
>>>>>>> e12330f... feat: add minimumFileSizeForCompress for customize minimum video file to compress
286290
- NextLevelSessionExporter
287291
- React-Core
288292
- react-native-create-thumbnail (1.4.1):
289293
- React
290294
- react-native-document-picker (5.0.4):
291295
- React-Core
296+
<<<<<<< HEAD
292297
- react-native-image-picker (4.0.3):
298+
=======
299+
- react-native-get-random-values (1.7.1):
300+
- React-Core
301+
- react-native-image-picker (4.1.2):
302+
>>>>>>> e12330f... feat: add minimumFileSizeForCompress for customize minimum video file to compress
293303
- React-Core
294304
- react-native-safe-area-context (3.2.0):
295305
- React-Core
@@ -443,6 +453,7 @@ DEPENDENCIES:
443453
- react-native-compressor (from `../..`)
444454
- react-native-create-thumbnail (from `../node_modules/react-native-create-thumbnail`)
445455
- react-native-document-picker (from `../node_modules/react-native-document-picker`)
456+
- react-native-get-random-values (from `../node_modules/react-native-get-random-values`)
446457
- react-native-image-picker (from `../node_modules/react-native-image-picker`)
447458
- react-native-safe-area-context (from `../node_modules/react-native-safe-area-context`)
448459
- React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`)
@@ -520,6 +531,8 @@ EXTERNAL SOURCES:
520531
:path: "../node_modules/react-native-create-thumbnail"
521532
react-native-document-picker:
522533
:path: "../node_modules/react-native-document-picker"
534+
react-native-get-random-values:
535+
:path: "../node_modules/react-native-get-random-values"
523536
react-native-image-picker:
524537
:path: "../node_modules/react-native-image-picker"
525538
react-native-safe-area-context:
@@ -579,6 +592,7 @@ SPEC CHECKSUMS:
579592
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
580593
NextLevelSessionExporter: 4d8aa5e617f1c709724f2453efe5d4628480f65a
581594
OpenSSL-Universal: 1aa4f6a6ee7256b83db99ec1ccdaa80d10f9af9b
595+
<<<<<<< HEAD
582596
RCT-Folly: ec7a233ccc97cc556cf7237f0db1ff65b986f27c
583597
RCTRequired: ec2ebc96b7bfba3ca5c32740f5a0c6a014a274d2
584598
RCTTypeSafety: 22567f31e67c3e088c7ac23ea46ab6d4779c0ea5
@@ -608,6 +622,40 @@ SPEC CHECKSUMS:
608622
React-RCTVibration: 4b99a7f5c6c0abbc5256410cc5425fb8531986e1
609623
React-runtimeexecutor: ff951a0c241bfaefc4940a3f1f1a229e7cb32fa6
610624
ReactCommon: bedc99ed4dae329c4fcf128d0c31b9115e5365ca
625+
=======
626+
RCT-Folly: a21c126816d8025b547704b777a2ba552f3d9fa9
627+
RCTRequired: 3cc065b52aa18db729268b9bd78a2feffb4d0f91
628+
RCTTypeSafety: 3c4fc37d5dea452d2ef17324db5504ec2f05083a
629+
React: 4a00720816c52a213424442954acb7e4b724804a
630+
React-callinvoker: 911fc6570538f3bb5c61edf9dc907c1beb4355bf
631+
React-Core: e134d3a5d7b2a1a731589be776e20dbb14868f27
632+
React-CoreModules: 2f8588b2aa47e7fef27125c8eaaabda963b3ac62
633+
React-cxxreact: 8f1382538cad0cc8b8eafca6d66268828e353bea
634+
React-hermes: 503642bd7704c60dcd1385c2da9607eac5260077
635+
React-jsi: 9fe1854d2c0486216acebd5db3c38b4ccb23ca0b
636+
React-jsiexecutor: db2f6e22a534d466fc0e34e622df47d9d20bab2f
637+
React-jsinspector: 8c0517dee5e8c70cd6c3066f20213ff7ce54f176
638+
React-logger: bfddd3418dc1d45b77b822958f3e31422e2c179b
639+
react-native-background-upload: 994d4e8abbcc1d0b0cff68d950429906ed68ca67
640+
react-native-compressor: fd8cc4b27fcd00e5e784b4d8d00583075326811f
641+
react-native-create-thumbnail: d31c84dd69fb4d60aec0ff2f49bc0de41ede7b2c
642+
react-native-document-picker: 226068006be4c52f8ef7f32d855ee025a12e9e64
643+
react-native-get-random-values: 2c4ff6b44cb71291dabe9a8ae87d3877dcf387da
644+
react-native-image-picker: 9c8a2687b69300ad9e95cec5d38f35ab9d32467d
645+
react-native-safe-area-context: 584dc04881deb49474363f3be89e4ca0e854c057
646+
React-perflogger: fcac6090a80e3d967791b4c7f1b1a017f9d4a398
647+
React-RCTActionSheet: caf5913d9f9e605f5467206cf9d1caa6d47d7ad6
648+
React-RCTAnimation: 6539e3bf594f6a529cd861985ba6548286ae1ead
649+
React-RCTBlob: 6e2e999d28b15fd03ed533f164ce33e0fcde571a
650+
React-RCTImage: c6bbb10eedb6b840c4474f2108b864173b83de15
651+
React-RCTLinking: 8fda9bb8fdb104e78110a903a9a77754318c7d11
652+
React-RCTNetwork: 2b26daad93830501cf14aab03eac04e304f942d3
653+
React-RCTSettings: 89c0dcee7adb706c749383596f57c1e882a27843
654+
React-RCTText: 71734fce8e6cb854daeb4a5eec182c303ea58473
655+
React-RCTVibration: 6600b5eed7c0fda4a433fa1198d1cb2690151791
656+
React-runtimeexecutor: 33a949a51bec5f8a3c9e8d8092deb259600d761e
657+
ReactCommon: 620442811dc6f707b4bf5e3b27d4f19c12d5a821
658+
>>>>>>> e12330f... feat: add minimumFileSizeForCompress for customize minimum video file to compress
611659
RNCMaskedView: 0e1bc4bfa8365eba5fbbb71e07fbdc0555249489
612660
RNFS: 3ab21fa6c56d65566d1fb26c2228e2b6132e5e32
613661
RNGestureHandler: a479ebd5ed4221a810967000735517df0d2db211

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"react-native": "0.64.1",
1818
"react-native-background-upload": "^6.2.4",
1919
"react-native-create-thumbnail": "^1.4.1",
20-
"react-native-document-picker": "^5.0.4",
20+
"react-native-document-picker": "^7.1.2",
2121
"react-native-fs": "^2.18.0",
2222
"react-native-gesture-handler": "^1.10.3",
23+
"react-native-get-random-values": "^1.7.1",
2324
"react-native-image-picker": "^4.0.3",
2425
"react-native-progress": "^4.1.2",
2526
"react-native-reanimated": "^2.2.0",

example/src/Screens/Video/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export default function App() {
112112
sourceVideo,
113113
{
114114
compressionMethod: 'auto',
115+
minimumFileSizeForCompress: 16,
115116
},
116117
(progress) => {
117118
if (backgroundMode) {

example/yarn.lock

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,11 @@ extglob@^2.0.4:
18461846
snapdragon "^0.8.1"
18471847
to-regex "^3.0.1"
18481848

1849+
fast-base64-decode@^1.0.0:
1850+
version "1.0.0"
1851+
resolved "https://registry.yarnpkg.com/fast-base64-decode/-/fast-base64-decode-1.0.0.tgz#b434a0dd7d92b12b43f26819300d2dafb83ee418"
1852+
integrity sha512-qwaScUgUGBYeDNRnbc/KyllVU88Jk1pRHPStuF/lO7B0/RTRLj7U0lkdTAutlBblY08rwZDff6tNU9cjv6j//Q==
1853+
18491854
fb-watchman@^2.0.0:
18501855
version "2.0.1"
18511856
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.1.tgz#fc84fb39d2709cf3ff6d743706157bb5708a8a85"
@@ -3437,10 +3442,12 @@ react-native-create-thumbnail@^1.4.1:
34373442
resolved "https://registry.yarnpkg.com/react-native-create-thumbnail/-/react-native-create-thumbnail-1.4.1.tgz#bd5d6c23a73373602b6795901ac3f78ea4ad8a53"
34383443
integrity sha512-JcxVsoA5u53xilWn67AvRVVtqR8Pz21uCSBxwlmqeqByecsPw414eqrnbfD1NUy6CzBVJXYmyq3hljUUV0pGCw==
34393444

3440-
react-native-document-picker@^5.0.4:
3441-
version "5.0.4"
3442-
resolved "https://registry.yarnpkg.com/react-native-document-picker/-/react-native-document-picker-5.0.4.tgz#86a874e093284b21896581985b8b6e13e04a4251"
3443-
integrity sha512-PN3NYZFgz6McbcfH5Cu3yf3nE4UBAiJYdL26F5cjypOoCFhdfLz0NM13V/tVOLGhWZXNUmI3g4dhPQBthq+pJg==
3445+
react-native-document-picker@^7.1.2:
3446+
version "7.1.2"
3447+
resolved "https://registry.yarnpkg.com/react-native-document-picker/-/react-native-document-picker-7.1.2.tgz#99c11a166c65f0fc4184f14067b7ea7a088252ed"
3448+
integrity sha512-9lVkvv37H2axi69t7IWhDBEEqF/sLXoIS0v3ROCgpcVRY0dyo+XZO5nBGUZtoBnqxiYBEoXDfWtCBmCU8h0/1Q==
3449+
dependencies:
3450+
invariant "^2.2.4"
34443451

34453452
react-native-fs@^2.18.0:
34463453
version "2.18.0"
@@ -3461,6 +3468,13 @@ react-native-gesture-handler@^1.10.3:
34613468
invariant "^2.2.4"
34623469
prop-types "^15.7.2"
34633470

3471+
react-native-get-random-values@^1.7.1:
3472+
version "1.7.1"
3473+
resolved "https://registry.yarnpkg.com/react-native-get-random-values/-/react-native-get-random-values-1.7.1.tgz#6dbeff54c7528763f209e0e7c991f1c78dcc6801"
3474+
integrity sha512-VXWdk73N7pQMEAcM9pvZv+yuWmu2qQrS0QldhdgYmo7dmPHb3x/7tvQKDxkR9gy9/+SoS2uQq9eq06sZ200piA==
3475+
dependencies:
3476+
fast-base64-decode "^1.0.0"
3477+
34643478
react-native-image-picker@^4.0.3:
34653479
version "4.0.3"
34663480
resolved "https://registry.yarnpkg.com/react-native-image-picker/-/react-native-image-picker-4.0.3.tgz#dd8d393e0c57321688885c74b52244ad36e532d6"

ios/Video/VideoCompressor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,13 @@ func makeValidUri(filePath: String) -> String {
199199

200200

201201
func compressVideo(url: URL, options: [String: Any], onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
202-
202+
var minimumFileSizeForCompress:Double=16.0;
203203
let fileSize=self.getfileSize(forURL: url);
204-
205-
if(fileSize>16)
204+
if((options["minimumFileSizeForCompress"]) != nil)
205+
{
206+
minimumFileSizeForCompress=options["minimumFileSizeForCompress"] as! Double;
207+
}
208+
if(fileSize>minimumFileSizeForCompress)
206209
{
207210
if(options["compressionMethod"] as! String=="auto")
208211
{
@@ -379,14 +382,11 @@ func makeValidUri(filePath: String) -> String {
379382
onCompletion(exporter.outputURL!)
380383
break
381384
default:
382-
// let error = CompressionError(message: "Compression didn't complete")
383-
// onFailure(error)
384385
onCompletion(url)
385386
break
386387
}
387388
break
388389
case .failure(let error):
389-
// onFailure(error)
390390
onCompletion(url)
391391
break
392392
}

src/Video/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type compressionMethod = 'auto' | 'manual';
1414
type videoCompresssionType = {
1515
bitrate?: number;
1616
compressionMethod?: compressionMethod;
17+
minimumFileSizeForCompress?: number;
1718
};
1819

1920
export declare enum FileSystemSessionType {
@@ -72,6 +73,7 @@ const Video: VideoCompressorType = {
7273
bitrate?: number;
7374
compressionMethod?: compressionMethod;
7475
maxSize?: number;
76+
minimumFileSizeForCompress?: number;
7577
},
7678
onProgress?: (progress: number) => void
7779
) => {
@@ -93,6 +95,7 @@ const Video: VideoCompressorType = {
9395
bitrate?: number;
9496
compressionMethod?: compressionMethod;
9597
maxSize?: number;
98+
minimumFileSizeForCompress?: number;
9699
} = { uuid };
97100
if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;
98101
if (options?.compressionMethod) {
@@ -105,6 +108,10 @@ const Video: VideoCompressorType = {
105108
} else {
106109
modifiedOptions.maxSize = 640;
107110
}
111+
if (options?.minimumFileSizeForCompress) {
112+
modifiedOptions.minimumFileSizeForCompress =
113+
options?.minimumFileSizeForCompress;
114+
}
108115
const result = await NativeVideoCompressor.compress(
109116
fileUrl,
110117
modifiedOptions

0 commit comments

Comments
 (0)