#import <AVFoundation/AVFoundation.h>
- (void)movToMP4:(NSString *)path completionHandler:(void(^)(NSURL * URL))finalUrl{
// path文件存放路徑
AVURLAsset *avAsset = [AVURLAssetURLAssetWithURL:[NSURLfileURLWithPath:path] options:nil];
NSArray *compatiblePresets = [AVAssetExportSessionexportPresetsCompatibleWithAsset:avAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
AVAssetExportSession *exportSession = [[AVAssetExportSessionalloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
NSString *exportPath = [NSStringstringWithFormat:@"%@/%@.mp4",[NSHomeDirectory() stringByAppendingString:@"/tmp"],@"1"];
exportSession.outputURL = [NSURLfileURLWithPath:exportPath];
NSLog(@"exportPath%@", exportPath);
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
caseAVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
break;
caseAVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
caseAVAssetExportSessionStatusCompleted:{ NSLog(@"轉換成功");
finalUrl(exportSession.outputURL);
}
break;
default:
break;
}
}];
}
}
隨筆隨記,以免忘記