react-native-compressor 1.3.3 → 1.4.1-alpha
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 +53 -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 -1
- 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/CompressorModule.java +46 -1
- package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.java +210 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +30 -5
- package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +6 -0
- package/ios/.DS_Store +0 -0
- package/ios/Compressor-Bridging-Header.h +1 -1
- package/ios/Compressor.m +118 -12
- package/ios/Image/ImageCompressor.h +2 -0
- package/ios/Image/ImageCompressor.m +153 -1
- package/ios/Video/VideoCompressor.swift +39 -38
- package/lib/commonjs/Audio/index.js.map +1 -1
- package/lib/commonjs/Video/index.js +1 -1
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/commonjs/index.js +22 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/index.js +18 -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 +1 -1
- package/lib/module/Video/index.js.map +1 -1
- package/lib/module/index.js +6 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/index.js +9 -4
- package/lib/module/utils/index.js.map +1 -1
- package/lib/typescript/index.d.ts +5 -2
- package/lib/typescript/utils/index.d.ts +3 -0
- package/package.json +2 -1
- package/src/Audio/index.tsx +1 -1
- package/src/Video/index.tsx +1 -1
- package/src/index.tsx +13 -1
- package/src/utils/index.tsx +14 -3
- package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
package/ios/Compressor.m
CHANGED
|
@@ -9,6 +9,30 @@
|
|
|
9
9
|
#define AlAsset_Library_Scheme @"assets-library"
|
|
10
10
|
@implementation Compressor
|
|
11
11
|
AVAssetWriter *assetWriter=nil;
|
|
12
|
+
static NSArray *metadatas;
|
|
13
|
+
|
|
14
|
+
- (NSArray *)metadatas
|
|
15
|
+
{
|
|
16
|
+
if (!metadatas) {
|
|
17
|
+
metadatas = @[
|
|
18
|
+
@"albumName",
|
|
19
|
+
@"artist",
|
|
20
|
+
@"comment",
|
|
21
|
+
@"copyrights",
|
|
22
|
+
@"creationDate",
|
|
23
|
+
@"date",
|
|
24
|
+
@"encodedby",
|
|
25
|
+
@"genre",
|
|
26
|
+
@"language",
|
|
27
|
+
@"location",
|
|
28
|
+
@"lastModifiedDate",
|
|
29
|
+
@"performer",
|
|
30
|
+
@"publisher",
|
|
31
|
+
@"title"
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
return metadatas;
|
|
35
|
+
}
|
|
12
36
|
|
|
13
37
|
RCT_EXPORT_MODULE()
|
|
14
38
|
|
|
@@ -20,17 +44,19 @@ RCT_EXPORT_METHOD(
|
|
|
20
44
|
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
21
45
|
@try {
|
|
22
46
|
ImageCompressorOptions *options = [ImageCompressorOptions fromDictionary:optionsDict];
|
|
47
|
+
[ImageCompressor getAbsoluteImagePath:imagePath completionHandler:^(NSString* absoluteImagePath){
|
|
48
|
+
if(options.autoCompress)
|
|
49
|
+
{
|
|
50
|
+
NSString *result = [ImageCompressor autoCompressHandler:absoluteImagePath options:options];
|
|
51
|
+
resolve(result);
|
|
52
|
+
}
|
|
53
|
+
else
|
|
54
|
+
{
|
|
55
|
+
NSString *result = [ImageCompressor manualCompressHandler:absoluteImagePath options:options];
|
|
56
|
+
resolve(result);
|
|
57
|
+
}
|
|
58
|
+
}];
|
|
23
59
|
|
|
24
|
-
if(options.autoCompress)
|
|
25
|
-
{
|
|
26
|
-
NSString *result = [ImageCompressor autoCompressHandler:imagePath options:options];
|
|
27
|
-
resolve(result);
|
|
28
|
-
}
|
|
29
|
-
else
|
|
30
|
-
{
|
|
31
|
-
NSString *result = [ImageCompressor manualCompressHandler:imagePath options:options];
|
|
32
|
-
resolve(result);
|
|
33
|
-
}
|
|
34
60
|
}
|
|
35
61
|
@catch (NSException *exception) {
|
|
36
62
|
reject(exception.name, exception.reason, nil);
|
|
@@ -176,7 +202,7 @@ RCT_EXPORT_METHOD(
|
|
|
176
202
|
|
|
177
203
|
//general
|
|
178
204
|
RCT_EXPORT_METHOD(
|
|
179
|
-
|
|
205
|
+
generateFilePath: (NSString*) extension
|
|
180
206
|
resolver: (RCTPromiseResolveBlock) resolve
|
|
181
207
|
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
182
208
|
@try {
|
|
@@ -188,6 +214,30 @@ RCT_EXPORT_METHOD(
|
|
|
188
214
|
}
|
|
189
215
|
}
|
|
190
216
|
|
|
217
|
+
RCT_EXPORT_METHOD(
|
|
218
|
+
getRealPath: (NSString*) path
|
|
219
|
+
type: (NSString*) type
|
|
220
|
+
resolver: (RCTPromiseResolveBlock) resolve
|
|
221
|
+
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
222
|
+
@try {
|
|
223
|
+
if([type isEqualToString:@"video"])
|
|
224
|
+
{
|
|
225
|
+
[ImageCompressor getAbsoluteVideoPath:path completionHandler:^(NSString* absoluteImagePath){
|
|
226
|
+
resolve(absoluteImagePath);
|
|
227
|
+
}];
|
|
228
|
+
}
|
|
229
|
+
else
|
|
230
|
+
{
|
|
231
|
+
[ImageCompressor getAbsoluteImagePath:path completionHandler:^(NSString* absoluteImagePath){
|
|
232
|
+
resolve(absoluteImagePath);
|
|
233
|
+
}];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
@catch (NSException *exception) {
|
|
237
|
+
reject(exception.name, exception.reason, nil);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
191
241
|
//general
|
|
192
242
|
RCT_EXPORT_METHOD(
|
|
193
243
|
getFileSize: (NSString*) filePath
|
|
@@ -216,6 +266,63 @@ RCT_EXPORT_METHOD(
|
|
|
216
266
|
}
|
|
217
267
|
}
|
|
218
268
|
|
|
269
|
+
|
|
270
|
+
RCT_EXPORT_METHOD(
|
|
271
|
+
getVideoMetaData: (NSString*) filePath
|
|
272
|
+
resolver: (RCTPromiseResolveBlock) resolve
|
|
273
|
+
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
274
|
+
@try {
|
|
275
|
+
[ImageCompressor getAbsoluteVideoPath:filePath completionHandler:^(NSString *absoluteImagePath) {
|
|
276
|
+
if([absoluteImagePath containsString:@"file://"])
|
|
277
|
+
{
|
|
278
|
+
absoluteImagePath=[absoluteImagePath stringByReplacingOccurrencesOfString:@"file://"
|
|
279
|
+
withString:@""];
|
|
280
|
+
}
|
|
281
|
+
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
282
|
+
|
|
283
|
+
BOOL isDir;
|
|
284
|
+
if (![fileManager fileExistsAtPath:absoluteImagePath isDirectory:&isDir] || isDir){
|
|
285
|
+
NSError *err = [NSError errorWithDomain:@"file not found" code:-15 userInfo:nil];
|
|
286
|
+
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
NSDictionary *attrs = [fileManager attributesOfItemAtPath: absoluteImagePath error: NULL];
|
|
290
|
+
UInt32 fileSize = [attrs fileSize];
|
|
291
|
+
NSString *fileSizeString = [@(fileSize) stringValue];
|
|
292
|
+
|
|
293
|
+
NSMutableDictionary *result = [NSMutableDictionary new];
|
|
294
|
+
NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
|
|
295
|
+
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:absoluteImagePath] options:assetOptions];\
|
|
296
|
+
AVAssetTrack *avAsset = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
|
|
297
|
+
CGSize size = [avAsset naturalSize];
|
|
298
|
+
NSString *extension = [[absoluteImagePath lastPathComponent] pathExtension];
|
|
299
|
+
CMTime time = [asset duration];
|
|
300
|
+
int seconds = ceil(time.value/time.timescale);
|
|
301
|
+
[result setObject:[NSString stringWithFormat: @"%.2f", size.width] forKey:@"width"];
|
|
302
|
+
[result setObject:[NSString stringWithFormat: @"%.2f", size.height] forKey:@"height"];
|
|
303
|
+
[result setObject:extension forKey:@"extension"];
|
|
304
|
+
[result setObject:fileSizeString forKey:@"size"];
|
|
305
|
+
[result setObject:[@(seconds) stringValue] forKey:@"duration"];
|
|
306
|
+
NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
|
|
307
|
+
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
|
|
308
|
+
// string keys
|
|
309
|
+
for (NSString *key in [self metadatas]) {
|
|
310
|
+
NSArray *items = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
|
|
311
|
+
withKey:key
|
|
312
|
+
keySpace:AVMetadataKeySpaceCommon];
|
|
313
|
+
for (AVMetadataItem *item in items) {
|
|
314
|
+
[result setObject:item.value forKey:key];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
resolve(result);
|
|
318
|
+
}];
|
|
319
|
+
}];
|
|
320
|
+
}
|
|
321
|
+
@catch (NSException *exception) {
|
|
322
|
+
reject(exception.name, exception.reason, nil);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
219
326
|
@end
|
|
220
327
|
|
|
221
328
|
|
|
@@ -242,4 +349,3 @@ RCT_EXTERN_METHOD(deactivateBackgroundTask: (NSDictionary *)options
|
|
|
242
349
|
RCT_EXTERN_METHOD(cancelCompression:(NSString *)uuid)
|
|
243
350
|
|
|
244
351
|
@end
|
|
245
|
-
|
|
@@ -12,4 +12,6 @@
|
|
|
12
12
|
+(NSString *)autoCompressHandler:(NSString *)imagePath options:(ImageCompressorOptions*)options;
|
|
13
13
|
+ (NSString *)manualCompress:(UIImage *)image output:(enum OutputType)output quality:(float)quality outputExtension:(NSString*)outputExtension isBase64:(Boolean)isBase64;
|
|
14
14
|
+ (UIImage *)scaleAndRotateImage:(UIImage *)image;
|
|
15
|
+
+ (void)getAbsoluteImagePath:(NSString *)imagePath completionHandler:(void (^)(NSString *absoluteImagePath))completionHandler;
|
|
16
|
+
+(void)getAbsoluteVideoPath:(NSString *)videoPath completionHandler:(void (^)(NSString *absoluteImagePath))completionHandler;
|
|
15
17
|
@end
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
#import <Accelerate/Accelerate.h>
|
|
2
2
|
#import <CoreGraphics/CoreGraphics.h>
|
|
3
|
-
|
|
3
|
+
#import <Photos/Photos.h>
|
|
4
|
+
#import <React/RCTUtils.h>
|
|
5
|
+
#import <React/RCTImageLoader.h>
|
|
4
6
|
#import "ImageCompressor.h"
|
|
7
|
+
#import <React/RCTConvert.h>
|
|
8
|
+
#import <Foundation/Foundation.h>
|
|
9
|
+
#import <MobileCoreServices/MobileCoreServices.h>
|
|
5
10
|
|
|
6
11
|
@implementation ImageCompressor
|
|
7
12
|
+ (CGSize) findTargetSize: (UIImage *) image maxWidth: (int) maxWidth maxHeight: (int) maxHeight {
|
|
@@ -288,4 +293,151 @@
|
|
|
288
293
|
return returnablePath;
|
|
289
294
|
}
|
|
290
295
|
|
|
296
|
+
+(void)getAbsoluteImagePath:(NSString *)imagePath completionHandler:(void (^)(NSString *absoluteImagePath))completionHandler
|
|
297
|
+
{
|
|
298
|
+
if (![imagePath containsString:@"ph://"]) {
|
|
299
|
+
completionHandler(imagePath);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
NSURL *imageURL = [NSURL URLWithString:[imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"]];
|
|
303
|
+
CGSize size = CGSizeZero;
|
|
304
|
+
CGFloat scale = 1;
|
|
305
|
+
RCTResizeMode resizeMode = RCTResizeModeContain;
|
|
306
|
+
NSString *assetID = @"";
|
|
307
|
+
PHFetchResult *results;
|
|
308
|
+
if (!imageURL) {
|
|
309
|
+
NSString *errorText = @"Cannot load a photo library asset with no URL";
|
|
310
|
+
@throw([NSException exceptionWithName:errorText reason:errorText userInfo:nil]);
|
|
311
|
+
} else if ([imageURL.scheme caseInsensitiveCompare:@"assets-library"] == NSOrderedSame) {
|
|
312
|
+
assetID = [imageURL absoluteString];
|
|
313
|
+
results = [PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil];
|
|
314
|
+
} else {
|
|
315
|
+
assetID = [imageURL.absoluteString substringFromIndex:@"ph://".length];
|
|
316
|
+
results = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil];
|
|
317
|
+
}
|
|
318
|
+
if (results.count == 0) {
|
|
319
|
+
NSString *errorText = [NSString stringWithFormat:@"Failed to fetch PHAsset with local identifier %@ with no error message.", assetID];
|
|
320
|
+
@throw([NSException exceptionWithName:errorText reason:errorText userInfo:nil]);
|
|
321
|
+
}
|
|
322
|
+
PHAsset *asset = [results firstObject]; // <-- WE GOT THE ASSET
|
|
323
|
+
PHImageRequestOptions *imageOptions = [PHImageRequestOptions new];
|
|
324
|
+
imageOptions.networkAccessAllowed = YES;
|
|
325
|
+
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
|
|
326
|
+
|
|
327
|
+
BOOL useMaximumSize = CGSizeEqualToSize(size, CGSizeZero);
|
|
328
|
+
CGSize targetSize;
|
|
329
|
+
if (useMaximumSize) {
|
|
330
|
+
targetSize = PHImageManagerMaximumSize;
|
|
331
|
+
imageOptions.resizeMode = PHImageRequestOptionsResizeModeNone;
|
|
332
|
+
} else {
|
|
333
|
+
targetSize = CGSizeApplyAffineTransform(size, CGAffineTransformMakeScale(scale, scale));
|
|
334
|
+
imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
PHImageContentMode contentMode = PHImageContentModeAspectFill;
|
|
338
|
+
if (resizeMode == RCTResizeModeContain) {
|
|
339
|
+
contentMode = PHImageContentModeAspectFit;
|
|
340
|
+
}
|
|
341
|
+
[[PHImageManager defaultManager] requestImageForAsset:asset
|
|
342
|
+
targetSize:targetSize
|
|
343
|
+
contentMode:contentMode
|
|
344
|
+
options:imageOptions
|
|
345
|
+
resultHandler:^(UIImage *result, NSDictionary<NSString *, id> *info) {
|
|
346
|
+
// WE GOT THE IMAGE
|
|
347
|
+
if (result) {
|
|
348
|
+
UIImage *image = result;
|
|
349
|
+
NSString *imageName = [assetID stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
|
|
350
|
+
NSString *imagePath = [self saveImageIntoCache:image withName:imageName];
|
|
351
|
+
completionHandler(imagePath);
|
|
352
|
+
} else {
|
|
353
|
+
@throw([NSException exceptionWithName:@"image not found" reason:@"image not found" userInfo:nil]);
|
|
354
|
+
}
|
|
355
|
+
}];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
+(NSString*) saveImageIntoCache:(UIImage *)image withName:(NSString *)name {
|
|
359
|
+
NSData *imageData = UIImageJPEGRepresentation(image, 1);
|
|
360
|
+
NSString *path =[self generateCacheFilePath:@"jpg"];
|
|
361
|
+
[[NSFileManager defaultManager] createFileAtPath:path contents:imageData attributes:NULL];
|
|
362
|
+
return path;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/// for video
|
|
366
|
+
|
|
367
|
+
+(void)getAbsoluteVideoPath:(NSString *)videoPath completionHandler:(void (^)(NSString *absoluteImagePath))completionHandler
|
|
368
|
+
{
|
|
369
|
+
|
|
370
|
+
if (![videoPath containsString:@"ph://"]) {
|
|
371
|
+
completionHandler(videoPath);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
NSString *assetId =[videoPath stringByReplacingOccurrencesOfString:@"ph://"
|
|
375
|
+
withString:@""];
|
|
376
|
+
AVFileType outputFileType = AVFileTypeMPEG4;
|
|
377
|
+
NSString *pressetType = AVAssetExportPresetPassthrough;
|
|
378
|
+
|
|
379
|
+
// Throwing some errors to the user if he is not careful enough
|
|
380
|
+
if ([assetId isEqualToString:@""]) {
|
|
381
|
+
NSError *error = [NSError errorWithDomain:@"RNGalleryManager" code: -91 userInfo:nil];
|
|
382
|
+
@throw([NSException exceptionWithName:error reason:error userInfo:nil]);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Getting Video Asset
|
|
387
|
+
NSArray* localIds = [NSArray arrayWithObjects: assetId, nil];
|
|
388
|
+
PHAsset * _Nullable videoAsset = [PHAsset fetchAssetsWithLocalIdentifiers:localIds options:nil].firstObject;
|
|
389
|
+
|
|
390
|
+
// Getting information from the asset
|
|
391
|
+
NSString *mimeType = (NSString *)CFBridgingRelease(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef _Nonnull)(outputFileType), kUTTagClassMIMEType));
|
|
392
|
+
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef _Nonnull)(mimeType), NULL);
|
|
393
|
+
NSString *extension = (NSString *)CFBridgingRelease(UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension));
|
|
394
|
+
|
|
395
|
+
// Creating output url and temp file name
|
|
396
|
+
NSString *path =[self generateCacheFilePath:extension];
|
|
397
|
+
NSURL *outputUrl = [NSURL URLWithString:[@"file://" stringByAppendingString:path]];
|
|
398
|
+
|
|
399
|
+
// Setting video export session options
|
|
400
|
+
PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
|
|
401
|
+
videoRequestOptions.networkAccessAllowed = YES;
|
|
402
|
+
videoRequestOptions.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
|
|
403
|
+
|
|
404
|
+
// Creating new export session
|
|
405
|
+
[[PHImageManager defaultManager] requestExportSessionForVideo:videoAsset options:videoRequestOptions exportPreset:pressetType resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
|
|
406
|
+
|
|
407
|
+
exportSession.shouldOptimizeForNetworkUse = YES;
|
|
408
|
+
exportSession.outputFileType = outputFileType;
|
|
409
|
+
exportSession.outputURL = outputUrl;
|
|
410
|
+
// Converting the video and waiting to see whats going to happen
|
|
411
|
+
[exportSession exportAsynchronouslyWithCompletionHandler:^{
|
|
412
|
+
switch ([exportSession status])
|
|
413
|
+
{
|
|
414
|
+
case AVAssetExportSessionStatusFailed:
|
|
415
|
+
{
|
|
416
|
+
NSError* error = exportSession.error;
|
|
417
|
+
NSString *codeWithDomain = [NSString stringWithFormat:@"E%@%zd", error.domain.uppercaseString, error.code];
|
|
418
|
+
@throw([NSException exceptionWithName:error reason:error userInfo:nil]);
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
case AVAssetExportSessionStatusCancelled:
|
|
422
|
+
{
|
|
423
|
+
NSError *error = [NSError errorWithDomain:@"RNGalleryManager" code: -91 userInfo:nil];
|
|
424
|
+
@throw([NSException exceptionWithName:error reason:error userInfo:nil]);
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
case AVAssetExportSessionStatusCompleted:
|
|
428
|
+
{
|
|
429
|
+
completionHandler(outputUrl.absoluteString);
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
default:
|
|
433
|
+
{
|
|
434
|
+
NSError *error = [NSError errorWithDomain:@"RNGalleryManager" code: -91 userInfo:nil];
|
|
435
|
+
@throw([NSException exceptionWithName:@"Unknown status" reason:@"Unknown status" userInfo:nil]);
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}];
|
|
440
|
+
}];
|
|
441
|
+
}
|
|
442
|
+
|
|
291
443
|
@end
|
|
@@ -2,6 +2,7 @@ import Foundation
|
|
|
2
2
|
import AssetsLibrary
|
|
3
3
|
import AVFoundation
|
|
4
4
|
import NextLevelSessionExporter
|
|
5
|
+
import Photos
|
|
5
6
|
|
|
6
7
|
struct CompressionError: Error {
|
|
7
8
|
private let message: String
|
|
@@ -36,7 +37,7 @@ class VideoCompressor: RCTEventEmitter, URLSessionTaskDelegate {
|
|
|
36
37
|
var uploadRejectors: [String: RCTPromiseRejectBlock] = [:]
|
|
37
38
|
var compressorExports: [String: NextLevelSessionExporter] = [:]
|
|
38
39
|
let videoCompressionThreshold:Int=7
|
|
39
|
-
|
|
40
|
+
|
|
40
41
|
|
|
41
42
|
override static func requiresMainQueueSetup() -> Bool {
|
|
42
43
|
return false
|
|
@@ -205,45 +206,45 @@ func makeValidUri(filePath: String) -> String {
|
|
|
205
206
|
|
|
206
207
|
|
|
207
208
|
func compressVideo(url: URL, options: [String: Any], onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
if(fileSize>minimumFileSizeForCompress)
|
|
215
|
-
{
|
|
216
|
-
if(options["compressionMethod"] as! String=="auto")
|
|
209
|
+
ImageCompressor.getAbsoluteVideoPath(url.absoluteString) { absoluteVideoPath in
|
|
210
|
+
var minimumFileSizeForCompress:Double=16.0;
|
|
211
|
+
let videoURL = URL(string: absoluteVideoPath!)
|
|
212
|
+
let fileSize=self.getfileSize(forURL: videoURL!);
|
|
213
|
+
if((options["minimumFileSizeForCompress"]) != nil)
|
|
217
214
|
{
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
215
|
+
minimumFileSizeForCompress=options["minimumFileSizeForCompress"] as! Double;
|
|
216
|
+
}
|
|
217
|
+
if(fileSize>minimumFileSizeForCompress)
|
|
218
|
+
{
|
|
219
|
+
if(options["compressionMethod"] as! String=="auto")
|
|
220
|
+
{
|
|
221
|
+
self.autoCompressionHelper(url: videoURL!, options:options) { progress in
|
|
222
|
+
onProgress(progress)
|
|
223
|
+
} onCompletion: { outputURL in
|
|
224
|
+
onCompletion(outputURL)
|
|
225
|
+
} onFailure: { error in
|
|
226
|
+
onFailure(error)
|
|
227
|
+
}
|
|
224
228
|
}
|
|
229
|
+
else
|
|
230
|
+
{
|
|
231
|
+
self.manualCompressionHelper(url: videoURL!, options:options) { progress in
|
|
232
|
+
onProgress(progress)
|
|
233
|
+
} onCompletion: { outputURL in
|
|
234
|
+
onCompletion(outputURL)
|
|
235
|
+
} onFailure: { error in
|
|
236
|
+
onFailure(error)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
225
242
|
}
|
|
226
243
|
else
|
|
227
244
|
{
|
|
228
|
-
|
|
229
|
-
onProgress(progress)
|
|
230
|
-
} onCompletion: { outputURL in
|
|
231
|
-
onCompletion(outputURL)
|
|
232
|
-
} onFailure: { error in
|
|
233
|
-
onFailure(error)
|
|
234
|
-
}
|
|
245
|
+
onCompletion(url)
|
|
235
246
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
else
|
|
241
|
-
{
|
|
242
|
-
onCompletion(url)
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
+
}
|
|
247
248
|
}
|
|
248
249
|
|
|
249
250
|
|
|
@@ -343,6 +344,7 @@ func makeValidUri(filePath: String) -> String {
|
|
|
343
344
|
}
|
|
344
345
|
|
|
345
346
|
func exportVideoHelper(url: URL,asset: AVAsset, bitRate: Int,resultWidth:Float,resultHeight:Float,uuid:String, onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
347
|
+
var videoCompressionCounter:Int=0
|
|
346
348
|
var tmpURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
347
349
|
.appendingPathComponent(ProcessInfo().globallyUniqueString)
|
|
348
350
|
.appendingPathExtension("mp4")
|
|
@@ -374,14 +376,14 @@ func makeValidUri(filePath: String) -> String {
|
|
|
374
376
|
compressorExports[uuid] = exporter
|
|
375
377
|
exporter.export(progressHandler: { (progress) in
|
|
376
378
|
let _progress:Float=progress*100;
|
|
377
|
-
if(Int(_progress)==
|
|
379
|
+
if(Int(_progress)==videoCompressionCounter)
|
|
378
380
|
{
|
|
379
|
-
|
|
381
|
+
videoCompressionCounter=Int(_progress)+self.videoCompressionThreshold
|
|
380
382
|
onProgress(progress)
|
|
381
383
|
}
|
|
382
384
|
|
|
383
385
|
}, completionHandler: { result in
|
|
384
|
-
|
|
386
|
+
videoCompressionCounter=0;
|
|
385
387
|
switch exporter.status {
|
|
386
388
|
case .completed:
|
|
387
389
|
onCompletion(exporter.outputURL!)
|
|
@@ -411,5 +413,4 @@ func makeValidUri(filePath: String) -> String {
|
|
|
411
413
|
return track;
|
|
412
414
|
}
|
|
413
415
|
|
|
414
|
-
|
|
415
416
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["NativeAudio","NativeModules","Compressor","Audio","compress","url","options","DEFAULT_COMPRESS_AUDIO_OPTIONS","checkUrlAndOptionsResult","isCorrect","message","mediaDetails","bitrate","i","AUDIO_BITRATE","length","quality","compress_audio","error"],"mappings":";;;;;;;AAAA;;AACA;;AAOA,MAAMA,WAAW,GAAGC,2BAAcC,UAAlC;AAEA,MAAMC,KAAgB,GAAG;AACvBC,EAAAA,QAAQ,EAAE,OAAOC,GAAP,EAAYC,OAAO,GAAGC,qCAAtB,KAAyD;AACjE,QAAI;AACF,YAAMC,wBAA2C,GAC/C,MAAM,+BAAmBH,GAAnB,EAAwBC,OAAxB,CADR;;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B,CAArC,CAAV;AACA;AACD;AACF;AACF;;AAED,eAAOf,WAAW,CAACiB,cAAZ,CAA2BZ,GAA3B,EAAgC;AACrCO,UAAAA,OADqC;AAErCI,UAAAA,OAAO,EAAEV,OAAO,CAACU;AAFoB,SAAhC,CAAP;AAID;AACF,KAjDD,CAiDE,OAAOE,KAAP,
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["NativeAudio","NativeModules","Compressor","Audio","compress","url","options","DEFAULT_COMPRESS_AUDIO_OPTIONS","checkUrlAndOptionsResult","isCorrect","message","mediaDetails","bitrate","i","AUDIO_BITRATE","length","quality","compress_audio","error"],"mappings":";;;;;;;AAAA;;AACA;;AAOA,MAAMA,WAAW,GAAGC,2BAAcC,UAAlC;AAEA,MAAMC,KAAgB,GAAG;AACvBC,EAAAA,QAAQ,EAAE,OAAOC,GAAP,EAAYC,OAAO,GAAGC,qCAAtB,KAAyD;AACjE,QAAI;AACF,YAAMC,wBAA2C,GAC/C,MAAM,+BAAmBH,GAAnB,EAAwBC,OAAxB,CADR;;AAEA,UAAI,CAACE,wBAAwB,CAACC,SAA9B,EAAyC;AACvC,cAAMD,wBAAwB,CAACE,OAA/B;AACD,OAFD,MAEO;AACL;AAEA;AACA;AACA,cAAMC,YAAiB,GAAG;AACxBC,UAAAA,OAAO,EAAE;AADe,SAA1B,CALK,CASL;;AACA,YAAIA,OAAY,GAAGL,sCAA+BK,OAAlD;;AAEA,YAAID,YAAY,IAAIA,YAAY,CAACC,OAAjC,EAA0C;AACxC;AACA,eAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGC,qBAAcC,MAAlC,EAA0CF,CAAC,EAA3C,EAA+C;AAC7C;AACA,gBAAIF,YAAY,CAACC,OAAb,GAAuBE,qBAAcD,CAAd,CAA3B,EAA6C;AAC3C,kBAAIA,CAAC,GAAG,CAAJ,GAAQC,qBAAcC,MAA1B,EAAkC;AAChC,oBAAIT,OAAO,CAACU,OAAR,KAAoB,KAAxB,EAA+BJ,OAAO,GAAGE,qBAAcD,CAAC,GAAG,CAAlB,CAAV,CAA/B,KACK,IAAIP,OAAO,CAACU,OAAR,KAAoB,QAAxB,EACHJ,OAAO,GAAGE,qBAAcD,CAAC,GAAG,CAAlB,CAAV,CADG,KAEAD,OAAO,GAAGE,qBAAcD,CAAd,CAAV;AACN,eALD,MAKO,IAAIA,CAAC,GAAG,CAAJ,GAAQC,qBAAcC,MAA1B,EAAkC;AACvC,oBAAIT,OAAO,CAACU,OAAR,KAAoB,KAAxB,EAA+BJ,OAAO,GAAGE,qBAAcD,CAAC,GAAG,CAAlB,CAAV,CAA/B,KACKD,OAAO,GAAGE,qBAAcD,CAAd,CAAV;AACN,eAHM,MAGAD,OAAO,GAAGE,qBAAcD,CAAd,CAAV;;AACP;AACD,aAb4C,CAe7C;;;AACA,gBACEF,YAAY,CAACC,OAAb,IAAwBE,qBAAcA,qBAAcC,MAAd,GAAuB,CAArC,CAD1B,EAEE;AACAH,cAAAA,OAAO,GAAGE,qBAAcA,qBAAcC,MAAd,GAAuB,CAArC,CAAV;AACA;AACD;AACF;AACF;;AAED,eAAOf,WAAW,CAACiB,cAAZ,CAA2BZ,GAA3B,EAAgC;AACrCO,UAAAA,OADqC;AAErCI,UAAAA,OAAO,EAAEV,OAAO,CAACU;AAFoB,SAAhC,CAAP;AAID;AACF,KAjDD,CAiDE,OAAOE,KAAP,EAAmB;AACnB,YAAMA,KAAK,CAACR,OAAZ;AACD;AACF;AAtDsB,CAAzB;eAyDeP,K","sourcesContent":["import { NativeModules } from 'react-native';\nimport {\n AUDIO_BITRATE,\n AudioType,\n DEFAULT_COMPRESS_AUDIO_OPTIONS,\n defaultResultType,\n checkUrlAndOptions,\n} from '../utils';\nconst NativeAudio = NativeModules.Compressor;\n\nconst Audio: AudioType = {\n compress: async (url, options = DEFAULT_COMPRESS_AUDIO_OPTIONS) => {\n try {\n const checkUrlAndOptionsResult: defaultResultType =\n await checkUrlAndOptions(url, options);\n if (!checkUrlAndOptionsResult.isCorrect) {\n throw checkUrlAndOptionsResult.message;\n } else {\n // Get resulting output file path\n\n // Get media details\n // const mediaDetails: any = await getDetails(url).catch(() => null);\n const mediaDetails: any = {\n bitrate: 0,\n };\n\n // Initialize bitrate\n let bitrate: any = DEFAULT_COMPRESS_AUDIO_OPTIONS.bitrate;\n\n if (mediaDetails && mediaDetails.bitrate) {\n // Check and return the appropriate bitrate according to quality expected\n for (let i = 0; i < AUDIO_BITRATE.length; i++) {\n // Check a particular bitrate to return its nearest lower according to quality\n if (mediaDetails.bitrate > AUDIO_BITRATE[i]) {\n if (i + 2 < AUDIO_BITRATE.length) {\n if (options.quality === 'low') bitrate = AUDIO_BITRATE[i + 2];\n else if (options.quality === 'medium')\n bitrate = AUDIO_BITRATE[i + 1];\n else bitrate = AUDIO_BITRATE[i];\n } else if (i + 1 < AUDIO_BITRATE.length) {\n if (options.quality === 'low') bitrate = AUDIO_BITRATE[i + 1];\n else bitrate = AUDIO_BITRATE[i];\n } else bitrate = AUDIO_BITRATE[i];\n break;\n }\n\n // Check if the matching bitrate is the last in the array\n if (\n mediaDetails.bitrate <= AUDIO_BITRATE[AUDIO_BITRATE.length - 1]\n ) {\n bitrate = AUDIO_BITRATE[AUDIO_BITRATE.length - 1];\n break;\n }\n }\n }\n\n return NativeAudio.compress_audio(url, {\n bitrate,\n quality: options.quality,\n });\n }\n } catch (error: any) {\n throw error.message;\n }\n },\n};\n\nexport default Audio;\n"]}
|
|
@@ -82,7 +82,7 @@ const Video = {
|
|
|
82
82
|
modifiedOptions.maxSize = 640;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
if (options
|
|
85
|
+
if ((options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress) !== undefined) {
|
|
86
86
|
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
87
87
|
}
|
|
88
88
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["VideoCompressEventEmitter","NativeEventEmitter","NativeModules","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","remove","cancelCompression","cancellationId","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","getCancellationId","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AAMA;;AAgEA,MAAMA,yBAAyB,GAAG,IAAIC,+BAAJ,CAChCC,2BAAcC,eADkB,CAAlC;AAIA,MAAMC,qBAAqB,GAAGF,2BAAcC,eAA5C;;AAEO,MAAME,gBAAgB,GAAG,OAC9BC,GAD8B,EAE9BC,OAF8B,EAG9BC,OAH8B,EAI9BC,UAJ8B,KAKb;AACjB,QAAMC,IAAI,GAAG,oBAAb;AACA,MAAIC,YAAJ;;AACA,MAAI;AACF,QAAIF,UAAJ,EAAgB;AACdE,MAAAA,YAAY,GAAGX,yBAAyB,CAACY,WAA1B,CACb,yBADa,EAEZC,KAAD,IAAgB;AACd,YAAIA,KAAK,CAACH,IAAN,KAAeA,IAAnB,EAAyB;AACvBD,UAAAA,UAAU,CAACI,KAAK,CAACC,IAAN,CAAWC,OAAZ,EAAqBF,KAAK,CAACC,IAAN,CAAWE,KAAhC,CAAV;AACD;AACF,OANY,CAAf;AAQD;;AACD,QAAIC,sBAASC,EAAT,KAAgB,SAAhB,IAA6BX,OAAO,CAACY,QAAR,CAAiB,SAAjB,CAAjC,EAA8D;AAC5DZ,MAAAA,OAAO,GAAGA,OAAO,CAACa,OAAR,CAAgB,SAAhB,EAA2B,EAA3B,CAAV;AACD;;AACD,UAAMC,MAAM,GAAG,MAAMjB,qBAAqB,CAACkB,MAAtB,CAA6Bf,OAA7B,EAAsC;AACzDG,MAAAA,IADyD;AAEzDa,MAAAA,MAAM,EAAEf,OAAO,CAACgB,UAFyC;AAGzDC,MAAAA,OAAO,EAA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AAsB;AACpBH,QAAAA,eAAe,CAACG,OAAhB,GAA0B3B,OAA1B,aAA0BA,OAA1B,uBAA0BA,OAAO,CAAE2B,OAAnC;AACD,OAFD,MAEO;AACLH,QAAAA,eAAe,CAACG,OAAhB,GAA0B,GAA1B;AACD;;AACD,
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["VideoCompressEventEmitter","NativeEventEmitter","NativeModules","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","remove","cancelCompression","cancellationId","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","undefined","getCancellationId","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AAMA;;AAgEA,MAAMA,yBAAyB,GAAG,IAAIC,+BAAJ,CAChCC,2BAAcC,eADkB,CAAlC;AAIA,MAAMC,qBAAqB,GAAGF,2BAAcC,eAA5C;;AAEO,MAAME,gBAAgB,GAAG,OAC9BC,GAD8B,EAE9BC,OAF8B,EAG9BC,OAH8B,EAI9BC,UAJ8B,KAKb;AACjB,QAAMC,IAAI,GAAG,oBAAb;AACA,MAAIC,YAAJ;;AACA,MAAI;AACF,QAAIF,UAAJ,EAAgB;AACdE,MAAAA,YAAY,GAAGX,yBAAyB,CAACY,WAA1B,CACb,yBADa,EAEZC,KAAD,IAAgB;AACd,YAAIA,KAAK,CAACH,IAAN,KAAeA,IAAnB,EAAyB;AACvBD,UAAAA,UAAU,CAACI,KAAK,CAACC,IAAN,CAAWC,OAAZ,EAAqBF,KAAK,CAACC,IAAN,CAAWE,KAAhC,CAAV;AACD;AACF,OANY,CAAf;AAQD;;AACD,QAAIC,sBAASC,EAAT,KAAgB,SAAhB,IAA6BX,OAAO,CAACY,QAAR,CAAiB,SAAjB,CAAjC,EAA8D;AAC5DZ,MAAAA,OAAO,GAAGA,OAAO,CAACa,O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AIF,YAAJ,EAAkB;AAChBA,UAAAA,YAAY,CAACe,MAAb;AACD;AACF,OAPH,CADF;AAUD;;AACD,WAAOtB,qBAAqB,CAACmC,sBAAtB,CAA6C,EAA7C,CAAP;AACD,GAxEgC;;AAyEjCE,EAAAA,wBAAwB,GAAG;AACzBzC,IAAAA,yBAAyB,CAAC0C,kBAA1B,CAA6C,uBAA7C;AACA,WAAOtC,qBAAqB,CAACqC,wBAAtB,CAA+C,EAA/C,CAAP;AACD;;AA5EgC,CAAnC;eA+EeZ,K","sourcesContent":["import {\n NativeModules,\n NativeEventEmitter,\n Platform,\n NativeEventSubscription,\n} from 'react-native';\nimport { uuidv4 } from '../utils';\n\nexport declare enum FileSystemUploadType {\n BINARY_CONTENT = 0,\n MULTIPART = 1,\n}\n\nexport declare type FileSystemAcceptedUploadHttpMethod =\n | 'POST'\n | 'PUT'\n | 'PATCH';\nexport type compressionMethod = 'auto' | 'manual';\ntype videoCompresssionType = {\n bitrate?: number;\n maxSize?: number;\n compressionMethod?: compressionMethod;\n minimumFileSizeForCompress?: number;\n getCancellationId?: (cancellationId: string) => void;\n};\n\nexport declare enum FileSystemSessionType {\n BACKGROUND = 0,\n FOREGROUND = 1,\n}\n\nexport declare type HTTPResponse = {\n status: number;\n headers: Record<string, string>;\n body: string;\n};\n\nexport declare type FileSystemUploadOptions = (\n | {\n uploadType?: FileSystemUploadType.BINARY_CONTENT;\n }\n | {\n uploadType: FileSystemUploadType.MULTIPART;\n fieldName?: string;\n mimeType?: string;\n parameters?: Record<string, string>;\n }\n) & {\n headers?: Record<string, string>;\n httpMethod?: FileSystemAcceptedUploadHttpMethod;\n sessionType?: FileSystemSessionType;\n};\n\nexport type VideoCompressorType = {\n compress(\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ): Promise<string>;\n cancelCompression(cancellationId: string): void;\n backgroundUpload(\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n ): Promise<any>;\n activateBackgroundTask(onExpired?: (data: any) => void): Promise<any>;\n deactivateBackgroundTask(): Promise<any>;\n};\n\nconst VideoCompressEventEmitter = new NativeEventEmitter(\n NativeModules.VideoCompressor\n);\n\nconst NativeVideoCompressor = NativeModules.VideoCompressor;\n\nexport const backgroundUpload = async (\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n): Promise<any> => {\n const uuid = uuidv4();\n let subscription: NativeEventSubscription;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'VideoCompressorProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.written, event.data.total);\n }\n }\n );\n }\n if (Platform.OS === 'android' && fileUrl.includes('file://')) {\n fileUrl = fileUrl.replace('file://', '');\n }\n const result = await NativeVideoCompressor.upload(fileUrl, {\n uuid,\n method: options.httpMethod,\n headers: options.headers,\n url,\n });\n return result;\n } finally {\n // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n};\n\nexport const cancelCompression = (cancellationId: string) => {\n return NativeVideoCompressor.cancelCompression(cancellationId);\n};\n\nconst Video: VideoCompressorType = {\n compress: async (\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ) => {\n const uuid = uuidv4();\n let subscription: NativeEventSubscription;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'videoCompressProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.progress);\n }\n }\n );\n }\n const modifiedOptions: {\n uuid: string;\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n } = { uuid };\n if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;\n if (options?.compressionMethod) {\n modifiedOptions.compressionMethod = options?.compressionMethod;\n } else {\n modifiedOptions.compressionMethod = 'manual';\n }\n if (options?.maxSize) {\n modifiedOptions.maxSize = options?.maxSize;\n } else {\n modifiedOptions.maxSize = 640;\n }\n if (options?.minimumFileSizeForCompress !== undefined) {\n modifiedOptions.minimumFileSizeForCompress =\n options?.minimumFileSizeForCompress;\n }\n if (options?.getCancellationId) {\n options?.getCancellationId(uuid);\n }\n const result = await NativeVideoCompressor.compress(\n fileUrl,\n modifiedOptions\n );\n return result;\n } finally {\n // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n },\n backgroundUpload,\n cancelCompression,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription: NativeEventSubscription =\n VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n if (subscription) {\n subscription.remove();\n }\n }\n );\n }\n return NativeVideoCompressor.activateBackgroundTask({});\n },\n deactivateBackgroundTask() {\n VideoCompressEventEmitter.removeAllListeners('backgroundTaskExpired');\n return NativeVideoCompressor.deactivateBackgroundTask({});\n },\n} as VideoCompressorType;\n\nexport default Video;\n"]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -45,6 +45,24 @@ Object.defineProperty(exports, "uuidv4", {
|
|
|
45
45
|
return _utils.uuidv4;
|
|
46
46
|
}
|
|
47
47
|
});
|
|
48
|
+
Object.defineProperty(exports, "generateFilePath", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () {
|
|
51
|
+
return _utils.generateFilePath;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(exports, "getRealPath", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _utils.getRealPath;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, "getVideoMetaData", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function () {
|
|
63
|
+
return _utils.getVideoMetaData;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
48
66
|
exports.default = void 0;
|
|
49
67
|
|
|
50
68
|
var _Video = _interopRequireWildcard(require("./Video"));
|
|
@@ -67,7 +85,10 @@ var _default = {
|
|
|
67
85
|
Image: _Image.default,
|
|
68
86
|
backgroundUpload: _Video.backgroundUpload,
|
|
69
87
|
getDetails: _utils.getDetails,
|
|
70
|
-
uuidv4: _utils.uuidv4
|
|
88
|
+
uuidv4: _utils.uuidv4,
|
|
89
|
+
generateFilePath: _utils.generateFilePath,
|
|
90
|
+
getRealPath: _utils.getRealPath,
|
|
91
|
+
getVideoMetaData: _utils.getVideoMetaData
|
|
71
92
|
};
|
|
72
93
|
exports.default = _default;
|
|
73
94
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["Video","Audio","Image","backgroundUpload","getDetails","uuidv4"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["Video","Audio","Image","backgroundUpload","getDetails","uuidv4","generateFilePath","getRealPath","getVideoMetaData"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;eAqBe;AACbA,EAAAA,KAAK,EAALA,cADa;AAEbC,EAAAA,KAAK,EAALA,cAFa;AAGbC,EAAAA,KAAK,EAALA,cAHa;AAIbC,EAAAA,gBAAgB,EAAhBA,uBAJa;AAKbC,EAAAA,UAAU,EAAVA,iBALa;AAMbC,EAAAA,MAAM,EAANA,aANa;AAObC,EAAAA,gBAAgB,EAAhBA,uBAPa;AAQbC,EAAAA,WAAW,EAAXA,kBARa;AASbC,EAAAA,gBAAgB,EAAhBA;AATa,C","sourcesContent":["import Video, { VideoCompressorType, backgroundUpload } from './Video';\nimport Audio from './Audio';\nimport Image from './Image';\nimport {\n getDetails,\n uuidv4,\n generateFilePath,\n getRealPath,\n getVideoMetaData,\n} from './utils';\n\nexport {\n Video,\n Audio,\n Image,\n backgroundUpload,\n //type\n VideoCompressorType,\n getDetails,\n uuidv4,\n generateFilePath,\n getRealPath,\n getVideoMetaData,\n};\nexport default {\n Video,\n Audio,\n Image,\n backgroundUpload,\n getDetails,\n uuidv4,\n generateFilePath,\n getRealPath,\n getVideoMetaData,\n};\n"]}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.uuidv4 = exports.checkUrlAndOptions = exports.getDetails = exports.DEFAULT_COMPRESS_AUDIO_OPTIONS = exports.AUDIO_BITRATE = void 0;
|
|
6
|
+
exports.uuidv4 = exports.checkUrlAndOptions = exports.getDetails = exports.getVideoMetaData = exports.getRealPath = exports.generateFilePath = exports.DEFAULT_COMPRESS_AUDIO_OPTIONS = exports.AUDIO_BITRATE = void 0;
|
|
7
7
|
|
|
8
8
|
var _reactNative = require("react-native");
|
|
9
9
|
|
|
@@ -23,12 +23,26 @@ const DEFAULT_COMPRESS_AUDIO_OPTIONS = {
|
|
|
23
23
|
};
|
|
24
24
|
exports.DEFAULT_COMPRESS_AUDIO_OPTIONS = DEFAULT_COMPRESS_AUDIO_OPTIONS;
|
|
25
25
|
|
|
26
|
-
const
|
|
26
|
+
const generateFilePath = extension => {
|
|
27
27
|
return new Promise((resolve, reject) => {
|
|
28
|
-
Compressor.
|
|
28
|
+
Compressor.generateFilePath(extension).then(result => resolve('file://' + result)).catch(error => reject(error));
|
|
29
29
|
});
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
+
exports.generateFilePath = generateFilePath;
|
|
33
|
+
|
|
34
|
+
const getRealPath = (path, type = 'video') => {
|
|
35
|
+
return Compressor.getRealPath(path, type);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.getRealPath = getRealPath;
|
|
39
|
+
|
|
40
|
+
const getVideoMetaData = path => {
|
|
41
|
+
return Compressor.getVideoMetaData(path);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
exports.getVideoMetaData = getVideoMetaData;
|
|
45
|
+
|
|
32
46
|
const isValidUrl = url => /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/.test(url);
|
|
33
47
|
|
|
34
48
|
const getFullFilename = path => {
|
|
@@ -120,7 +134,7 @@ const checkUrlAndOptions = async (url, options) => {
|
|
|
120
134
|
outputFilePath = options.outputFilePath;
|
|
121
135
|
defaultResult.outputFilePath = outputFilePath;
|
|
122
136
|
} else {
|
|
123
|
-
outputFilePath = await
|
|
137
|
+
outputFilePath = await generateFilePath('mp3');
|
|
124
138
|
defaultResult.outputFilePath = outputFilePath;
|
|
125
139
|
}
|
|
126
140
|
|