Skip to content

Commit 163ff05

Browse files
committed
feat: add threshhold of 3% in video compression progress
1 parent 7986c10 commit 163ff05

File tree

5 files changed

+44
-29
lines changed

5 files changed

+44
-29
lines changed

android/src/main/java/com/reactnativecompressor/CompressorModule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.graphics.Bitmap;
44
import android.media.MediaMetadataRetriever;
5+
import android.net.Uri;
56
import android.os.Build;
67
import android.util.Log;
78

@@ -83,10 +84,8 @@ public void compress_audio(
8384
Promise promise) {
8485
try{
8586
final VideoCompressorHelper options = VideoCompressorHelper.fromMap(optionMap);
86-
String srcPath = fileUrl;
87-
if (srcPath.indexOf("file://") > -1) {
88-
srcPath = srcPath.substring(srcPath.indexOf(':') + 1);
89-
}
87+
Uri uri= Uri.parse(fileUrl);
88+
String srcPath = uri.getPath();
9089
String destinationPath = generateCacheFilePath("mp3", reactContext);
9190

9291
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.reactnativecompressor.Video;
22

33
import android.media.MediaMetadataRetriever;
4+
import android.net.Uri;
45

56
import androidx.annotation.NonNull;
67
import androidx.annotation.Nullable;
@@ -24,6 +25,8 @@
2425

2526
@ReactModule(name = VideoModule.NAME)
2627
public class VideoModule extends ReactContextBaseJavaModule {
28+
int videoCompressionThreshold=3;
29+
int currentVideoCompression=0;
2730
public static final String NAME = "VideoCompressor";
2831
private final ReactApplicationContext reactContext;
2932
public VideoModule(ReactApplicationContext reactContext) {
@@ -53,25 +56,22 @@ public void compress(
5356
Promise promise) {
5457
try{
5558
final VideoCompressorHelper options = VideoCompressorHelper.fromMap(optionMap);
56-
String srcPath = fileUrl;
57-
if (srcPath.indexOf("file://") > -1) {
58-
srcPath = srcPath.substring(srcPath.indexOf(':') + 1);
59-
}
60-
String destinationPath = generateCacheFilePath("mp4", reactContext);
61-
59+
Uri uri= Uri.parse(fileUrl);
60+
String srcPath = uri.getPath();
61+
String destinationPath = generateCacheFilePath("mp4", reactContext);
6262
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
6363
metaRetriever.setDataSource(srcPath);
6464
int height =Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
6565
int width = Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
6666

6767
boolean isPortrait = height > width;
68-
float maxSize = 1920;
68+
int maxSize = 1920;
6969
if(isPortrait && height > maxSize){
70-
width = (1920/height)*width;
71-
height = 1920;
70+
width = (int) (((float)maxSize/height)*width);
71+
height = maxSize;
7272
}else if(width > maxSize){
73-
height = (1920/width)*height;
74-
width = 1920;
73+
height = (int) (((float)maxSize/width)*height);
74+
width = maxSize;
7575
}
7676

7777
float videoBitRate = (float) (height * width * 1.5);
@@ -94,18 +94,25 @@ public void onFinish(boolean result) {
9494

9595
@Override
9696
public void onProgress(float percent) {
97-
WritableMap params = Arguments.createMap();
98-
WritableMap data = Arguments.createMap();
99-
params.putString("uuid", options.uuid);
100-
data.putDouble("progress", percent/100);
101-
params.putMap("data", data);
102-
sendEvent(reactContext, "videoCompressProgress", params);
97+
int roundProgress=Math.round(percent);
98+
if(roundProgress%videoCompressionThreshold==0&&roundProgress>currentVideoCompression) {
99+
WritableMap params = Arguments.createMap();
100+
WritableMap data = Arguments.createMap();
101+
params.putString("uuid", options.uuid);
102+
data.putDouble("progress", percent / 100);
103+
params.putMap("data", data);
104+
sendEvent(reactContext, "videoCompressProgress", params);
105+
currentVideoCompression=roundProgress;
106+
}
103107
}
104108
});
105109

106110
} catch (Exception ex) {
107111
promise.reject(ex);
108112
}
113+
finally {
114+
currentVideoCompression=0;
115+
}
109116
}
110117

111118
@ReactMethod

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ PODS:
282282
- React-jsinspector (0.64.1)
283283
- react-native-background-upload (6.2.4):
284284
- React
285-
- react-native-compressor (0.0.1):
285+
- react-native-compressor (0.2.0):
286286
- NextLevelSessionExporter
287287
- React-Core
288288
- react-native-create-thumbnail (1.4.1):
@@ -591,7 +591,7 @@ SPEC CHECKSUMS:
591591
React-jsiexecutor: 124e8f99992490d0d13e0649d950d3e1aae06fe9
592592
React-jsinspector: 500a59626037be5b3b3d89c5151bc3baa9abf1a9
593593
react-native-background-upload: b85fac36cfee561c8b3a282e5700e50dc98249c5
594-
react-native-compressor: 597416a9ae6b7446e6795ccead336433b0d88745
594+
react-native-compressor: 8ab48b7bc33b8a3c2b19b31681bd58e4ff359e27
595595
react-native-create-thumbnail: d31c84dd69fb4d60aec0ff2f49bc0de41ede7b2c
596596
react-native-document-picker: 1a7518132d4a06b67f459be9bb1464a567d2b3b4
597597
react-native-image-picker: 474cf2c33c2b6671da53d293a16c97995f0aec15

example/src/Screens/Video/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function App() {
4949
})
5050
.then((response) => setcompressedVideoThumbnail(response.path))
5151
.catch((error) => {
52-
console.log({ error });
52+
console.log({ errorThumnail: error });
5353
setcompressedVideoThumbnail(sourceVideoThumbnail);
5454
});
5555

ios/Video/VideoCompressor.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
3434
var hasListener: Bool=false
3535
var uploadResolvers: [String: RCTPromiseResolveBlock] = [:]
3636
var uploadRejectors: [String: RCTPromiseRejectBlock] = [:]
37+
let videoCompressionThreshold:Int=3
38+
var videoCompressionCounter:Int=0
3739

3840
override static func requiresMainQueueSetup() -> Bool {
3941
return false
@@ -197,11 +199,11 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
197199
let isPortrait = height > width
198200
let maxSize = Float(1920);
199201
if(isPortrait && height > maxSize){
200-
width = (1920/height)*width
201-
height = 1920
202+
width = (maxSize/height)*width
203+
height = maxSize
202204
}else if(width > maxSize){
203-
height = (1920/width)*height
204-
width = 1920
205+
height = (maxSize/width)*height
206+
width = maxSize
205207
}
206208

207209
let videoBitRate = bitRate ?? height*width*1.5
@@ -227,8 +229,15 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
227229

228230

229231
exporter.export(progressHandler: { (progress) in
230-
onProgress(progress)
232+
let _progress:Float=progress*100;
233+
if(Int(_progress)==self.videoCompressionCounter)
234+
{
235+
self.videoCompressionCounter=Int(_progress)+self.videoCompressionThreshold
236+
onProgress(progress)
237+
}
238+
231239
}, completionHandler: { result in
240+
self.videoCompressionCounter=0;
232241
switch result {
233242
case .success(let status):
234243
switch status {

0 commit comments

Comments
 (0)