美文网首页IT圈ios框架视频 音频
iOS之AVFoundation视频转码

iOS之AVFoundation视频转码

作者: CGPointZero | 来源:发表于2016-03-14 13:19 被阅读3646次

    利用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>

    相关文章

      网友评论

      • 漫步的小蚂蚁:问下 ,MOV转MP4,第一次录视频可以转,同一个路径,就是覆盖式再录一次,进行转码,就会出现存储失败,什么情况,求告知
        漫步的小蚂蚁:解决了,每次转之前把之前路径的视频删掉, 不会主动覆盖的.
      • 采代码的小蘑菇:报未知错误怎么办
      • 7cfc6cb3d757:请问如何获取转码过程中的进度值呢,需要给出进度值给用户看
      • 634585edc862:请问mov怎么转avi
      • 缭雾:没有必要转 貌似 MOV容器兼容MP4 . 所以只要改下后缀就行了吧
      • 桐生一猫:请问这个转码需要多久啊
        CGPointZero:@桐生一猫 不播放,这些代码只实现了转码功能
        桐生一猫:@CGPointZero 是边播放边转码,还是边录制边转码
        CGPointZero:@桐生一猫 这个是异步转码的,你可以测试下,100M的几秒钟吧,大的我没测试

      本文标题:iOS之AVFoundation视频转码

      本文链接:https://www.haomeiwen.com/subject/owsflttx.html