ios 开发 视频格式转换、mov转MP4
NSURL*videoUrl = mediaURL;
(格式) media = @"file:///private/var/mobile/Containers/Data/Application/0541F390-33AE-4910-BFC3-30819DF8317C/tmp/0A809814-E89A-42D9-9760-F9E0FDDA9B4A.MOV"
/**
* mov格式转mp4格式
*/
AVURLAsset*avAsset = [AVURLAssetURLAssetWithURL:[NSURLfileURLWithPath:videoUrl.absoluteString]options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
NSArray*compatiblePresets = [AVAssetExportSessionexportPresetsCompatibleWithAsset:avAsset];
/**
AVAssetExportPresetMediumQuality 表示视频的转换质量,
*/
if([compatiblePresetscontainsObject:AVAssetExportPresetMediumQuality]) {
NSURL*newVideoUrl ;//一般.mp4
NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
[formatersetDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;
exportSession.outputURL= newVideoUrl;
//要转换的格式,这里使用 MP4
exportSession.outputFileType=AVFileTypeMPEG4;
//转换的数据是否对网络使用优化
exportSession.shouldOptimizeForNetworkUse=YES;
//异步处理开始转换
[exportSessionexportAsynchronouslyWithCompletionHandler:^(void)
{
//转换状态监控
switch(exportSession.status) {
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusCompleted:
{
//转换完成
NSLog(@"AVAssetExportSessionStatusCompleted");
break;
}
}
}];
}