iOS视频转码 mov 转 mp4

 
 

利用AVFoundation框架实现视频格式转码,下面以mov转mp4为例: <pre> /**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;
         }
     }];
}

}</pre>
用法如下:
<pre>
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@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];
}
</pre>



作者:CGPointZero
链接:http://www.jianshu.com/p/25c218019fa0
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值