Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS UIImagePicker with mp4 format

Is it possible to save video and add it to custom ALAsset, captured from UIImagePicker in mp4 format? Or I have to save it in .mov and make compression by AVAssetExportSession?

like image 716
mbutan Avatar asked Nov 04 '22 08:11

mbutan


1 Answers

Yes, you can compress video using AVAssetExportSession. Here you can specify video type, quality and output url for compress video.

See below methods:

- (void) saveVideoToLocal:(NSURL *)videoURL {

    @try {
        NSArray *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docPath = [documentsDirectory objectAtIndex:0];

        NSString *videoName = [NSString stringWithFormat:@"sampleVideo.mp4"];
        NSString *videoPath = [docPath stringByAppendingPathComponent:videoName];

        NSURL *outputURL = [NSURL fileURLWithPath:videoPath];

        NSLog(@"Loading video");

        [self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {

             if (exportSession.status == AVAssetExportSessionStatusCompleted) {
                 NSLog(@"Compression is done");
             }
             [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
         }];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception :%@",exception.description);
        [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
    }
}


//---------------------------------------------------------------

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler {
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        handler(exportSession);
    }];
}

Here I saved compress video to document directory of application. You can check detail working of this in below sample code:

Sample demo:

like image 186
Kampai Avatar answered Nov 17 '22 08:11

Kampai