Skip to content

Commit b93cb3d

Browse files
committed
fix: video upload issue
1 parent 66c149a commit b93cb3d

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
@ReactModule(name = VideoModule.NAME)
2727
public class VideoModule extends ReactContextBaseJavaModule {
28-
int videoCompressionThreshold=3;
28+
int videoCompressionThreshold=7;
2929
int currentVideoCompression=0;
3030
public static final String NAME = "VideoCompressor";
3131
private final ReactApplicationContext reactContext;
@@ -63,6 +63,7 @@ public void compress(
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));
66+
int bitrate=Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
6667

6768
boolean isPortrait = height > width;
6869
int maxSize = 1920;
@@ -73,8 +74,13 @@ public void compress(
7374
height = (int) (((float)maxSize/width)*height);
7475
width = maxSize;
7576
}
76-
77-
float videoBitRate = (float) (height * width * 1.5);
77+
else
78+
{
79+
if(options.bitrate==0) {
80+
options.bitrate = (int) (bitrate * 0.8);
81+
}
82+
}
83+
float videoBitRate = (options.bitrate>0)?options.bitrate: (float) (height * width * 1.5);
7884

7985
VideoSlimmer.convertVideo(srcPath, destinationPath, width, height, (int) videoBitRate, new VideoSlimmer.ProgressListener() {
8086

example/ios/CompressorExample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@
573573
buildSettings = {
574574
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
575575
CLANG_ENABLE_MODULES = YES;
576+
CODE_SIGN_IDENTITY = "iPhone Developer";
576577
CODE_SIGN_STYLE = Manual;
577578
CURRENT_PROJECT_VERSION = 1;
578579
DEVELOPMENT_TEAM = 454A869839;
@@ -585,9 +586,9 @@
585586
"-ObjC",
586587
"-lc++",
587588
);
588-
PRODUCT_BUNDLE_IDENTIFIER = com.onshobbak.dev;
589+
PRODUCT_BUNDLE_IDENTIFIER = com.onshobbak.lava;
589590
PRODUCT_NAME = CompressorExample;
590-
PROVISIONING_PROFILE_SPECIFIER = "match Development com.onshobbak.dev";
591+
PROVISIONING_PROFILE_SPECIFIER = "match Development com.onshobbak.lava";
591592
SWIFT_OBJC_BRIDGING_HEADER = "CompressorExample-Bridging-Header.h";
592593
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
593594
SWIFT_VERSION = 5.0;
@@ -601,6 +602,7 @@
601602
buildSettings = {
602603
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
603604
CLANG_ENABLE_MODULES = YES;
605+
CODE_SIGN_IDENTITY = "iPhone Developer";
604606
CODE_SIGN_STYLE = Manual;
605607
CURRENT_PROJECT_VERSION = 1;
606608
DEVELOPMENT_TEAM = 454A869839;
@@ -612,9 +614,9 @@
612614
"-ObjC",
613615
"-lc++",
614616
);
615-
PRODUCT_BUNDLE_IDENTIFIER = com.onshobbak.dev;
617+
PRODUCT_BUNDLE_IDENTIFIER = com.onshobbak.lava;
616618
PRODUCT_NAME = CompressorExample;
617-
PROVISIONING_PROFILE_SPECIFIER = "match Development com.onshobbak.dev";
619+
PROVISIONING_PROFILE_SPECIFIER = "match Development com.onshobbak.lava";
618620
SWIFT_OBJC_BRIDGING_HEADER = "CompressorExample-Bridging-Header.h";
619621
SWIFT_VERSION = 5.0;
620622
VERSIONING_SYSTEM = "apple-generic";

example/ios/CompressorExample/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
<true/>
2727
<key>NSAppTransportSecurity</key>
2828
<dict>
29-
<key>NSAllowsArbitraryLoads</key>
30-
<true/>
29+
<key>NSAllowsArbitraryLoads</key>
30+
<true/>
3131
<key>NSExceptionDomains</key>
3232
<dict>
3333
<key>localhost</key>

ios/Video/VideoCompressor.swift

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

4040
override static func requiresMainQueueSetup() -> Bool {
@@ -177,6 +177,7 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
177177
let tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
178178
.appendingPathComponent(ProcessInfo().globallyUniqueString)
179179
.appendingPathExtension("mp4")
180+
var _bitRate=bitRate;
180181
let asset = AVAsset(url: url)
181182
guard asset.tracks.count >= 1 else {
182183
let error = CompressionError(message: "Invalid video URL, no track found")
@@ -209,8 +210,12 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
209210
height = (maxSize/width)*height
210211
width = maxSize
211212
}
213+
else
214+
{
215+
_bitRate=bitRate ?? Float(abs(track.estimatedDataRate))*0.8
216+
}
212217

213-
let videoBitRate = bitRate ?? height*width*1.5
218+
let videoBitRate = _bitRate ?? height*width*1.5
214219

215220
let compressionDict: [String: Any] = [
216221
AVVideoAverageBitRateKey: videoBitRate,

0 commit comments

Comments
 (0)