ビデオを圧縮する最良の方法。
これがコードです。
-(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];
//Delete the original asset
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *path1 = [NSString stringWithFormat:@"%@/%@", documentsDirectory1,caldate];
NSURL *url1 = [NSURL fileURLWithPath:path1];
NSLog(@"new move %@",path);
[self convertVideoToLowQuailtyWithInputURL:url outputURL:url1 handler:Nil];
[picker dismissModalViewControllerAnimated: 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");
}
}];
}