Skip to content

Commit 9bbca7b

Browse files
authored
feat(duration): add video duration to response (react-native-image-picker#1676)
1 parent 0c9641d commit 9bbca7b

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ The `callback` will be called with a response object, refer to [The Response Obj
105105
| fileSize | OK | OK | The file size (photos only) |
106106
| type | OK | OK | The file type (photos only) |
107107
| fileName | OK | OK | The file name |
108+
| duration | OK | OK | The selected video duration in seconds |
108109
109110
## Note on file storage
110111

android/src/main/java/com/imagepicker/Utils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.graphics.Bitmap;
1212
import android.graphics.BitmapFactory;
1313
import android.hardware.camera2.CameraCharacteristics;
14+
import android.media.MediaMetadataRetriever;
1415
import android.net.Uri;
1516
import android.os.Build;
1617
import android.os.ParcelFileDescriptor;
@@ -251,6 +252,14 @@ static double getFileSize(Uri uri, Context context) {
251252
}
252253
}
253254

255+
static int getDuration(Uri uri, Context context) {
256+
MediaMetadataRetriever m = new MediaMetadataRetriever();
257+
m.setDataSource(context, uri);
258+
int duration = Math.round(Float.parseFloat(m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))) / 1000;
259+
m.release();
260+
return duration;
261+
}
262+
254263
static boolean shouldResizeImage(int origWidth, int origHeight, Options options) {
255264
if ((options.maxWidth == 0 || options.maxHeight == 0) && options.quality == 100) {
256265
return false;
@@ -346,10 +355,10 @@ static ReadableMap getResponseMap(Uri uri, Options options, Context context) {
346355

347356
static ReadableMap getVideoResponseMap(Uri uri, Context context) {
348357
String fileName = uri.getLastPathSegment();
349-
350358
WritableMap map = Arguments.createMap();
351359
map.putString("uri", uri.toString());
352360
map.putDouble("fileSize", getFileSize(uri, context));
361+
map.putInt("duration", getDuration(uri, context));
353362
map.putString("fileName", fileName);
354363
return map;
355364
}

ios/ImagePickerManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ - (void)onVideoObtained:(NSDictionary<NSString *,id> *)info {
186186
}
187187
}
188188

189+
AVAsset *asset = [AVAsset assetWithURL:videoDestinationURL];
190+
self.response[@"duration"] = @(roundf(CMTimeGetSeconds(asset.duration)));
189191
self.response[@"uri"] = videoDestinationURL.absoluteString;
190192
self.callback(@[self.response]);
191193
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface ImagePickerResponse {
2626
fileSize?: number;
2727
type?: string; //TODO
2828
fileName?: string;
29+
duration?: number;
2930
}
3031

3132
export type PhotoQuality =

0 commit comments

Comments
 (0)