The best way to compress video.
Here is the code.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; NSData *data = [NSData dataWithContentsOfURL:videoURL]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0]; NSString *caldate = [NSString stringWithFormat:@"%@.mov",[now description]]; caldate = [caldate stringByReplacingOccurrencesOfString:@" " withString:@""]; NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory,caldate]; NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; NSURL *selectedVideoUrl; if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) { tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; } NSLog(@"old move %@",path); NSURL *url = [NSURL fileURLWithPath:tempFilePath]; [data writeToFile:path atomically:YES];
}
-(void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler{ AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality]; exportSession.outputURL = outputURL; if ([[UIApplication sharedApplication]canOpenURL:inputURL]){ NSLog(@"open"); } exportSession.outputFileType = AVFileTypeQuickTimeMovie; NSLog(@"errrsfseSession %@", exportSession.error); [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { if(exportSession.status != AVAssetExportSessionStatusCompleted){ NSLog(@"exportSession %@", exportSession.error); } if (exportSession.status == AVAssetExportSessionStatusCompleted) { NSLog(@"doneszdfsadj"); } }]; }
source share