import "AssetsLibrary/AssetsLibrary.h"
- (void)mov2mp4:(NSURL *)movUrl
{
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:movUrl options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
/**
AVAssetExportPresetMediumQuality 表示视频的转换质量,
*/
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
//转换完成保存的文件路径
NSString * resultPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4",@"cvt1"];
exportSession.outputURL = [NSURL fileURLWithPath:resultPath];
//要转换的格式,这里使用 MP4
exportSession.outputFileType = AVFileTypeMPEG4;
//转换的数据是否对网络使用优化
exportSession.shouldOptimizeForNetworkUse = YES;
//异步处理开始转换
[exportSession exportAsynchronouslyWithCompletionHandler:^(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");
//测试使用,保存在手机相册里面
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:exportSession.outputURL completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"%@",error);
}
}];
break;
}
}
}];
}
}
网友评论