diff --git a/README.md b/README.md index 78e6faa..f007343 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,9 @@ RNThumbnail.get(filepath).then((result) => { console.log(result.path); // thumbnail path }) ``` + +### Options + +`RNThumbnail.get` supports a second argument which is a object with the following possible options: + +- `timestamp` (currently `ios` only): The timestamp of the thumbnail in seconds (calculates the frame based on the FPS of the video). Default Value: `0` diff --git a/index.js b/index.js index c480aff..4d83631 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,19 @@ -import { NativeModules } from 'react-native'; +import { NativeModules, Platform } from 'react-native'; -const { RNThumbnail } = NativeModules; +const { RNThumbnail: NativeRNThumbnail } = NativeModules; -export default RNThumbnail; +const defaultOptions = { + timestamp: 0, + quality: 1 +} + +export default class RNThumbnail { + static get(path, options = {}) { + if (Platform.OS === 'ios') { + const mergedOptions = {...defaultOptions, ...options} + return NativeRNThumbnail.get(path, mergedOptions.timestamp, mergedOptions.quality) + } + return NativeRNThumbnail.get(path) + } +}; diff --git a/ios/RNThumbnail.m b/ios/RNThumbnail.m index 8310709..12a3095 100644 --- a/ios/RNThumbnail.m +++ b/ios/RNThumbnail.m @@ -12,8 +12,11 @@ - (dispatch_queue_t)methodQueue } RCT_EXPORT_MODULE() -RCT_EXPORT_METHOD(get:(NSString *)filepath resolve:(RCTPromiseResolveBlock)resolve - reject:(RCTPromiseRejectBlock)reject) +RCT_EXPORT_METHOD(get:(NSString *)filepath + timestamp:(NSNumber * __nonnull)timestamp + quality:(NSNumber * __nonnull)quality + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) { @try { filepath = [filepath stringByReplacingOccurrencesOfString:@"file://" @@ -21,11 +24,17 @@ - (dispatch_queue_t)methodQueue NSURL *vidURL = [NSURL fileURLWithPath:filepath]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil]; + int64_t value = 1; + if (timestamp != 0) { + AVAssetTrack * videoAssetTrack = [asset tracksWithMediaType: AVMediaTypeVideo].firstObject; + value = [timestamp longLongValue] * videoAssetTrack.nominalFrameRate; + } + AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; generator.appliesPreferredTrackTransform = YES; NSError *err = NULL; - CMTime time = CMTimeMake(1, 60); + CMTime time = CMTimeMake(value, 60); CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err]; UIImage *thumbnail = [UIImage imageWithCGImage:imgRef]; @@ -34,7 +43,7 @@ - (dispatch_queue_t)methodQueue NSUserDomainMask, YES) lastObject]; - NSData *data = UIImageJPEGRepresentation(thumbnail, 1.0); + NSData *data = UIImageJPEGRepresentation(thumbnail, [quality floatValue]); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *fullPath = [tempDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"thumb-%@.jpg", [[NSProcessInfo processInfo] globallyUniqueString]]]; [fileManager createFileAtPath:fullPath contents:data attributes:nil]; @@ -48,4 +57,3 @@ - (dispatch_queue_t)methodQueue } @end -