關于視頻壓縮并轉mp4

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


?? ? ? ? ? ? }


?? ? ? ? }];


? ? }

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容