由于苹果拍摄的视频并非mp4格式而是MOV,考虑兼容性的问题,so上传后台的时候最好转成和Android通用的格式,比如mp4
- (void)movFileTransformToMP4WithSourceUrl:(id)sourceUrl completion:(void(^)(NSString*Mp4FilePath))comepleteBlock
{
/**
* mov格式转mp4格式
*/
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:sourceUrl options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
NSLog(@"%@",compatiblePresets);
if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
//这里可以选择一个合适的分辨率
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
//时间戳命名
NSDate*date = [NSDatedate];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formattersetDateFormat:@"yyyyMMddHHmmss"];
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
self.videoName = [NSString stringWithFormat:@"%@.mp4",[formatter stringFromDate:date]];
self.imageName = [NSString stringWithFormat:@"%@.jpg",[formatter stringFromDate:date]];
NSString* resultPath = [pathDocumentsstringByAppendingPathComponent:self.videoName];
exportSession.outputURL= [NSURLfileURLWithPath:resultPath];
exportSession.outputFileType = AVFileTypeMPEG4;//可以配置多种输出文件格式
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSessionexportAsynchronouslyWithCompletionHandler:^(void)
{
switch(exportSession.status) {
case AVAssetExportSessionStatusUnknown:
// NSLog(@"AVAssetExportSessionStatusUnknown");
// CLOUDMESSAGETIPS(@"视频格式转换出错Unknown", 0.8); //自定义错误提示信息
break;
case AVAssetExportSessionStatusWaiting:
// NSLog(@"AVAssetExportSessionStatusWaiting");
// CLOUDMESSAGETIPS(@"视频格式转换出错Waiting", 0.8);
break;
case AVAssetExportSessionStatusExporting:
// NSLog(@"AVAssetExportSessionStatusExporting");
// CLOUDMESSAGETIPS(@"视频格式转换出错Exporting", 0.8);
break;
case AVAssetExportSessionStatusCompleted:
{
// NSLog(@"AVAssetExportSessionStatusCompleted");
comepleteBlock(resultPath);
NSLog(@"mp4 file size:%lf MB",[NSData dataWithContentsOfURL:exportSession.outputURL].length/1024.f/1024.f);
}
break;
case AVAssetExportSessionStatusFailed:
// NSLog(@"AVAssetExportSessionStatusFailed");
// CLOUDMESSAGETIPS(@"视频格式转换出错Unknown", 0.8);
break;
case AVAssetExportSessionStatusCancelled:
// NSLog(@"AVAssetExportSessionStatusFailed");
// CLOUDMESSAGETIPS(@"视频格式转换出错Cancelled", 0.8);
break;
}
}];
}
}