Skip to content

Commit e12330f

Browse files
committed
feat: add minimumFileSizeForCompress for customize minimum video file to compress
1 parent 3b34cca commit e12330f

File tree

10 files changed

+47
-11
lines changed

10 files changed

+47
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,12 @@ const uploadResult = await backgroundUpload(
223223
The maximum size can be height in case of portrait video or can be width in case of landscape video.
224224

225225
- ###### `bitrate: string`
226+
226227
bitrate of video which reduce or increase video size. if compressionMethod will auto then this prop will not work
227228

229+
- ###### `minimumFileSizeForCompress: number` (default: 16)
230+
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)
231+
228232
## Audio
229233

230234
- ###### `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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,15 @@ PODS:
293293
- glog
294294
- react-native-background-upload (6.5.0):
295295
- React
296-
- react-native-compressor (1.0.2):
296+
- react-native-compressor (1.0.3):
297297
- NextLevelSessionExporter
298298
- React-Core
299299
- react-native-create-thumbnail (1.4.1):
300300
- React
301301
- react-native-document-picker (5.3.0):
302302
- React-Core
303+
- react-native-get-random-values (1.7.1):
304+
- React-Core
303305
- react-native-image-picker (4.1.2):
304306
- React-Core
305307
- react-native-safe-area-context (3.3.2):
@@ -459,6 +461,7 @@ DEPENDENCIES:
459461
- react-native-compressor (from `../..`)
460462
- react-native-create-thumbnail (from `../node_modules/react-native-create-thumbnail`)
461463
- react-native-document-picker (from `../node_modules/react-native-document-picker`)
464+
- react-native-get-random-values (from `../node_modules/react-native-get-random-values`)
462465
- react-native-image-picker (from `../node_modules/react-native-image-picker`)
463466
- react-native-safe-area-context (from `../node_modules/react-native-safe-area-context`)
464467
- React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`)
@@ -545,6 +548,8 @@ EXTERNAL SOURCES:
545548
:path: "../node_modules/react-native-create-thumbnail"
546549
react-native-document-picker:
547550
:path: "../node_modules/react-native-document-picker"
551+
react-native-get-random-values:
552+
:path: "../node_modules/react-native-get-random-values"
548553
react-native-image-picker:
549554
:path: "../node_modules/react-native-image-picker"
550555
react-native-safe-area-context:
@@ -622,9 +627,10 @@ SPEC CHECKSUMS:
622627
React-jsinspector: 8c0517dee5e8c70cd6c3066f20213ff7ce54f176
623628
React-logger: bfddd3418dc1d45b77b822958f3e31422e2c179b
624629
react-native-background-upload: 994d4e8abbcc1d0b0cff68d950429906ed68ca67
625-
react-native-compressor: 3eb2118dc1da59c134cdffa721e0b76486d7a404
630+
react-native-compressor: fd8cc4b27fcd00e5e784b4d8d00583075326811f
626631
react-native-create-thumbnail: d31c84dd69fb4d60aec0ff2f49bc0de41ede7b2c
627632
react-native-document-picker: 226068006be4c52f8ef7f32d855ee025a12e9e64
633+
react-native-get-random-values: 2c4ff6b44cb71291dabe9a8ae87d3877dcf387da
628634
react-native-image-picker: 9c8a2687b69300ad9e95cec5d38f35ab9d32467d
629635
react-native-safe-area-context: 584dc04881deb49474363f3be89e4ca0e854c057
630636
React-perflogger: fcac6090a80e3d967791b4c7f1b1a017f9d4a398

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"react-native-document-picker": "^5.0.4",
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.4",

example/src/Screens/Video/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export default function App() {
114114
sourceVideo,
115115
{
116116
compressionMethod: 'auto',
117+
minimumFileSizeForCompress: 16,
117118
},
118119
(progress) => {
119120
if (backgroundMode) {

example/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,11 @@ extglob@^2.0.4:
19151915
snapdragon "^0.8.1"
19161916
to-regex "^3.0.1"
19171917

1918+
fast-base64-decode@^1.0.0:
1919+
version "1.0.0"
1920+
resolved "https://registry.yarnpkg.com/fast-base64-decode/-/fast-base64-decode-1.0.0.tgz#b434a0dd7d92b12b43f26819300d2dafb83ee418"
1921+
integrity sha512-qwaScUgUGBYeDNRnbc/KyllVU88Jk1pRHPStuF/lO7B0/RTRLj7U0lkdTAutlBblY08rwZDff6tNU9cjv6j//Q==
1922+
19181923
fb-watchman@^2.0.0:
19191924
version "2.0.1"
19201925
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.1.tgz#fc84fb39d2709cf3ff6d743706157bb5708a8a85"
@@ -3597,6 +3602,13 @@ react-native-gesture-handler@^1.10.3:
35973602
invariant "^2.2.4"
35983603
prop-types "^15.7.2"
35993604

3605+
react-native-get-random-values@^1.7.1:
3606+
version "1.7.1"
3607+
resolved "https://registry.yarnpkg.com/react-native-get-random-values/-/react-native-get-random-values-1.7.1.tgz#6dbeff54c7528763f209e0e7c991f1c78dcc6801"
3608+
integrity sha512-VXWdk73N7pQMEAcM9pvZv+yuWmu2qQrS0QldhdgYmo7dmPHb3x/7tvQKDxkR9gy9/+SoS2uQq9eq06sZ200piA==
3609+
dependencies:
3610+
fast-base64-decode "^1.0.0"
3611+
36003612
react-native-image-picker@^4.0.3:
36013613
version "4.1.2"
36023614
resolved "https://registry.yarnpkg.com/react-native-image-picker/-/react-native-image-picker-4.1.2.tgz#1b4b4980a6878930675396c23dffbaa802cbbcc6"

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
@@ -21,6 +21,7 @@ type videoCompresssionType = {
2121
bitrate?: number;
2222
maxSize?: number;
2323
compressionMethod?: compressionMethod;
24+
minimumFileSizeForCompress?: number;
2425
};
2526

2627
export declare enum FileSystemSessionType {
@@ -116,6 +117,7 @@ const Video: VideoCompressorType = {
116117
bitrate?: number;
117118
compressionMethod?: compressionMethod;
118119
maxSize?: number;
120+
minimumFileSizeForCompress?: number;
119121
},
120122
onProgress?: (progress: number) => void
121123
) => {
@@ -137,6 +139,7 @@ const Video: VideoCompressorType = {
137139
bitrate?: number;
138140
compressionMethod?: compressionMethod;
139141
maxSize?: number;
142+
minimumFileSizeForCompress?: number;
140143
} = { uuid };
141144
if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;
142145
if (options?.compressionMethod) {
@@ -149,6 +152,10 @@ const Video: VideoCompressorType = {
149152
} else {
150153
modifiedOptions.maxSize = 640;
151154
}
155+
if (options?.minimumFileSizeForCompress) {
156+
modifiedOptions.minimumFileSizeForCompress =
157+
options?.minimumFileSizeForCompress;
158+
}
152159
const result = await NativeVideoCompressor.compress(
153160
fileUrl,
154161
modifiedOptions

0 commit comments

Comments
 (0)