Skip to content

Commit

Permalink
feat(duration): add video duration to response (#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravirajn22 committed Apr 28, 2021
1 parent 0c9641d commit 9bbca7b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ The `callback` will be called with a response object, refer to [The Response Obj
| fileSize | OK | OK | The file size (photos only) |
| type | OK | OK | The file type (photos only) |
| fileName | OK | OK | The file name |
| duration | OK | OK | The selected video duration in seconds |
## Note on file storage
Expand Down
11 changes: 10 additions & 1 deletion android/src/main/java/com/imagepicker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.camera2.CameraCharacteristics;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
Expand Down Expand Up @@ -251,6 +252,14 @@ static double getFileSize(Uri uri, Context context) {
}
}

static int getDuration(Uri uri, Context context) {
MediaMetadataRetriever m = new MediaMetadataRetriever();
m.setDataSource(context, uri);
int duration = Math.round(Float.parseFloat(m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))) / 1000;
m.release();
return duration;
}

static boolean shouldResizeImage(int origWidth, int origHeight, Options options) {
if ((options.maxWidth == 0 || options.maxHeight == 0) && options.quality == 100) {
return false;
Expand Down Expand Up @@ -346,10 +355,10 @@ static ReadableMap getResponseMap(Uri uri, Options options, Context context) {

static ReadableMap getVideoResponseMap(Uri uri, Context context) {
String fileName = uri.getLastPathSegment();

WritableMap map = Arguments.createMap();
map.putString("uri", uri.toString());
map.putDouble("fileSize", getFileSize(uri, context));
map.putInt("duration", getDuration(uri, context));
map.putString("fileName", fileName);
return map;
}
Expand Down
2 changes: 2 additions & 0 deletions ios/ImagePickerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ - (void)onVideoObtained:(NSDictionary<NSString *,id> *)info {
}
}

AVAsset *asset = [AVAsset assetWithURL:videoDestinationURL];
self.response[@"duration"] = @(roundf(CMTimeGetSeconds(asset.duration)));
self.response[@"uri"] = videoDestinationURL.absoluteString;
self.callback(@[self.response]);
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ImagePickerResponse {
fileSize?: number;
type?: string; //TODO
fileName?: string;
duration?: number;
}

export type PhotoQuality =
Expand Down

0 comments on commit 9bbca7b

Please sign in to comment.