iOS之AVFoundation视频转码

利用AVFoundation框架实现视频格式转码,下面以mov转mp4为例:


/**mov转mp4格式*/
-(void)convertMovSourceURL:(NSURL *)sourceUrl
{
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:AVAssetExportPresetHighestQuality];
NSDateFormatter *formater=[[NSDateFormatter alloc] init];//用时间给文件全名
[formater setDateFormat:@"yyyyMMddHHmmss"];
NSString *mp4Path=[[NSUserDefaults standardUserDefaults] objectForKey:@"kMP4FilePath"];
NSString *resultPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0] stringByAppendingFormat:@"/output-%@.mp4", [formater stringFromDate:[NSDate date]]];

    exportSession.outputURL=[NSURL fileURLWithPath:resultPath];
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.shouldOptimizeForNetworkUse = YES;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void){
         switch (exportSession.status) {
            case AVAssetExportSessionStatusCancelled:
                 NSLog(@"AVAssetExportSessionStatusCancelled");
                 break;
             case AVAssetExportSessionStatusUnknown:
                 NSLog(@"AVAssetExportSessionStatusUnknown");
                 break;
             case AVAssetExportSessionStatusWaiting:
                 NSLog(@"AVAssetExportSessionStatusWaiting");
                 break;
             case AVAssetExportSessionStatusExporting:
                 NSLog(@"AVAssetExportSessionStatusExporting");
                 break;
             case AVAssetExportSessionStatusCompleted:
             {
                 //NSLog(@"resultPath = %@",resultPath);
                 UIAlertController \*alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"转换完成" preferredStyle:UIAlertControllerStyleAlert];
                 UIAlertAction \*confirm=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                 [alert addAction:confirm];
                 [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
                 BOOL success=[[NSFileManager defaultManager]moveItemAtPath:resultPath toPath:[mp4Path stringByAppendingPathComponent:@"1.mp4"] error:nil];
                 if(success)
                 {
                     NSArray \*files=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:mp4Path error:nil];
                     NSLog(@"%@",files);
                     NSLog(@"success");
                 }
                 
                 break;
             }
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"AVAssetExportSessionStatusFailed");
                 break;
         }
     }];
}

}


用法如下:

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    self.title=@"视屏格式转换";

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [btn setTitle:@"转换" forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(convert:) forControlEvents:UIControlEventTouchUpInside];
    btn.center=self.view.center;
    }

pragma mark - UIImagePicker

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoURL=[info objectForKey:@"UIImagePickerControllerMediaURL"];
//[ProgressHUD show:@"转换中..."];
[self convertMovSourceURL:videoURL];
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}

iOS之AVFoundation视频转码_第1张图片

你可能感兴趣的:(iOS之AVFoundation视频转码)