souvik-ghosh / react-native-create-thumbnail

iOS/Android thumbnail generator with support for both local and remote videos
MIT License
246 stars 105 forks source link

timeStamp not working #79

Closed dequidv closed 1 year ago

dequidv commented 1 year ago

Description No matter what timeStamp I put, it always create a thumbnail with the first frame. The thumbnail is rightfully created but not at the timeStamp I use. The timeStamp I try to put is equal to (video duration * 1000) / 2. The video used as reference is then played in react-native video library.

To Reproduce Steps to reproduce the behavior:

  1. Create any thumbnail from remote or local (in cache) video with timeStamp > 0 Exemple of path of used file : file:///data/user/0/fr.ochy.app/cache/954579d2-c171-4346-8646-7a5366f940c4.mp4 Version react-native-create-thumbnail 1.6.3 react-native: 0.68,

Expected Results Describe what you expected to happen.

Snack, code example, screenshot, or link to a repository

Here is how I use the method createThumbnail

try { const response = await createThumbnail({ url: analysis?.data?.videoLocalUri, timeStamp: analysis?.data?.videoData?.duration ? (analysis?.data?.videoData?.duration * 1000) / 2 : 0, }); setThumbnail(response?.path); } catch (err) { console.error(err); } finally { setStatus('success'); setLoading(false); }

cnickless commented 1 year ago

I'm experiencing the same issue on iOS with RN 0.70.4 and v1.6.4 of this library.

The video is remote (https). Can confirm that each thumbnail passes an accurate timestamp but each image draws the first frame of the video.

interstates21 commented 1 year ago

+1

Timestamp works for me, but the threshold is huge, so it misses a specific frame and captures a random frame in a range of 5-10 sec

davetodd commented 1 year ago

I posted a PR that fixes this in my testing, but if anyone wants to use patch-package instead, this is what I've added:

diff --git a/node_modules/react-native-create-thumbnail/ios/CreateThumbnail.m b/node_modules/react-native-create-thumbnail/ios/CreateThumbnail.m
index 92cc49f..6bfa4a9 100644
--- a/node_modules/react-native-create-thumbnail/ios/CreateThumbnail.m
+++ b/node_modules/react-native-create-thumbnail/ios/CreateThumbnail.m
@@ -112,6 +112,8 @@ - (void) cleanDir:(NSString *)path forSpace:(unsigned long long)size {

 - (void) generateThumbImage:(AVURLAsset *)asset atTime:(int)timeStamp completion:(void (^)(UIImage* thumbnail))completion failure:(void (^)(NSError* error))failure {
     AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
+    generator.requestedTimeToleranceAfter = CMTimeMake(0, 1000);
+    generator.requestedTimeToleranceBefore = CMTimeMake(0, 1000);
     generator.appliesPreferredTrackTransform = YES;
     generator.maximumSize = CGSizeMake(512, 512);
     CMTime time = CMTimeMake(timeStamp, 1000);