由于蘋果拍攝的視頻并非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;
?? ? ? ? ? ? }
?? ? ? ? }];
? ? }
}