具体操作如下
-(void)handleVideoAccroding:(NSURL *)originFilePath
{
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *outputPath = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"fdvideo"]];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputPath]) {
[[NSFileManager defaultManager] removeItemAtPath:outputPath error:nil];
}
//创建一个由URL标识的代表任何资源的assert对象
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:originFilePath options:nil];
//转码配置 建议选择AVAssetExportPresetMediumQuality
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
//设置视频转码输出路径
exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
//AVFileTypeMPEG4 文件输出类型,可以更改,是枚举类型,官方有提供,更改该值也可以改变视频的压缩比例
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatus exportStatus = exportSession.status;
switch (exportStatus)
{
case AVAssetExportSessionStatusFailed:
{
NSError *exportError = exportSession.error;
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
LHLog(@"转码失败 AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted:
{
LHLog(@"视频转码成功");
dispatch_async(dispatch_get_main_queue(), ^{
NSData *data = [NSData dataWithContentsOfFile:outputPath];
[self uploadPhoto:data andInt:0 andCount:self.imagesDataI.count];
});
}
break;
default:
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
break;
}
}];
}