iOS之视频压缩


//视频压缩的原理就是以低质量的导出


- (void)viewDidLoad {

    [superviewDidLoad];

   

    //从相册中选择视频出来

    UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

    

    //设置素材的来源

    self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    self.picker = picker;

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 相片库

     UIImagePickerControllerSourceTypeCamera, 相机

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 相册

     */

    NSLog(@"%@",[UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]);

    

    self.picker.delegate =self;

    //设置多媒体类型  图片  音频视频

    self.picker.mediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info

{

//    NSLog(@"%@",info);

//    UIImage *image =   info[UIImagePickerControllerOriginalImage];

//    self.imageView.image = image;

    [self.pickerdismissViewControllerAnimated:YEScompletion:nil];

    

    //获取视频的url

    NSURL *url =  info[UIImagePickerControllerMediaURL];

    

    //视频压缩

    //8M  200KB 应用场景  对像素要求不高但是需要看到视频的场景

    // 小视频 网络传输

    

    //压缩原理就是以低质量的导出

    

    AVAsset *asset = [AVAssetassetWithURL:url];

    //参数1是素材对象 参数2 压缩的质量

    AVAssetExportSession *session = [[AVAssetExportSessionalloc]initWithAsset:assetpresetName:AVAssetExportPresetMediumQuality];

    //建议 : AVAssetExportPresetHighestQuality 不压缩 AVAssetExportPresetLowQuality低质量

    //AVAssetExportPresetMediumQuality 中间

    //路径 放在什么地方?

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"hello.m4v"];

    

    //删除之前存在的文件 如果没有就设么都不做

    [[NSFileManagerdefaultManager]removeItemAtPath:patherror:nil];

    

    session.outputURL = [NSURLfileURLWithPath:path];

    //文件格式

//    self.session = session;

    NSLog(@"%@",session.supportedFileTypes);

    session.outputFileType =AVFileTypeQuickTimeMovie;

//    NSLog(@"%@",NSHomeDirectory());

    //导出

    [session exportAsynchronouslyWithCompletionHandler:^{

        NSLog(@"导出完成!!!");

    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [selfpresentViewController:self.pickeranimated:YEScompletion:nil];

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值