Skip to content

Commit

Permalink
fix(mp4): no longer hang on export when taking a video (#2097)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan-dutoit <johan.dutoit@peppy.health>
  • Loading branch information
Johan-dutoit and Johan-dutoit committed Mar 14, 2023
1 parent cf4d608 commit ee8be86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const actions: Action[] = [
type: 'capture',
options: {
saveToPhotos: true,
formatAsMp4: true,
mediaType: 'video',
includeExtra,
},
Expand Down
38 changes: 19 additions & 19 deletions ios/ImagePickerManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl
}

NSMutableDictionary *response = [[NSMutableDictionary alloc] init];
response[@"fileName"] = fileName;

if([self.options[@"formatAsMp4"] boolValue]) {
NSURL *parentURL = [videoDestinationURL URLByDeletingLastPathComponent];
Expand All @@ -270,35 +269,36 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl

[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoDestinationURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];

exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = YES;

dispatch_semaphore_t sem = dispatch_semaphore_create(0);

dispatch_async(dispatch_get_main_queue(), ^(void) {
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
CGSize dimentions = [ImagePickerUtils getVideoDimensionsFromUrl:outputURL];
response[@"duration"] = [NSNumber numberWithDouble:CMTimeGetSeconds([AVAsset assetWithURL:outputURL].duration)];
response[@"uri"] = outputURL.absoluteString;
response[@"type"] = [ImagePickerUtils getFileTypeFromUrl:outputURL];
response[@"fileSize"] = [ImagePickerUtils getFileSizeFromUrl:outputURL];
response[@"width"] = @(dimentions.width);
response[@"height"] = @(dimentions.height);
dispatch_semaphore_signal(sem);
} else if (exportSession.status == AVAssetExportSessionStatusFailed || exportSession.status == AVAssetExportSessionStatusCancelled) {
dispatch_semaphore_signal(sem);
}
}];
});
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
CGSize dimentions = [ImagePickerUtils getVideoDimensionsFromUrl:outputURL];
response[@"fileName"] = [outputURL lastPathComponent];
response[@"duration"] = [NSNumber numberWithDouble:CMTimeGetSeconds([AVAsset assetWithURL:outputURL].duration)];
response[@"uri"] = outputURL.absoluteString;
response[@"type"] = [ImagePickerUtils getFileTypeFromUrl:outputURL];
response[@"fileSize"] = [ImagePickerUtils getFileSizeFromUrl:outputURL];
response[@"width"] = @(dimentions.width);
response[@"height"] = @(dimentions.height);

dispatch_semaphore_signal(sem);
} else if (exportSession.status == AVAssetExportSessionStatusFailed || exportSession.status == AVAssetExportSessionStatusCancelled) {
dispatch_semaphore_signal(sem);
}
}];


dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
} else {
CGSize dimentions = [ImagePickerUtils getVideoDimensionsFromUrl:videoDestinationURL];

response[@"fileName"] = fileName;
response[@"duration"] = [NSNumber numberWithDouble:CMTimeGetSeconds([AVAsset assetWithURL:videoDestinationURL].duration)];
response[@"uri"] = videoDestinationURL.absoluteString;
response[@"type"] = [ImagePickerUtils getFileTypeFromUrl:videoDestinationURL];
Expand Down

0 comments on commit ee8be86

Please sign in to comment.