视频压缩

1.介绍

2.使用步骤

1.到相册中获取到视频
2.对视频进行压缩
3.保存到沙盒目录

3.使用

//获得视频资源
- (void)videoTransform{
    // 1.去相册里面拿到视频
    UIImagePickerController *pc = [[UIImagePickerController alloc] init];
    pc.delegate = self;
    // 1.1 资源来源配置
    pc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 1.2 资源类型, 指定只能显示视频文件
    pc.mediaTypes = @[(NSString *)kUTTypeMovie];
    // 2.弹出视频资源
    [self presentViewController:pc animated:YES completion:nil];
}

//转化视频
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //1.控制器销毁
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //2.视频文件的URL
    NSURL *fileUrl = info[UIImagePickerControllerMediaURL];
    
    //3.沙盒目录 Document
    NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *destFilePath = [docuPath stringByAppendingPathComponent:@"original.MOV"];
    NSURL *destUrl = [NSURL fileURLWithPath:destFilePath];
    
    //4.将视频文件copy到沙盒目录中
    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *error = nil;
    [manager copyItemAtURL:fileUrl toURL:destUrl error:&error];
    
    //5.视频进行压缩 (将视频加载, 导出代质量版本)
    //5.1加载视频资源
    AVAsset *asset = [AVAsset assetWithURL:destUrl];
    //5.2创建视频资源导出会话
    /**
     NSString *const AVAssetExportPresetLowQuality; // 低质量
     NSString *const AVAssetExportPresetMediumQuality;
     NSString *const AVAssetExportPresetHighestQuality; //高质量
     */
    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
    //5.3创建导出视频的URL
    NSString *resultPath = [docuPath stringByAppendingPathComponent:@"dest.MOV"];
    session.outputURL = [NSURL fileURLWithPath:resultPath];
    //5.4必须配置输出属性
    session.outputFileType = @"com.apple.quicktime-movie";
    //5.5导出视频
    [session exportAsynchronouslyWithCompletionHandler:^{
        NSLog(@"视频导出完成");
    }];
}

你可能感兴趣的:(视频压缩)